如何在EditItemTemplate字段中绑定GridView中的DropDownList?

8

以下是我的代码,它将在运行时绑定到网格视图中:

...
<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);
    }
}
< p > 在这里, BindOpponentDD() 只是DropDownList被填充的地方。我没有在正确的事件中执行吗?如果不是,我需要把它放在哪里?

非常感谢你提前...

5个回答

6

好的,我猜我只是太蠢了。我想通了。

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

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

我认为你也不需要 (e.Row.RowType == DataControlRowType.DataRow),除非你以某种方式破解了可编辑的标题行。 - quillbreaker
不,你需要这样做,因为它从标题行开始,然后逐行进行。如果你不这样做,就会出现错误,或者它根本无法正常工作。 - Jason
或者您可以通过以下方式检查行是否处于编辑模式:if(e.RowState && DataControlRowState.Edit) > 0 {} - Emad Mokhtar

2

感谢Saurabh Tripathi,

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

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

如果有人遇到同样的问题,可以尝试以下方法。祝好运。

1

我也遇到了同样的问题,但是这个解决方法(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上内置的编辑/删除/更新/取消操作。对于编辑按钮,我将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事件发生在行进入编辑模式之前,因此在下拉列表数据绑定之前。所以一切正常。这有点像是一个hack,但我已经花了足够的时间来尝试弄清楚为什么编辑行还没有被数据绑定。


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

试试这个

这会对你有帮助的


0

这段代码将会实现你想要的功能:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接