【问题标题】:How to set SelectedValue of DropDownList in GridView EditTemplate如何在 GridView EditTemplate 中设置 DropDownList 的 SelectedValue
【发布时间】:2011-03-29 08:16:06
【问题描述】:

如前所述,我正在尝试做this。我发现的唯一区别是上面代码中包含的附加列表项。

我尝试使用AppendDataBoundItems=true,但它仍然无法正常工作。我还想将其默认值设置为在 itemtemplate 的标签中显示的值,即 DropDownList 的 SelectedValue='<%# Eval("DepartmentName") %>' 但下拉列表中的属性对我不可用。 可能是什么原因。 ??

<EditItemTemplate>
    <asp:DropDownList ID="ddlDepartment_Edit" runat="server" 
        DataSourceID="dsDepartment_Edit" DataTextField="DepartmentName" 
        DataValueField="PK_DepartmentId">
    </asp:DropDownList>
    <asp:SqlDataSource ID="dsDepartment_Edit" runat="server" 
        ConnectionString="<%$ ConnectionStrings:BlackHillsConnect %>"  
        ProviderName="System.Data.SqlClient" SelectCommand="sp_GetDepartmentDropDown" 
        SelectCommandType="StoredProcedure">
    </asp:SqlDataSource>                                 
</EditItemTemplate>
<ItemTemplate>
    <asp:Label ID="lblDepartmentName" runat="server" Text='<%# Eval("DepartmentName") %>' >
    </asp:Label>
</ItemTemplate>   

我正在使用GridView

【问题讨论】:

    标签: c# asp.net data-binding sqldatasource


    【解决方案1】:

    DataValueField 似乎是错误的——不应该是DepartmentId 吗?同样,您需要有 SelectedValue='&lt;%# Eval("**DepartmentId**") %&gt;' - DepartmentName 将是 SeletectText

    【讨论】:

    • 这是我遇到麻烦的地方。没有这样的属性对我来说是可见的,即 SelectedValue 或 SelectedText,因此我可以为它们分配一些值。关于 DataValueField,是的,你是对的,它应该是主键。但在这两个属性可供我使用之前,我不会参与讨论。
    • VS Designer 可能不会在智能感知中向您显示该属性,但它就在那里。有两个属性 - SelectedValue 和 SelectedIndex。如果你写 SelectedIndex='' 会发生什么?
    • 我的错误 - 它应该是 SelectedValue = ''。
    【解决方案2】:

    在您的网格上有一个名为ItemCommand 的事件。为它创建一个方法:

    protected void Grid1_ItemCommand(object source, GridCommandEventArgs e)
    

    现在只需创建一个 case 语句,该语句将识别用户何时单击网格上的编辑按钮:

     case Grid.EditCommandName:     
    //set a member variable to the string of the cell you are editing.
    //something like: mString = e.item..["Column"].toString();                  
                       break;
    

    现在您已将成员变量设置为要在加载/预渲染下拉列表之前选择的字符串。对dropdownbox 使用事件OnPrerenderOnLoad,并将选定项设置为此字符串。

    【讨论】:

      【解决方案3】:

      使用GridView_DataBound 事件处理程序解决了这个问题。

      在您的情况下,您需要添加 HiddenField 来存储 PK_DepartmentId 值:

      <asp:GridView ID="gvExample" runat="server" AutoGenerateColumns="False" OnDataBound="gvExample_DataBound">
        <Columns>
          <asp:TemplateField HeaderText="Department">
            <EditItemTemplate>
              <asp:DropDownList ID="ddlDepartment_Edit" runat="server" DataSourceID="dsDepartment_Edit"
                DataTextField="DepartmentName" DataValueField="PK_DepartmentId">
              </asp:DropDownList>
              <asp:HiddenField ID="hfDepartmentId" runat="server" Value='<%# Bind("PK_DepartmentId") %>' />
              <asp:SqlDataSource ID="dsDepartment_Edit" runat="server" ConnectionString="<%$ ConnectionStrings:BlackHillsConnect %>"
                ProviderName="System.Data.SqlClient" SelectCommand="sp_GetDepartmentDropDown" SelectCommandType="StoredProcedure">
              </asp:SqlDataSource>
            </EditItemTemplate>
            <ItemTemplate>
              <asp:Label ID="lblDepartmentName" runat="server" Text='<%# Eval("DepartmentName") %>'>
              </asp:Label>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:CommandField ShowEditButton="True" ButtonType="Button" />
        </Columns>
      </asp:GridView>
      
      protected void gvExample_DataBound(object sender, EventArgs e)
      {
        foreach (GridViewRow gvRow in gvExample.Rows)
        {
          DropDownList ddlDepartment = gvRow.FindControl("ddlDepartment_Edit") as DropDownList;
          HiddenField hfDepartmentId = gvRow.FindControl("hfDepartmentId") as HiddenField;
      
          if (ddlDepartment != null && hfDepartmentId != null)
          {
            ddlDepartment.SelectedValue = hfDepartmentId.Value;
          }
        }
      }
      

      【讨论】:

        【解决方案4】:

        这是我找到的最好的......

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            foreach (GridViewRow gvRow in GridView1.Rows)
            {
                RadioButtonList rbl = gvRow.FindControl("rblPromptType") as RadioButtonList;
                HiddenField hf = gvRow.FindControl("hidPromptType") as HiddenField;
        
                if (rbl != null && hf != null)
                {
                    if (hf.Value != "")
                    {
                        //clear the default selection if there is one
                        rbl.ClearSelection();
                    }
        
                    rbl.SelectedValue = hf.Value;
                }
            }
        }
        

        【讨论】:

          【解决方案5】:

          为什么你们建议使用循环,当有一个 GridView 方法专门用于当行的条件发生变化时 - RowDataBound()

          protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
                  {
                      GridViewRow gvRow = (GridViewRow)e.Row;
                      HiddenField hfAgentID = (HiddenField)gvRow.FindControl("hfAgentID");
                      if (hfAgentID != null)
                      {
                          if (e.Row.RowType == DataControlRowType.DataRow)
                          {
                              DropDownList ddlAgent = (DropDownList)gvRow.FindControl("ddlAgent");
                              ddlAgent.SelectedValue = hfAgentID.Value;
                          }
                      }
                  }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-08-14
            • 2012-09-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-05-21
            • 1970-01-01
            • 2010-11-28
            相关资源
            最近更新 更多