Getting textbox value inside a Detailsview control

Kamyar picture Kamyar · Mar 13, 2011 · Viewed 8.5k times · Source

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.

Answer

Sarzniak picture Sarzniak · Mar 13, 2011

use the following:

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