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

时间:2008-10-05 15:53:36

标签: asp.net data-binding gridview

这是我在运行时绑定的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的位置。我不是在正确的活动中这样做的吗?如果没有,我需要把它放进去吗?

提前非常感谢...

5 个答案:

答案 0 :(得分:5)

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

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

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

答案 1 :(得分:2)

感谢Saurabh Tripathi,

您提供的解决方案为我工作。 在gridView_RowDataBound()事件中使用。

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

如果有人遇到同样的问题,那就试试吧。

干杯。

答案 2 :(得分: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"); 
   }
}

试试这个

这将有助于你

答案 3 :(得分: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上触发内置的编辑/删除/更新/取消操作。对于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 :(得分: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>
相关问题