ASP.NET:DataValueField中的DropDownList [英] ASP.NET: DataValueField in DropDownList
本文介绍了ASP.NET:DataValueField中的DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图创建两个的dropdownlist,第一个与用户的列表,并且仅当我点击OK按钮,显示占所述用户的列表中的第二一个可见。 ASPX页面具有以下code:
I'm trying to create two dropdownlist, the first one with a list of users, and the second one visible only when i click the OK button, showing a list of accounts for the user. The aspx page has the following code:
User:
<asp:SqlDataSource id="sourceUsers" runat="server" ConnectionString="<%$ ConnectionStrings:ct2012 %>"
SelectCommand="SELECT name, id FROM users" />
<asp:DropDownList id="codUsers" runat="server" DataSourceID="sourceUsers"
DataTextField="name" DataValueField="id" AutoPostBack="true" >
</asp:DropDownList>
<br />
Conto:
<asp:SqlDataSource id="sourceAccounts" runat="server" ConnectionString="<%$ ConnectionStrings:ct2012 %>" />
<asp:DropDownList id="accUsers" runat="server" DataSourceID="sourceAccounts"
DataTextField="code" DataValueField="id" Visible="false" >
</asp:DropDownList>
<br />
<asp:Button ID="btOK" text="OK" OnClick="cmdNew_Click" runat="server" />
在C#code处理按钮事件是:
The c# code to handle the button event is:
protected void cmdNew_Click(object sender, EventArgs e)
{
sourceAccounts.SelectCommand = "SELECT code, id FROM accounts WHERE ID_user=" + codUsers.DataValueField;
accUsers.Visible = true;
}
问题在于codUsers.DataValueField返回我的字符串ID,而不是指数值....
The problem is that codUsers.DataValueField returns me the string "id" instead of the value of the index....
推荐答案
尝试
codUsers.SelectedValue;
这应该为你工作。
这篇关于ASP.NET:DataValueField中的DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文