【问题标题】:Set DropDownList's SelectValue based on Database根据数据库设置 DropDownList 的 SelectValue
【发布时间】:2016-07-19 15:34:12
【问题描述】:

我在 GridView 中有一个 DropDownList,我希望选择的值是该特定人在数据库中的任何值

我的 DropDownList 的 ASP 代码:

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

我的 ddlTeam_OnBound:

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";
                }
            }
        }

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

    DropDownList ddl = new DropDownList();
                string query2 = "SELECT team_name FROM sec WHERE job = " + TextBox1.Text;
                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    using (SqlCommand cmd = new SqlCommand(query2, con))
                    {
                        con.Open();
                        using (SqlDataReader read = cmd.ExecuteReader())
                        {
                        while(read.Read())
                        {
                            ddl.SelectedValue = read["team_name"].ToString();
                        }
                        }
                        con.Close();
                    }
                }

【问题讨论】:

    标签: c# asp.net selectedvalue


    【解决方案1】:

    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>
    

    【讨论】:

    • 这个问题是我需要显示整个团队名称列表,其中 selectedvalue 是与数据库中该人关联的当前值
    • 好的。将ddlTeam_DataBound 返回到代码并在其中设置.SelectedValue。但仅此而已。
    • 我无法在 DataBound 方法中执行此操作,因为它依赖于 TextBox 条目。我所做的是将上面更新部分中的代码放在 TextChanged 方法中,但它不起作用
    • 在 OP 中,ddl 源不依赖于任何参数。您还可以在 GridView_ItemDataBound 处理程序中设置 SelectedValue。 DDL 为空,因为您设置了 DataSourceID=srs1 并且 sqldatasource 具有 id=src2
    【解决方案2】:

    您没有执行 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;
    

    【讨论】:

    • 对不起,我有所有这些 - 为了简单起见,我只是省略了 - 我的问题是我从一个表中提取 ddl 项,需要从另一个表中获取初始选定值。
    • 好的,你原来的问题有点混乱。那么您的问题是要根据数据库中的另一个 DropDownList 选定值填充一个 DropDownList 吗?
    • 不,我正在从一个表(team_names)填充列表,但我希望初始 selectedvalue 来自另一个表(秒)。
    • 好的,所以进行数据库调用以获取您想要的值并设置下拉列表的初始值。
    • 更新了我的答案 - 仍然无法正常工作。有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 2020-08-08
    • 2017-08-22
    • 1970-01-01
    相关资源
    最近更新 更多