简体   繁体   English

如何用表格数据填充文本框?

[英]How to populate textboxes with table data?

I've got a table, I've got textboxes, let's make a connection! 我有一张桌子,我有文本框,让我们建立联系! I'm using ASP.NET and sql server2008; 我正在使用ASP.NET和sql server2008; I've got my connection string coded into the webconfig file already, but I'm hung up on how to populate specific textboxes with specific fields from my table. 我已经将连接字符串编码到webconfig文件中,但是我想知道如何用表中的特定字段填充特定的文本框。 More specifically, the user will submit the data on the home page, and then be given the chance to edit this data on the edit page. 更具体地说,用户将在主页上提交数据,然后有机会在编辑页面上编辑此数据。 I know, session would be the easiest way to do this, but my trainer specifically forbade it on the grounds that high traffic would cause problems with session objects, and commanded me to use a sql connection. 我知道,会话是执行此操作的最简单方法,但是我的培训师特别禁止这样做,因为高流量会导致会话对象出现问题,并命令我使用sql连接。 He's the boss. 他是老板。 So how do I get the data just inserted from the home page to display in editable textboxes on the edit page? 那么,如何获取刚从主页插入的数据以显示在编辑页面上的可编辑文本框中?

Take a look at this : Data-Binding 看一看: 数据绑定

You can use FormView to display the data stored on SQL Server. 您可以使用FormView来显示存储在SQL Server上的数据。

You have to define a SqlDataSource like this : 您必须像这样定义SqlDataSource:

 <asp:SqlDataSource
      id="SqlDataSource1"
      runat="server"
      DataSourceMode="DataReader"
      ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
      SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
  </asp:SqlDataSource>

Then you can use FormView like this : 然后,您可以像这样使用FormView:

<asp:FormView ID="FormView1" 
    DataSourceID="SqlDataSource1" 
    DataKeyNames="ProductID"     
    RunAt="server">
<ItemTemplate>
<table>
  <tr>
    <td align="right"><b>Product ID:</b></td>       
    <td><%# Eval("ProductID") %></td>
  </tr>
  <tr>
    <td align="right"><b>Product Name:</b></td>     
    <td><%# Eval("ProductName") %></td>
  </tr>
  <tr>
    <td align="right"><b>Category ID:</b></td>      
    <td><%# Eval("CategoryID") %></td>
  </tr>
  <tr>
    <td align="right"><b>Quantity Per Unit:</b></td>
    <td><%# Eval("QuantityPerUnit") %></td>
  </tr>
  <tr>
    <td align="right"><b>Unit Price:</b></td>       
    <td><%# Eval("UnitPrice") %></td>
  </tr>
</table>                 


Hope this help.... 希望能有所帮助。

您可以执行SELECT命令,然后使用以下代码将所有数据放入TextBox中:

YourTextBox.Text = yourDBValue;

Assuming that there is some kind of login system (ie you can at least identify your user) then the task is fairly straightforward. 假设存在某种登录系统(即,您至少可以标识您的用户),则任务非常简单。

When the user enters their data on the home page / data entry page, you validate the data server side, and assuming all is well, store this data in the database for the current user. 当用户在主页/数据输入页面上输入数据时,您将验证数据服务器端,并假设一切正常,请将该数据存储在当前用户的数据库中。 So, perhaps they are entering their address (as an example). 因此,也许他们正在输入自己的地址(例如)。 You might have something like this: 您可能会有这样的事情:

// NB: this is NOT code, just shorthand for a hypothetical database schema
Table: User
    Columns: User_ID, Email, Name

Table: Address
    Columns: User_ID (foreign key into User), Line1, Line2, ... PostCode, Country ... (you get the picture)

So, after they've entered the data, you store the details into this table, setting the User_ID to be the ID of the current user. 因此,在他们输入数据之后,您可以将详细信息存储到该表中,并将User_ID设置为当前用户的ID。

Then, on the edit page, you can just look up the existing address details for the current user and set the value of the text boxes on your page to be the various column values. 然后,在编辑页面上,您可以查找当前用户的现有地址详细信息,并将页面上文本框的值设置为各种列值。

Better still, you can make the edit page the initial entry page as well, where if it doesn't find existing data it leaves the text boxes empty, thus saving you writing 2 separate pages. 更好的是,您也可以将编辑页面设置为初始输入页面,如果找不到现有数据,则将文本框留空,从而节省了编写2个单独页面的时间。

If you don't have this kind of schema, where you have user accounts or whatever, but the data is user specific, you can still use a scheme like this, but instead of keying the Address table on a User_ID, you could use a value like a GUID which you assign to the user and store in their session. 如果您没有这种模式,拥有用户帐户或其他名称的数据,但是数据是特定于用户的,则仍然可以使用类似的方案,但是您可以使用键来代替在User_ID上键入Address表值,例如您分配给用户并存储在其会话中的GUID。 This would allow you to track a user for the duration of thier session at least. 这样一来,您至少可以在会话期间跟踪用户。 To extend this you could write this value to a cookie, remembering to set the expiration date, which would allow you to track them for several sessions, until the cookie expires, or they change browser etc. 要扩展此范围,您可以将此值写入cookie,记住设置过期日期,这样可以跟踪多个会话,直到cookie过期或更改浏览器等。

Table: Address
    Columns: Session_Guid, Line1, Line2, ... PostCode, Country ...

This is the basis of most websites with a login, they use a unique value in a cookie to track a users. 这是大多数具有登录名的网站的基础,它们使用cookie中的唯一值来跟踪用户。

You're asking how to take a textbox, enter some data, and then submit that to a database? 您在问如何获取文本框,输入一些数据,然后将其提交到数据库?

So Insert statements? 那么插入语句? I would use LINQ, but I'm not sure you are using it. 我会使用LINQ,但是我不确定您是否正在使用它。 Or prepare a Stored Procedure which handles the insert. 或准备一个处理插入内容的存储过程。 However, in the most simple form: 但是,以最简单的形式:

 try {
     using ( SqlConnection conn = new SqlConnection(cstr)) {
       SqlParameter name = cmd.Parameters.Add( "@name", SqlDbType.NVarChar, 15 );
       name.Value = TextBox1.Text;
       string insertString = @"insert into Table(columnName) values (@name)";

       SqlCommand cmd = new SqlCommand(insertString, conn);
       cmd.ExecuteNonQuery();
     }
} catch (Exception ex) {
  //do some logging
} finally {
  conn.Close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM