【发布时间】:2020-05-05 01:50:20
【问题描述】:
我在母版页上有一个下拉列表,我想在加载内容页面时在内容页面上传递选定的值。我的问题是,只有当我更改下拉列表中的值时,该值才会传递。因此,当页面加载时,我必须从下拉列表中重新选择以捕获下拉列表的值。如果我正在浏览内容页面,则所选值不会在页面加载时传递。 我的母版页代码.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);
}
【问题讨论】:
-
您在页面加载时的 DDL 中寻找什么价值?为什么要使用 Session 对象?您正在 DDL 中设置选定索引更改时选定值的值,这样它才会出现。我不知道你为什么要那样做。
-
您是否在页面标记中声明了母版页的确切类型?
-
您好,是的,我在标记上声明了 masterr 页面。我已设置选定的值以在用户浏览时保持选择。如果让您感到困惑,我可以发表评论。会话并不重要。我正在调用主控件而不是会话。我找到了这个链接,但答案对我没有多大帮助。 stackoverflow.com/questions/5933929/…
标签: c# asp.net master-pages