Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello ,

i have a master page contain dropdownlist about Selecting Language

in masterPage pageLoad i write this Code

C#
//initial value of Session
    if (Session["DropLang_Value"] = null)
    {
        Session["DropLang_Value"] = 0.ToString();
        Session["DropLang_Text"] = English;.ToString();
    }



and Write this code in Event

C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
     Session["DropLang_Value"] = DropDownList1.SelectedItem.Value;
     Session["DropLang_Text"] = DropDownList1.SelectedItem.Text;
   }


i have another Order_Now.aspx that contain a dropdownlist and Get data from database depending on the selected item from dropdown list


Order Now.aspx Code


C#
public partial class Order_Now : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

SqlConnection con = new    SqlConnection(ConfigurationManager.ConnectionStrings["Myconecttion"].ConnectionString);
SqlCommand cmd = new SqlCommand("select*from ProductLoc where cultureid=@value", con);
cmd.Connection = con;
cmd.Parameters.AddWithValue("@value", Session["DropLang_Value"].ToString());
con.Open();
SqlDataReader r = cmd.ExecuteReader();
DropOrderType.DataTextField = "ProductName";
DropOrderType.DataValueField= "ProductId";
DropOrderType.DataSource = r;
DropOrderType.DataBind();
con.Close();      
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    }
}



After i make breakPoint and see what is the way that .Net Take to Execute my codes
i found that :

The problem is that when i go to Order_NOW.aspx and select different language from dropdownlist , the session variable still have the initial value (0) .
Select index changed update the value but After my page Execution :(
The problem is that ,the Session variable is updated after the Page_Load event
from the Master page and the Content pages is executed . :( :sigh: :confused:

So How can i solve this problem :-D
Posted
Updated 6-Sep-18 3:53am
v2
Comments
F-ES Sitecore 6-Sep-18 7:51am    
If you're only using the Session as a way of passing data from master to child page then you're better just having the child page read the dropdown value direct from the master page. You can do this using Master.FindControl or google how to use strongly typed master pages and you can put a property on your master page that is called "CurrentValue" or something, that returns the value of the dropdown.

Hercal wrote:
PS: I didn't mean to necro-post. I haven't seen the original posted date for this thread as it get pushed to the top list of the current list.


First off, you don't have to do 0.ToString() when assigning a default value to a string. You could simply do: string someAwesomeVariableName = "0";

Second, Content Page events trigger first before Master Page events that's why you are getting an unexpected results. I'd suggest you to read: Events in ASP.NET Master and Content Pages[^]

Third, I wouldn't recommend using Session to pass data between master and content pages as it can be hard to maintain it. There are two options that you can use to access master page's controls from child content page.

Option 1: Using FindControl method

For example, if you have a DropDownList control in your Master page with ID="ddlLanguage", then in your content page you can do:

C#
DropDownList ddl = (DropDownList)Page.Master.FindControl("ddlLanguage"); 


If you are working with Data Representation controls such as GridView, Repeater, etc.. then finding controls within master page can be a bit tricky especially if you don't know how the controls are being nested. You could try implementing a recursive FindControl method just like the one demonstrated here: Master Page and FindControl

Option 2: Using Properties

This option is my preferred as I find it easy to maintain and has less overhead compared to FindControl. What you just need to do is define a public property within your Master Page code behind file that holds the DropDownList value. For example:

C#
public string MyPropertyOnMasterPage  
{  
        get  
        {  
            // Get value of control on master page  
            return ddlLanguage.SelectedValue;  
        }  
        set  
        {  
            // Set new value for control on master page  
            ddlLanguage.SelectedValue = value;  
        }  
} 


Then in your DropDownList SelectedChanged event, you can set the value like this:

C#
protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e){
     MyPropertyOnMasterPage = ddlLanguage.SelectedItem.Value;
}


Now, in your content page markup (.ASPX), you need to define the MasterType directive defining VirtualPath:

ASP.NET
<%@ MasterType VirtualPath ="~/TheVirtualPathToYourMasterPageFileHere" %>


Once you have that all set, you simply access the property MyPropertyOnMasterPage in your content page C# code like this:

C#
protected void Page_Load(object sender, EventArgs e)  { 
     string selectedLanguage = Master.MyPropertyOnMasterPage;
}


Fourth, always wrap your code within !IsPostBack block when binding a control within Page Load event to avoid unexpected behavior across postbacks.

C#
protected void Page_Load(object sender, EventArgs e)  { 
     if(!IsPostBack){
        //your code here for binding the control
     }
}


Finally, make it a habit to put objects that eat resources such as SqlConnection, SqlCommand and SqlDataReader within a using statement to ensure that objects will be properly disposed and closed after they are used. Here's an updated version of your code:

C#
protected void Page_Load(object sender, EventArgs e)  { 
     if(!IsPostBack){
	   string sqlStatement = "select * from ProductLoc where cultureid= @value";
   	   string connectionString = ConfigurationManager.ConnectionStrings["Myconecttion"].ConnectionString;
        	using(SqlConnection connection = new SqlConnection(connectionString)){
           		using(SqlCommand cmd = new SqlCommand(sqlStatement ,connection)){
               	 	cmd.CommandType = CommandType.Text;
       	            	cmd.Parameters.AddWithValue("@value", Master.MyPropertyOnMasterPage);
           			using(SqlDataReader reader = cmd.ExecuteReader()){
					DropOrderType.DataTextField = "ProductName";
					DropOrderType.DataValueField= "ProductId";
					DropOrderType.DataSource = reader;
					DropOrderType.DataBind();
				}
        		}
        	}
     }
}
 
Share this answer
 
v3
Hi,

I think you need to consider your design.
Anyway you can get the selected drop down list from Request.Form ! althogh it is not a standard way but it may solve your problem.

Another way might be have a property in your masterpage to pass you dropdownlist object or selectedvalue. and then get the selected value from your order_now.aspx ( I am not sure about this solution but you can test it) some thing like this
cmd.Parameters.AddWithValue("@value", YourMaster.DDLProperty.SelectedValue.ToString());


just note that in c# you can get DDLProperty by using reflection something like this:
System.Reflection.FieldInfo oFI= this.Master.GetType().GetField("DDLPropertyName",System.Reflection.BindingFlags.Instance  | System.Reflection.BindingFlags.NonPublic );
DropDownList oDDL= (DropDownList)oFI.GetValue(this.Master);

cmd.Parameters.AddWithValue("@value", oDDL.SelectedValue.ToString());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900