问题
Two exceptions:
- Index out of bound
- FindControl returns null (it's pretending or not detecting the controls)
cs code: (for now dropdownlist just needs to be populated at editing mode)
protected void GridView3_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView3.EditIndex = e.NewEditIndex;
ShowData("a"); //bind data
GridViewRow gVR = GridView3.Rows[GridView3.EditIndex];
aspx code:
<asp:TemplateField HeaderText="x" ItemStyle-CssClass="ix">
<EditItemTemplate>
<asp:DropDownList ID="xnList" runat="server" Text='<%# Bind("[columnx]")%>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("[columnx]") %>'></asp:Label>
</ItemTemplate>
<ItemStyle CssClass="ix" />
</asp:TemplateField>
Given above snippet, right at the 3rd line I am getting following error. This is absurd as the same works well for other gridview and this gridview has 10 rows, so definitely not out of bound. What could be the issue here?
References:
- Finding a control in GridView in EDIT Mode returns null.
- FindControl in gridview returns null
EDIT:
Those who are generously trying and sparing their time to help me out with a solution, please check out Jeff Atwood's blog post about Page.FindControl. Reading it, I feel my dropdownlist is definitely a child within Gridview... Given this post, it comes much closer to what I have encountered.. But I am not 100% sure, if same case applies to what I am struggling with, since I have two gridviews. However only one has edit mode controls - the other is plain plain gridvos. Can someone show me the right direction?
EDIT: I have tried each one of above link's answers/solution. None working as of now.
回答1:
As many have pointed out the RowDataBound() is the correct event to hook data up for controls within gridview for edit, update or display modes. I was desperate and then tried out Row_Updating. HOwever that wasn't the issue with the error I was getting.
It was mainly due to the Text='<%# Bind("[columnx]")%>' of,
<asp:DropDownList ID="xnList" runat="server" Text='<%# Bind("[columnx]")%>'>
So the final solution is as per any of the answers posted out there.
cs:
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
DropDownList ddl = e.Row.FindControlRecursive("dhl") as DropDownList;
DropDownList stageDDL = e.Row.FindControlRecursive("dhl") as DropDownList;
stageDDL.DataSource = this.clservice.Getstuff("someparam");
stageDDL.DataTextField = "columnx";
stageDDL.DataValueField = "columnx";
stageDDL.DataBind();
}
}
}
aspx:
<asp:TemplateField HeaderText="x" ItemStyle-CssClass="ix">
<EditItemTemplate>
<asp:DropDownList ID="xnList" runat="server" DataTextField="columnx" DataValueField="columnx">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("[columnx]") %>'></asp:Label>
</ItemTemplate>
<ItemStyle CssClass="ix" />
</asp:TemplateField>
来源:https://stackoverflow.com/questions/24771214/why-isnt-the-dropdownlist-found-by-findcontrol-of-gridviewrow