2012-08-14 44 views
0

我使用以下方法为标签或文本框分配值: 首先,我采取dropdownlist然后我使用数据绑定,然后我分配dropdownlist值到标签或文本框。如何分配值从数据库标签或文本框在asp.net c#

<asp:DropDownList ID="DropDownList1" runat="server" 
     DataSourceID="A" DataTextField="Regisno" DataValueField="Regisno"> 
    </asp:DropDownList> 

<asp:AccessDataSource ID="A" runat="server" DataFile="~/App_Data/Database.mdb" 
      SelectCommand="SELECT [Regisno] FROM [tblsignupv] WHERE ([Email] = ?)"> 
      <SelectParameters> 
       <asp:SessionParameter Name="Email" SessionField="User" Type="String" /> 
      </SelectParameters> 
     </asp:AccessDataSource> 

我想用C#代码知道直接从数据库值分配给标签或文本框的方法

提前

回答

0

日Thnx如果你的组合框已经填充了数据,您可以选择项目只需使用:

DropDownList1.SelectedValue="123"; 

如果你的价值观来自数据库,则必须使用类似:

SqlCommand comm = new SqlCommand(); 
comm.Connection = new SqlConnection("Data Source=(local);Initial Catalog=[db];user=[user];password=[pwd];"); 
String sql = @"SELECT [Regisno] FROM [tblsignupv] WHERE ([Email] = '[emailaddress]')"; 
comm.CommandText = sql; 
comm.Connection.Open(); 
SqlDataReader cursor = comm.ExecuteReader(); 
while (cursor.Read()) 
{ 
    // add your option to the combobox here 
    customerID.Items.Add(new ListItem(cursor["text"].ToString(), cursor["value"].ToString())); 
} 
comm.Connection.Close(); 

当然,您需要将“文本”和“值”更改为数据库的列。

相关问题