[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 642
  • Last Modified:

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

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
Denisdh
Asked:
Denisdh
  • 3
  • 2
1 Solution
 
novynovCommented:
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
 
Anurag ThakurTechnical ManagerCommented:
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
 
DenisdhAuthor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
novynovCommented:
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
 
DenisdhAuthor Commented:
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
 
novynovCommented:
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now