【问题标题】:DropDownList.SelectedValue not taking effectDropDownList.SelectedValue 未生效
【发布时间】:2019-05-05 19:58:45
【问题描述】:

这似乎是一个重复的问题,但我认为不是。我在 ASPX 页面中设置我的asp:DropDownList,并在检查当前 gridview 行是否正在编辑后,在RowDataBound 事件期间将其设置为SelectedValue。设置控件的SelectedValue属性时没有报错,设置后立即检查,没有发生变化。

这是相关代码

            <asp:SqlDataSource
                ID="DataSourceAnswerGroups"
                runat="server"
                ConnectionString="<%$ ConnectionStrings %>"
                DataSourceMode="DataSet"
                SelectCommand="
                    SELECT
                        Id
                       ,Description
                    FROM dbo.table
                    WHERE Active = 1
                    ORDER BY Description
                    ;
                " />
            <asp:GridView
                ID="GridView"
                runat="server"
                OnRowDataBound="GridView_RowDataBound">
                <Columns>
                    <asp:TemplateField HeaderText="Answer Group" SortExpression="AnswerGroup" ItemStyle-Wrap="false" ItemStyle-CssClass="left">
                        <ItemTemplate>
                            <%# Eval("AnswerGroup") %>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList
                                ID="ddlAnswerGroupEdit"
                                runat="server"
                                DataSourceID="DataSourceAnswerGroups"
                                DataValueField="Id"
                                DataTextField="Description"/>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

后面的代码

    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex == GridView.EditIndex)
        {
            DropDownList ddlAnswerGroupEdit = (DropDownList)e.Row.FindControl("ddlAnswerGroupEdit");
            ddlAnswerGroupEdit.SelectedValue = GridView.DataKeys[e.Row.RowIndex].Values["AnswerGroupId"].ToString();
            Response.Write(GridView.DataKeys[e.Row.RowIndex].Values["AnswerGroupId"].ToString() + "<br/>"); // this outputs the correct guid
            Response.Write(ddlAnswerGroupEdit.SelectedValue + "<br/>"); // this always outputs the same guid, the one that represents the first item on the list.
        }
    }

在代码中的任何地方都没有对 DdataSource 或 DropDownList 进行其他操作。 默认选择应该会改变,但它会默默地失败。 任何帮助将不胜感激。

【问题讨论】:

    标签: c# asp.net .net dropdown selectedvalue


    【解决方案1】:

    你在哪里绑定 AnswerGroupId?

    替换它

    ddlAnswerGroupEdit.SelectedValue = GridView.DataKeys[e.Row.RowIndex].Values["AnswerGroupId"].ToString();
    

    ddlAnswerGroupEdit.SelectedValue = GridView.DataKeys[e.Row.RowIndex]["AnswerGroupId"].ToString();
    

    【讨论】:

    • 所有愚蠢的问题。这并没有解决它,但它确实让我朝着正确的方向前进。这是一个区分大小写的问题。 SQL Server 以大写形式返回 guid,而控制项以小写形式返回。这是我必须做的唯一修复:ddlAnswerGroupEdit.SelectedValue = GridView.DataKeys[e.Row.RowIndex].Values["AnswerGroupId"].ToString().ToLower();
    【解决方案2】:

    在所有愚蠢的问题中。这并没有解决它,但它确实让我朝着正确的方向前进。这是一个区分大小写的问题。 SQL Server 以大写形式返回 guid,而控制项以小写形式返回。这是我必须做的唯一修复:

    ddlAnswerGroupEdit.SelectedValue = GridView.DataKeys[e.Row.RowIndex].Values["AnswerGroupId"].ToString().ToLower();

    【讨论】:

      猜你喜欢
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 2021-04-30
      • 2021-10-03
      相关资源
      最近更新 更多