简体   繁体   English

ASP.NET:DropDownList中的DataValueField

[英]ASP.NET: DataValueField in DropDownList

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: aspx页面具有以下代码:

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" />

The c# code to handle the button event is: 用于处理按钮事件的C#代码为:

protected void cmdNew_Click(object sender, EventArgs e)
    {
        sourceAccounts.SelectCommand = "SELECT code, id FROM accounts WHERE ID_user=" + codUsers.DataValueField;
        accUsers.Visible = true;
    }

The problem is that codUsers.DataValueField returns me the string "id" instead of the value of the index.... 问题是codUsers.DataValueField向我返回了字符串“ id”而不是索引值。

Use: 采用:

`codUsers.SelectedValue` instead of  `codUsers.DataValueField`

Or 要么

codUsers.SelectedItem.Value

Or 要么

codUsers.Items[codUsers.SelectedIndex].Value

Try 尝试

codUsers.SelectedValue;

This one should work for you. 这应该为您工作。

try using codUsers.SelectedValue; 尝试使用codUsers.SelectedValue;

sourceAccounts.SelectCommand = "SELECT code, id FROM accounts WHERE ID_user=" + 
codUsers.SelectedValue;

You are setting yourself up for possible SQL Injection tho. 您正在为可能的SQL注入设置自己。 (Maybe not in this particular case because you are getting a value that is pre-defined, but lets say this value would come from a textbox, you would be better of with parametrized queries: (在这种情况下可能不是这样,因为您得到的是预定义的值,但是可以说该值来自文本框,使用参数化查询会更好:

 string myVariable = someTextBox.Text;
 string sqlString = "Select something from someTable where someField=@someVariable";
 oCmd.Parameters.AddWithValue("@someVariable", myVariable);

It should be .SelectedValue Property. 它应该是.SelectedValue属性。

So the SelectCommand will be as: 因此,SelectCommand将为:

sourceAccounts.SelectCommand = "SELECT code, id FROM accounts WHERE ID_user=" + 
                                codUsers.SelectedValue;

DataValueField will be used at the time of Assignment of Value field to the DropDownList. 在将值字段分配给DropDownList时将使用DataValueField

and

DataTextField for Text Field. 文本字段的DataTextField

codUsers.DataValueField is the string id , which you set here: DataValueField="id" codUsers.DataValueField 在此处设置的字符串idDataValueField="id"

What you're looking for is codUsers.SelectedValue . 您正在寻找的是codUsers.SelectedValue

Please note also that using string concatenation for your SQL statements is a very bad idea. 另请注意,对您的SQL语句使用字符串连接是一个非常糟糕的主意。 A quick Google search for "ADO .NET Parameterized Queries" turned up an old tutorial on the subject . Google快速搜索“ ADO .NET参数化查询”,发现了有关该主题的旧教程 It's been a while since I've used plain ADO.NET for data access, but I'm sure there's still a lot of information out there. 自从我使用普通的ADO.NET进行数据访问以来已经有一段时间了,但是我敢肯定那里仍然有很多信息。

Basically you want to use the query parameter tools to build your query, not just string concatenation. 基本上,您想使用查询参数工具来构建查询,而不仅仅是字符串串联。 (Both for SQL injection reasons and for performance reasons, as well as just being cleaner code in general.) Though I recommend taking it a step further and using something like LINQ to SQL or Entity Framework for your data access, which parameterizes for you behind the scenes and simply gives you an object model to work with instead of direct database access. (出于SQL注入的原因和出于性能的原因,以及一般来说都是更简洁的代码。)尽管我建议更进一步,并使用诸如LINQ to SQL或Entity Framework之类的方法进行数据访问,但这在后面为您参数化场景,并简单地为您提供要使用的对象模型,而不是直接访问数据库。

Use Only 仅使用

codUsers.SelectedItem.Text

it will return as it is dropdownlist field data 它将作为下拉列表字段数据返回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM