locked
GridView Control - EmptyDataTemplate - FindControl RRS feed

  • Question

  • User1368056285 posted

    I was strugling to find a TextBox which resides in the EmtyDataTemplate of a Gridview within a FormView

    Finaly found a decent way to grab the control so I though I should share it because I didn't find the apropriate answer while searching the Internet

    • Case one: the easy one

    You have a TextBox in the EmtpyDataTemplate and a clickable Button

    <EmptyDataTemplate>

    <asp:TextBox ID="txtInfo" runat="server" />

    <asp:Button ID="btn" runat="server" onclick="btn_Click" Text="Test" />

    </EmptyDataTemplate>

    -- you can easily reference the TextBox in the button's click event

    protected void btn_Click(object sender, EventArgs e)

    {

    Button btn = (Button)sender;

    TextBox txtInfo = (TextBox)btn.NamingContainer.FindControl("txtInfo");

    }

    as simple as that

    Case two: the one that turned a little of my hairs grey, you don't have a control that fires an event you can trigger
     

    <EmptyDataTemplate>

    <asp:TextBox ID="txtInfo" runat="server" />

    </EmptyDataTemplate>

    The Gridview 'gvInternalInfo' resides in a FormView 'fvVerslag'

     

    here is the Gridview

     <?xml:namespace prefix = asp /><asp:GridView class=st id=""gvInternalInfo" DataKeyNames="EVXMIID" runat="server" AutoGenerateColumns="False"
                                                                DataSourceID="odsVerslagInternalInfo" SkinID="KanoGrid" ShowFooter="True" Caption="Interne Informatie">
                                                                <EMPTYDATATEMPLATE>
                                                                    <asp:TextBox class=st id=""txtInfo" runat="server" TextMode="MultiLine" Rows="5" Width="650px"></asp:TextBox>
                                                                    <asp:Button class=st id=""btn" runat="server" onclick="btn_Click" Text="Test">
                                                                </EMPTYDATATEMPLATE>
                                                                <COLUMNS>
                                                                    <asp:TemplateField class=st HeaderText=""Interne Informatie" SortExpression="INFO" ItemStyle-Wrap="true">
                                                                        <FOOTERTEMPLATE>
                                                                            <asp:TextBox class=st id=""txtInfo" runat="server" Text='<%# Bind("INFO") %>' Width="650px"
                                                                                TextMode="MultiLine" Rows="5"></asp:TextBox></FOOTERTEMPLATE>
                                                                        <ITEMTEMPLATE>
                                                                            <asp:Label class=st id=""Label1" runat="server" Text='<%# Bind("INFO") %>'></asp:Label></ITEMTEMPLATE>
                                                                        <ITEMSTYLE class=st Wrap=""True">
                                                                    </asp:TemplateField>
                                                                    <asp:TemplateField class=st HeaderText=""Wie" SortExpression="UPDATEDBY">
                                                                        <ITEMTEMPLATE>
                                                                            <asp:Label class=st id=""Label2" runat="server" Text='<%# Bind("UPDATEDBY") %>'></asp:Label></ITEMTEMPLATE>
                                                                    </asp:TemplateField>
                                                                    <asp:TemplateField class=st HeaderText=""Wanneer" SortExpression="DATEUPDATE">
                                                                        <ITEMTEMPLATE>
                                                                            <asp:Label class=st id=""Label3" runat="server" Text='<%# Bind("DATEUPDATE") %>'></asp:Label></ITEMTEMPLATE>
                                                                    </asp:TemplateField>
                                                                </COLUMNS>
                                                            </asp:GridView>
     <
    asp:GridView ID="gvInternalInfo" DataKeyNames="EVXMIID" runat="server" AutoGenerateColumns="False"

    DataSourceID="odsVerslagInternalInfo" SkinID="KanoGrid" ShowFooter="True" Caption="Interne Informatie">

    <EmptyDataTemplate>

    <asp:TextBox ID="txtInfo" runat="server" />

    </EmptyDataTemplate>

    <Columns>

    <asp:TemplateField HeaderText="Interne Informatie" SortExpression="INFO" ItemStyle-Wrap="true">

    <FooterTemplate>

    <asp:TextBox ID="txtInfo" runat="server" Text='<%# Bind("INFO") %>' Width="650px"

    TextMode="MultiLine" Rows="5"></asp:TextBox></FooterTemplate>

    <ItemTemplate>

    <asp:Label ID="Label1" runat="server" Text='<%# Bind("INFO") %>'></asp:Label></ItemTemplate>

    <ItemStyle Wrap="True" />

    </asp:TemplateField>

    <asp:TemplateField HeaderText="Wie" SortExpression="UPDATEDBY">

    <ItemTemplate>

    <asp:Label ID="Label2" runat="server" Text='<%# Bind("UPDATEDBY") %>'></asp:Label></ItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField HeaderText="Wanneer" SortExpression="DATEUPDATE">

    <ItemTemplate>

    <asp:Label ID="Label3" runat="server" Text='<%# Bind("DATEUPDATE") %>'></asp:Label></ItemTemplate>

    </asp:TemplateField>

    </Columns>

    </asp:GridView>

    Now I needed the text of the txtInfo TextBox in the ItemUpdating event of fvVerslag 

     

    protected void fvVerslag_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    
    {
    
    GridView gv = (GridView)fvVerslag.FindControl("gvInternalInfo");
    
    GridViewRow gvr ;
    
    
    
    if (gv.Rows.Count == 0)
    
    {
    
    Table tbl = (Table)gv.Controls[0];
    
    gvr = (GridViewRow)tbl.Controls[0];
    
    }
    
    else {
    // when there are rows returned you can reference the gridviewrow by grabbing the footerrow
    gvr = (GridViewRow)gv.FooterRow;
    
    }
    
    TableCell cell = gvr.Cells[0];
    
    TextBox txtInfo = (TextBox)cell.FindControl("txtInfo");
    
    }
    
    // jieha
     hope this helps for some of you

    </asp:Button>
    Friday, January 9, 2009 5:56 AM

All replies

  • User1727424460 posted

     Thanks Cohete x)

    Friday, January 9, 2009 7:10 AM
  • User2012684780 posted

    Cohete,

    Thanks for the helpful info.  It worked for me!

    BTW you can call FindControl on the GridViewRow directly instead of first referencing a cell.

    regards

     

    Jon

    Tuesday, May 5, 2009 9:17 PM