Search code examples
asp.net.net-3.5detailsviewnested-controls

Getting textbox value inside a Detailsview control


I have a DetailsView control with a template field as follows:

<asp:TemplateField SortExpression="Title">
  <ItemTemplate>
    <asp:TextBox ID="txtMessageTitle" Text='<%# Bind("Title") %>' runat="server">
    </asp:TextBox>
    <asp:Button ID="btnUpdateTitle" runat="server" Text="Update" 
      CommandName="UpdateTitle" CommandArgument='<%# Bind("MessageID") %>' oncommand="btnUpdateCommand"/>
  </ItemTemplate>
</asp:TemplateField>  

The Details View is wrapped inside an UpdatePanel.

When the user clicks on btnUpdateButton I'd like to be able to retrieve the textbox (txtMessageTitle) value in code behind and by using CommandArgument of the button, update the appropriate MessageTitle in database. How can I retrieve the textbox value inside the DetailsView control from the Command event of my button? Thanks.


Solution

  • use the following:

       TextBox txtMessageTitle = detailsViewName.FindControl("txtMessageTitle") as TextBox;
       string text = txtMessageTitle.Text;