我使用以下方法将值分配给标签或文本框:首先我采用下拉列表,然后使用数据绑定,然后将下拉列表值分配给标签或文本框。
<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# 代码直接从数据库为标签或文本框赋值的方法
提前感谢
如果您的组合框已填充数据,则只需使用以下命令即可选择项目:
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();
当然,您需要将"文本"和"值"更改为数据库的列。