Solved

How do I pass a variable from my masterpage to a content page?

Posted on 2008-10-08
6
619 Views
Last Modified: 2013-12-17
Experts,

I have an asp:dropdown on my master page and I need to pass the value a user selects in the dropdown to another page that also inherits the same materpage.

I have an onselectedindexchanged method that takes the value of the dropdown and assigns it to a public variable and then redirects to another page that also inherits the same master page. Is this the right way to go about what I need and if so how can I access this public variable in the page I redirect to?

I have spent all evening reading and trying different ways of accessing the variable with no luck.

MasterPage.master.cs:
 

protected void DropdownlistCategories_SelectedIndexChanged(object sender, EventArgs e)

{

  LabelCategory.Text = DropdownlistCategories.SelectedItem.Text;

  Response.Redirect("jobseekers/joblistings.aspx");

}
 
 

MasterPage.master:
 

<asp:dropdownlist ID="DropdownlistCategories" runat="server" 

DataSourceID="XMLSourceJobCategory" DataTextField="display" AutoPostBack="true" 

DataValueField="value" OnTextChanged="DropdownlistCategories_SelectedIndexChanged"

></asp:dropdownlist>

<asp:XmlDataSource ID="XMLSourceJobCategory" runat="server" DataFile="~/App_Data/jobcategory.xml"></asp:XmlDataSource>
 
 

joblistings.aspx:
 

<%@ Page Title="" Language="C#" MasterPageFile="~/Right.master" AutoEventWireup="true" CodeFile="joblistings.aspx.cs" Inherits="joblistings" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder_Text" Runat="Server">

    <asp:Label ID="LabelPassString" runat="server" Text=""></asp:Label>

</asp:Content>
 
 

joblistings.aspx.cs:
 

protected void Page_Load(object sender, EventArgs e)

{

  LabelPassString = this.Master.Page.Form.FindControl("LabelCategory") as Label;

}

Open in new window

0
Comment
Question by:Denisdh
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:novynov
ID: 22674455
Unless I'm misunderstanding, I don't think this will work - at least if you are moving between two pages that both utilize the same master page. Although you are using the same master page, by redirecting to another page, you are getting a "fresh instance" of the master page: the state/property values from the master page instance of the previous page are no longer available. I did a little test of this with a public datetime property that gets instantiated on the load of the master page. When I did a postback that redirected to another page (both used the same master page), the datetime property was set back to DateTime.MinValue...as I expected.

If you need to store a selected value on one page, for use on another, you may need to consider a Session variable, a cookie, or passing a querystring value. Each has it's merits and liabilities. It all depends on the problem we're trying to solve - and the constraints we have to work with.

I hope this helps. Let me know if I can be of further assistance.



0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22675144
the following line will give you the dropdownlist controls access on the master page in the content page
DropDownList  ddl = (DropDownList)Page.Master.FindControl("DropdownlistCategories");

if you just need to get the value then
string val = ((DropDownList)Page.Master.FindControl("DropdownlistCategories")).SelectedValue.ToString();
0
 

Author Comment

by:Denisdh
ID: 22676155
I'm looking at using a query string. But just before I do can anyone tell me if <%@ PreviousPageType VirtualPath="~/Right.master" %> is an option or some version there of?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 4

Expert Comment

by:novynov
ID: 22677471
Is it an option for doing what? The PreviousPageType is used for cross page posting. If you are redirecting to another page, PreviousPageType won't come to bear -as a redirect is just a GET.

However, if you are looking at a way to preserve the dropdownlist value between pages, then possibly you could do this, by causing a cross page post and then setting the dropdown's selected index on the postback.

However, I'm still not sure I understand what you're trying to accomplish well enough to say that this is the best approach.
0
 

Author Comment

by:Denisdh
ID: 22677596
http://www.hoctordesign.com has a dropdown on everypage. I want to have the page jobseekers/joblistings.aspx load when the value of the dropdown changes. I need the value to be passed so that I can then show only the job category value that was selected in GridView on that page.
0
 
LVL 4

Accepted Solution

by:
novynov earned 500 total points
ID: 22677931
OK. So, I haven't written any code to demo this yet...but it seems like you could do the following:

- Place your dropdown in the masterpage, like you've done
- In the selectedindexchanged event handler, write the selected index into a session variable and perform the redirect
- In the onload of the masterpage, look for a valid index in the session variable. If it exists, set the SelectedIndex of the dropdown to it.
- You'll also need to handle the case where they are already on the joblistings.aspx page and choose another category. Although you could just do the redirect to self once you've set the new val of the session var.


Although you could also do it with querystrings, doing it with a session variable would keep your url's clean. The only reason this one would not work would be if you needed to be Session free on your iis server.
 
Let me know if you need more help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
New to SOAP and need some direction 10 47
Two different visual studio versions 3 23
Export import database 4 44
Reset asp.net login password 4 34
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now