根据数据库设置DropDownList的SelectValue

时间:2016-07-19 15:34:12

标签: c# asp.net selectedvalue

我在GridView中有一个DropDownList,我希望所选值可以是该特定人员在数据库中的值

我的DropDownList的ASP代码:

<ion-icon name="my-custom-icon"></ion-icon>

我的ddlTeam_OnBound:

<asp:TemplateField HeaderText="Team" SortExpression="Team">
                <ItemTemplate>
                    <asp:DropDownList ID="ddlTeam" runat="server" 
                   DataSourceID="SqlDataSource1" DataTextField="Team" 
                        DataValueField="Team" ondatabound="ddlTeam_DataBound">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:connectionString %>" 
                        SelectCommand="SELECT DISTINCT [Team] FROM [Team_Names]"></asp:SqlDataSource>
                </ItemTemplate>
            </asp:TemplateField>

更新 - 没有错误但DDL为空:

protected void ddlTeam_DataBound(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            foreach (ListItem item in ddl.Items)
            {
                if (item.Text == "valor")
                {
                    item.Text = "Team Valor";
                }
                else if (item.Text == "mystic")
                {
                    item.Text = "Team Mystic";
                }
            }
        }

2 个答案:

答案 0 :(得分:0)

您没有执行SqlCommand或打开SqlConnection。您应该将输入放入参数以防止潜在的SQL注入攻击。

举个例子:

            string teamName = string.Empty;
            using (SqlConnection connection = new SqlConnection("your connection string"))
            {
                connection.Open();

                string query = "SELECT DISTINCT team_name FROM sec WHERE job = @job";
                SqlParameter param = new SqlParameter
                {
                    ParameterName = "@job",
                    Value = TextBox1.Text
                };

                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Parameters.Add(param);
                    SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow);

                    if (reader.Read())
                    {
                        teamName = reader.GetString(0);
                        // or
                        int ord = reader.GetOrdinal("team_name");
                        teamName = reader.GetString(ord); // Handles nulls and empty strings.
                    }
                }
            }

修改

您还必须正确设置下拉列表。

 DropDownList ddl = new DropDownList();
 ddl.DataSource = // call your database code - see above
 ddl.DataValueField = "ValueProperty";
 ddl.DataTextField = "TextProperty";
 ddl.DataBind();

 ddl.SelectedValue = teamName;

答案 1 :(得分:0)

SqlDataSource的查询中执行所有操作,并且在没有必要的情况下不使用代码。

<asp:TemplateField HeaderText="Team" SortExpression="Team">
    <ItemTemplate>
        <asp:DropDownList ID="ddlTeam" runat="server" 
       DataSourceID="SqlDataSource1" DataTextField="Team_txt" 
            DataValueField="Team"><%--No need  ondatabound="ddlTeam_DataBound"--%>
        </asp:DropDownList><%--datasourceId must match --%>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:connectionString %>" 
            SelectCommand="SELECT DISTINCT [Team],case Team when 'valor' then 'Team Valor' when 'mystic' then 'Team Mystic' else Team end team_txt FROM [Team_Names]"></asp:SqlDataSource>
    </ItemTemplate>
</asp:TemplateField>