如何在EditItemTemplate字段中的GridView中绑定DropDownList?
下面是在运行时绑定一个GridView我的代码:如何在EditItemTemplate字段中的GridView中绑定DropDownList?
...
<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被填充的位置。我在正确的事件中没有这样做吗?如果没有,我需要把它放进去?
在此先感谢这么多...
好吧,我想我只是愚蠢。我想到了。
在RowDataBound事件,只需添加以下条件:
if (myGridView.EditIndex == e.Row.RowIndex)
{
//do work
}
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");
}
}
试试这个
这将帮助ü
我有同样的问题,但这个修复(贾森,这是添加条件处理程序)不适合我;编辑行从来没有数据绑定,所以这个条件从来没有评估为真。 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,当然,恰好行进入编辑模式,从而下拉databinds之前之前。所以一切正常。这有点破解,但我花了足够的时间试图弄清为什么编辑行已经不是数据绑定了。
感谢SAURABH帕蒂,
你为我提供工作的解决方案。 在gridView_RowDataBound()事件中使用。
if(gridView.EditIndex == e.Row.RowIndex && e.Row.RowType == DataControlRowType.DataRow)
{
// FindControl
// And populate it
}
如果有人遇到同样的问题,那就试试看。
干杯。
该代码将做你想要什么:
<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>
我想你也并不需要(e.Row.RowType == DataControlRowType.DataRow),除非你已经在某种程度上砍死编辑标题行。 – quillbreaker 2009-06-19 18:48:06