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:
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:
public string MyPropertyOnMasterPage
{
get
{
return ddlLanguage.SelectedValue;
}
set
{
ddlLanguage.SelectedValue = value;
}
}
Then in your DropDownList SelectedChanged event, you can set the value like this:
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
:
<%@ MasterType VirtualPath ="~/TheVirtualPathToYourMasterPageFileHere" %>
Once you have that all set, you simply access the property
MyPropertyOnMasterPage
in your content page C# code like this:
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.
protected void Page_Load(object sender, EventArgs e) {
if(!IsPostBack){
}
}
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:
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();
}
}
}
}
}