Solved

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

Posted on 2008-10-08
6
616 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

19 Experts available now in Live!

Get 1:1 Help Now