Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Two exceptions:

  1. Index out of bound
  2. 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:

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
164 views
Welcome To Ask or Share your Answers For Others

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...