我在母版页上有一个下拉列表,我想在内容页面加载时在内容页面上传递选定的值。我的问题是该值仅在我更改下拉列表中的值时才通过。因此,当页面加载时,我必须从下拉列表中重新选择以捕获下拉列表的值。如果我正在浏览内容页面,则所选值不会在页面加载时传递。我的母版页代码.net:
<asp:DropDownList ID="ddlcategories"
runat="server" DataSourceID="SqlDataSourcecategories" DataTextField="CategoryName"
DataValueField="CategoryID" AutoPostBack="True"
onselectedindexchanged="ddlcategories_SelectedIndexChanged"></asp:DropDownList>
母版页cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlcategories.DataBind();
ddlcategories.Items.Insert(0, "Uncategorized");
ddlcategories.Items[0].Value = "0";
ddlcategories.SelectedValue = Convert.ToString(Session["lblCategoryID"]);
}
}
protected void ddlcategories_SelectedIndexChanged(object sender, EventArgs e)
{
Session["lblCategoryID"] = Convert.ToInt32(ddlcategories.SelectedValue);
}
内容页面 cs:
protected void Page_Load(object sender, EventArgs e)
{
Label10.Text = Convert.ToString(((DropDownList)Master.FindControl("ddlcategories")).SelectedValue);
}