问题描述
当前问题:我的DropDownList随DataTextField =COLUMNS_NAMEDataValueField =DATA_TYPE属性一起提供,DropDownList_SelectedIndexChanged()不会根据所选输入保留文本。但是它保留了项目列表中的第一个值
需要解决方案:如何根据DATA_TYPE属性保留所选的输入文本? 我尝试存储会话[DDLValue] = DropDownList.SelectedItem.Text ,但它始终保留来自索引中存在的相应DATA_TYPE的项目列表中的第一个值。
ie如果我从以下DropDownList输入中选择e,DropDownList中保留的值为d
保留e
COLUMN_NAME DATA_TYPE
/ pre>
a decimal
b decimal
c decimal
d int
e int
f varchar
g varchar
h varchar
i varchar
j varchar
Aspx代码:
< asp:DropDownList ID =DropDownList5runat =serverAutoPostBack =trueOnSelectedIndexChanged =DropDownList5_SelectedIndexChangedDataSourceID =MySqlDataSource>
< / asp:DropDownList>
< asp:SqlDataSource ID =MySqlDataSourcerunat =server>
< / asp:SqlDataSource>
C#代码:
protected void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
BindDropDownLists();
}
}
private void BindDropDownLists()
{
MySqlDataSource.ConnectionString = connection;
MySqlDataSource.SelectCommand =SELECT DATA_TYPE +'_'+ convert(varchar(10),ROW_NUMBER()OVER(ORDER BY DATA_TYPE))作为DATA_TYPE,COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE(TABLE_NAME ='RESULT'AND COLUMN_NAME IN('Column1','column2','Column3'));
DropDownList5.DataTextField =COLUMN_NAME;
DropDownList5.DataValueField =DATA_TYPE;
DropDownList5.DataBind();
}
解决方案,您的下拉列表如下所示,需要使用唯一的值字段进行正确选择
Current Issue: My DropDownList is provided with DataTextField="COLUMNS_NAME" DataValueField="DATA_TYPE" properties, the DropDownList_SelectedIndexChanged() does not retain the text based on the selected input. But it retains the first value from the list of items
Solution Required: How to Retain the selected input text based on the DATA_TYPE property ? I tried storing the Session["DDLValue"] = DropDownList.SelectedItem.Text but it always retains the first value from the list of items which satisfies the respective DATA_TYPE present in an Index.
i.e. if i choose "e" from The following DropDownList inputs the value retained in DropDownList is "d"
How to retain "e"
COLUMN_NAME DATA_TYPE a decimal b decimal c decimal d int e int f varchar g varchar h varchar i varchar j varchar
Aspx Code:
<asp:DropDownList ID="DropDownList5" runat="server" AutoPostBack="true" OnSelectedIndexChanged ="DropDownList5_SelectedIndexChanged" DataSourceID="MySqlDataSource"> </asp:DropDownList> <asp:SqlDataSource ID="MySqlDataSource" runat="server"> </asp:SqlDataSource>
C# code:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindDropDownLists(); } } private void BindDropDownLists() { MySqlDataSource.ConnectionString = connection; MySqlDataSource.SelectCommand = "SELECT DATA_TYPE + '_' + convert(varchar(10), ROW_NUMBER() OVER(ORDER BY DATA_TYPE ))as DATA_TYPE, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = 'RESULT' AND COLUMN_NAME IN ('Column1','column2','Column3'))"; DropDownList5.DataTextField = "COLUMN_NAME"; DropDownList5.DataValueField = "DATA_TYPE"; DropDownList5.DataBind(); }
解决方案After render , your drop down list is look like below , need to use unique value field for proper selection
这篇关于DropDownList与重复的数据值字段OnSelected得到困惑,并选择第一个为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!