Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1705
Find control in a templated column
posted

Hey guys, 

 

I read somewhere that templated columns cannot be edited, so I dropped a textbox and a linkbutton right in the templated column in order to update the field:

<ig:TemplateDataField Key="URL" Header-Text="URL" >            

    <ItemTemplate >

        <a runat="server" id="URLLink" href='<%# Eval("URL")%>' >Link</a>

        <br />

        <asp:TextBox runat="server" ID="txtBxURL"></asp:TextBox> <asp:LinkButton runat="server" ID="lnkBttnChangeURL" Text="Update" CommandName="ChangeURL" CommandArgument='<%# Eval("RowID")%>'/>                    

    </ItemTemplate>                                               

</ig:TemplateDataField>

The problem is I can't find a way to get a reference to the TextBox control. I tried the following line of code within the OnItemCommand event (fired by the link button):

((TemplateDataField)this.WbDtGrdAppVersion.Columns["URL"]).ItemTemplate

but ItemTemplate doesn't seem to have a FindControl or some sort of collection of controls.

Any help would be appreciated.

 

Thanks !

 

  • 40
    Verified Answer
    posted

    In order to reference the TextBox control in a templated column you need to create a TextBox instance in your code behind and use the FindControl in the grid.items.  Below is a code sample.

    TextBox oTextBox = new TextBox();

    foreach (GridRecord oGridRecord in YourGridHere.Rows)
    {
         oTextBox = (TextBox)oGridRecord.Items[0].FindControl("txtBxURL");
    }

    This will give you access to the text box in your grid.

    • 1705
      posted in reply to Lee

      this worked!

      I just had to change the item index to match my column index (didn't know that's what items means)

       

      Thanks.

      • 5
        posted in reply to Ulises

        Thank you! This worked for me too!

        • 1705
          Verified Answer
          posted in reply to Ulises

          This is actually what I did:

          TextBox txtBxURL = WbDtGrdAppVersion.Rows.FromKey(new Object[] { e.CommandArgument }).Items[10].FindControl("txtBxURL") as TextBox;

           

          Instead of iterating through the collection, I accessed it with the row key.