Solved

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

Posted on 2008-10-08
6
620 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hey Audio! 3 29
tutorial for ebay api 3 34
Convert datetime to time string 10 21
Powershell File Sort 8 29
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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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