【问题标题】:How do you bind a DropDownList in a GridView in the EditItemTemplate Field?如何在 EditItemTemplate 字段的 GridView 中绑定 DropDownList?
【发布时间】:2010-09-15 09:21:40
【问题描述】:

这是我在运行时绑定的 gridview 中的代码:

...
<asp:templatefield>
    <edititemtemplate>
        <asp:dropdownlist runat="server" id="ddgvOpp" />
    </edititemtemplate>
    <itemtemplate>
        <%# Eval("opponent.name") %>
    </itemtemplate>
</asp:templatefield>
...

我想绑定下拉列表“ddgvOpp”,但我不知道如何绑定。我应该,但我没有。这是我所拥有的,但我不断收到“对象引用”错误,这是有道理的:

protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) //skip header row
    {
        DropDownList ddOpp = (DropDownList)e.Row.Cells[5].FindControl("ddgvOpp");
        BindOpponentDD(ddOpp);
    }
}

BindOpponentDD() 就是填充 DropDownList 的位置。我不是在正确的事件中这样做吗?如果没有,我需要把它放在哪个?

提前非常感谢...

【问题讨论】:

    标签: asp.net data-binding gridview


    【解决方案1】:

    好吧,我想我只是愚蠢。我想通了。

    在 RowDataBound 事件中,只需添加以下条件:

    if (myGridView.EditIndex == e.Row.RowIndex)
    {
         //do work
    }
    

    【讨论】:

    • 我认为你也不需要 (e.Row.RowType == DataControlRowType.DataRow),除非你以某种方式破解了可编辑的标题行。
    • 不,因为它从标题行开始并向下工作。如果你不这样做,你会得到一个错误,或者它只是不起作用。
    • 或者你可以通过if(e.RowState &amp;&amp; DataControlRowState.Edit) &gt; 0 {}检查行是否处于编辑模式
    【解决方案2】:
    protected void grdDevelopment_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       if (grdDevelopment.EditIndex == e.Row.RowIndex && e.Row.RowType==DataControlRowType.DataRow) 
       {       
           DropDownList drpBuildServers = (DropDownList)e.Row.Cells[0].FindControl("ddlBuildServers"); 
       }
    }
    

    试试这个

    这对你有帮助

    【讨论】:

      【解决方案3】:

      我也遇到了同样的问题,但是这个修复(Jason 的,将条件添加到处理程序)对我不起作用;编辑行从未被数据绑定,因此该条件从未评估为真。 RowDataBound 根本不会使用与 GridView.EditIndex 相同的 RowIndex 调用。不过,我的设置有点不同,因为我没有以编程方式绑定下拉列表,而是将其绑定到页面上的 ObjectDataSource。但是,下拉列表仍然必须每行单独绑定,因为它的可能值取决于行中的其他信息。所以 ObjectDataSource 有一个 SessionParameter,我确保在需要绑定时设置适当的会话变量。

      <asp:ObjectDataSource ID="objInfo" runat="server" SelectMethod="GetData" TypeName="MyTypeName">
      <SelectParameters>
          <asp:SessionParameter Name="MyID" SessionField="MID" Type="Int32" />
      </SelectParameters>
      

      以及相关行中的下拉菜单:

      <asp:TemplateField HeaderText="My Info" SortExpression="MyInfo">
              <EditItemTemplate>
                  <asp:DropDownList ID="ddlEditMyInfo" runat="server" DataSourceID="objInfo" DataTextField="MyInfo" DataValueField="MyInfoID" SelectedValue='<%#Bind("ID") %>' />
              </EditItemTemplate>
              <ItemTemplate>
                  <span><%#Eval("MyInfo") %></span>
              </ItemTemplate>
          </asp:TemplateField>
      

      我最终没有使用 GridView 中的 CommandField 来生成我的编辑、删除、更新和取消按钮;我使用 TemplateField 自行完成,通过适当设置 CommandNames,我能够触发 GridView 上的内置编辑/删除/更新/取消操作。对于 Edit 按钮,我将 CommandArgument 设置为绑定下拉列表所需的信息,而不是像通常那样使用行的 PK。幸运的是,这并没有阻止 GridView 编辑相应的行。

      <asp:TemplateField>
              <ItemTemplate>
                  <asp:ImageButton ID="ibtnDelete" runat="server" ImageUrl="~/images/delete.gif" AlternateText="Delete" CommandArgument='<%#Eval("UniqueID") %>' CommandName="Delete" />
                  <asp:ImageButton ID="ibtnEdit" runat="server" ImageUrl="~/images/edit.gif" AlternateText="Edit" CommandArgument='<%#Eval("MyID") %>' CommandName="Edit" />
              </ItemTemplate>
              <EditItemTemplate>
                  <asp:ImageButton ID="ibtnUpdate" runat="server" ImageUrl="~/images/update.gif" AlternateText="Update" CommandArgument='<%#Eval("UniqueID") %>' CommandName="Update" />
                  <asp:ImageButton ID="ibtnCancel" runat="server" ImageUrl="~/images/cancel.gif" AlternateText="Cancel" CommandName="Cancel" />
              </EditItemTemplate>
          </asp:TemplateField>
      

      在 RowCommand 处理程序中:

      void grdOverrides_RowCommand(object sender, GridViewCommandEventArgs e)
              {
                  if (e.CommandName == "Edit")
                      Session["MID"] = Int32.Parse(e.CommandArgument.ToString());
              }
      

      当然,RowCommand 发生在行进入编辑模式之前,因此在下拉数据绑定之前发生。所以一切正常。这有点小技巧,但我已经花了足够的时间试图弄清楚为什么编辑行还没有被数据绑定。

      【讨论】:

        【解决方案4】:

        感谢 Saurabh Tripathi,

        您提供的解决方案对我有用。 在 gridView_RowDataBound() 事件中使用。

        if(gridView.EditIndex == e.Row.RowIndex && e.Row.RowType == DataControlRowType.DataRow)
        {
            // FindControl
            // And populate it
        }
        

        如果有人遇到同样的问题,请尝试一下。

        干杯。

        【讨论】:

          【解决方案5】:

          这段代码会做你想做的事:

          <asp:TemplateField HeaderText="garantia" SortExpression="garantia">
           <EditItemTemplate>
             <asp:DropDownList ID="ddgvOpp" runat="server" SelectedValue='<%# Bind("opponent.name") %>'>
                 <asp:ListItem Text="Si"  Value="True"></asp:ListItem>
                 <asp:ListItem Text="No" Value="False"></asp:ListItem>
             </asp:DropDownList>
           </EditItemTemplate>
           <ItemTemplate>
             <asp:Label ID="Label1" runat="server" Text='<%# Bind("opponent.name") %>'></asp:Label>
           </ItemTemplate>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-05-21
            • 1970-01-01
            • 2016-12-03
            • 1970-01-01
            相关资源
            最近更新 更多