开发者

Is there a way to bind TextBox default values as a group?

开发者 https://www.devze.com 2023-02-06 01:51 出处:网络
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

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;
...


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>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号