Getting the value of a text field 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>  

Detailed view wrapped inside UpdatePanel.

When the user clicks on btnUpdateButton, I would like to get the value of the text field ( txtMessageTitle) in the code behind and use the CommandArgument button to update the corresponding MessageTitle in the database. How to get the value of a text field inside a DetailsView control from the Command event of my button? Thank.

+3
source share
1 answer

use the following:

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

All Articles