如何在Post back和创建动态web控件期间保留DropDownList所选的输入文本
本文关键字:DropDownList 保留 文本 输入 控件 Post back web 动态 创建 | 更新日期: 2023-09-27 18:01:53
我能够基于DropDownList_SelectedValue生成一些动态web控件。
问题是,被选中的输入文本没有保留在主DropDownList中,但在回发后,它选择了与初始选择值的数据类型相似的索引列表中的第一个文本。
数据绑定DropDownList:
COLUMN_NAME DATA_TYPE
a decimal
b decimal
c decimal
d int
e int
f varchar
g varchar
h varchar
i varchar
j varchar
如果我在我的下拉列表中选择j,它会保留g作为我的DDL输入文本。如何在回发后保留j本身。
Aspx代码:
<asp:DropDownList ID="DropDownList5" runat="server" DataSourceID="column_list_for_filter" DataTextField="COLUMN_NAME" DataValueField="DATA_TYPE" OnSelectedIndexChanged ="DropDownList5_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
<asp:SqlDataSource ID="column_list_for_filter" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>" SelectCommand="SELECT COLUMN_NAME AS 'All_Columns', DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = 'RESULT')"></asp:SqlDataSource>
c#代码:private void BindDropDownLists()
{
foreach (Control ctl in DynamicPanel.Controls)
{
if (ctl is DropDownList)
{
(ctl as DropDownList).DataBind();
}
}
}
protected void Page_PreInit(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindDropDownLists();
}
else
{
createdynamiccontrols_decimal();
}
if (DropDownList5.SelectedValue == "decimal")
{
createdynamiccontrols_decimal();
}
if (DropDownList5.SelectedValue == "varchar")
{
createdynamiccontrols_varchar();
}
}
protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
{
//How to Proceed
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//How to Proceed
}
}
试试这个:
protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
{
Session["DDLValue"] = dropdownlist.SelectedValue;
}
在页面加载时,获取会话并将其设置为下拉列表,如果它不是NULL:
if(Session["DDLValue"] != null)
{
dropdownlist.SelectedValue = Session["DDLValue"].ToString();
}
希望有帮助!