1

I know the question is worded oddly, but I didn't know how else to say it.
I'm making an "Edit Customer" page in asp .net and want to enter in all the current values into the TextBoxes on PageLoad. Is there a way to do this using databinding, or do I have to get the Customer from the DataContext and then set the TextBoxes line-by-line? ie

firstNameTB.Text = Customer.FirstName;
lastNameTB.Text = Customer.LastName;
...
Marcus
  • 5,407
  • 3
  • 31
  • 54

2 Answers2

1

You could use a FormView and bind the data to the controls: MSDN:Data-Binding Expressions Overview

When the Update button for the row is clicked, the values of each control property bound using Bind syntax are extracted and passed to the data source control for the update operation.

<asp:FormView ID="FormView1"
      DataSourceID="SqlDataSource1"
      DataKeyNames="CustomerID"     
      RunAt="server">

<EditItemTemplate>
  <table>
    <tr>
      <td align=right>
        <b>Customer ID:</b>
      </td>
      <td>
        <%# Eval("CustomerID") %>
      </td>
    </tr>
    <tr>
      <td align=right>
        <b>First Name:</b>
      </td>
      <td>
        <asp:TextBox ID="EditFirstNameTextBox" RunAt="Server"
          Text='<%# Bind("FirstName") %>' />
      </td>
    </tr>
    <tr>
      <td align=right>
        <b>Last Name:</b>
      </td>
      <td>
        <asp:TextBox ID="EditLastNameTextBox" RunAt="Server"
            Text='<%# Bind("LastName") %>'  />
      </td>
    </tr>
    <tr>
      <td colspan="2">
        <asp:LinkButton ID="UpdateButton" RunAt="server"
          Text="Update" CommandName="Update" />
        &nbsp;
        <asp:LinkButton ID="CancelUpdateButton" RunAt="server"
          Text="Cancel" CommandName="Cancel" />
      </td>
    </tr>
  </table>
</EditItemTemplate>               
</asp:FormView>
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 1
    Actually you don't need a FormView for that. Just assign the databinding expression to an according property (e.g. Text property of a Label, or Text property or a TextBox) and be sure that the surrounding control somehow receives a .DataBind() in the lifecycle – citronas Jan 20 '11 at 22:54
  • Good to know. Actually i mentioned the FormView because the OP wants to make a 'Edit Customer' page. Therefore i assumed that he doesn't know the FormView/DetailsView-controls. – Tim Schmelter Jan 20 '11 at 23:05
0

Take a look at this question, which is very similar: Better way to populate form fields from SQL?

Community
  • 1
  • 1
Chris B. Behrens
  • 6,255
  • 8
  • 45
  • 71