更新时间:2023-12-05 07:55:34
try这一个....
try This one ....
Label lblfirst = new Label();
Label lblsecond = new Label();
lblfirst.Text="bind data from DB";
lblsecond.Text="bind data from DB";
您好,
您可以使用以下代码在表中动态添加尽可能多的标签:
Hi,
You can use following code to add as many labels as rows in your table dynamically:
protected void Page_Load(object sender, EventArgs e)
{
AddDynamicLabels();
}
private void AddDynamicLabels()
{
string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlConnection con = new SqlConnection(ConString);
string CmdString = "Select EmployeeID, FirstName, HireDate FROM Employees ORDER BY Salary DESC";
SqlCommand cmd = new SqlCommand(CmdString, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Label lbl = new Label();
lbl.Text = reader["FirstName"].ToString();
lbl.BackColor = System.Drawing.Color.Orange;
lbl.BorderStyle = BorderStyle.Solid;
lbl.BorderWidth = 1;
this.Controls.Add(lbl);
}
con.Close();
}
你好...
考虑标签ID为 LblOne 和SqlDataReader rd1,您可以简单地编写以下代码:
在设计页面中:
Hello ...
Considering the label id as LblOne and SqlDataReader rd1 you can simply code this :
in the design page :
<asp:Label ID="LblOne" runat="server" Text="" />
在后面的代码中:VB.Net代码
in the code behind : VB.Net code
LblOne.Text = String.Empty ' to clear the label
While rd1.read()
LblOne.Text += rd1("firldName").ToString
End While