C# 如何在多次单击按钮时更改asp.net内容页中的label.Text?
我在内容页中有一个标签,它放在母版页中。在多次单击按钮时,我希望在每次单击时更改标签文本。当我创建一个简单的页面而不将其嵌套在母版页中时,这种方式很好,但是当我在母版页中嵌套的内容页中使用这种方式时,多次单击标签文本不会改变 代码如下:C# 如何在多次单击按钮时更改asp.net内容页中的label.Text?,c#,asp.net,C#,Asp.net,我在内容页中有一个标签,它放在母版页中。在多次单击按钮时,我希望在每次单击时更改标签文本。当我创建一个简单的页面而不将其嵌套在母版页中时,这种方式很好,但是当我在母版页中嵌套的内容页中使用这种方式时,多次单击标签文本不会改变 代码如下: Label1.Text = " "+ (i++); (i是一个全局变量。) 这是一个大问题的模拟问题。实际上,我有一个大型应用程序,其中我从数据库中获取令牌编号并将其显示在标签上。单击“下一步”按钮时,将显示下一个令牌。当我不在母版页内的内容页占位符中添加内容
Label1.Text = " "+ (i++);
(i
是一个全局变量。)
这是一个大问题的模拟问题。实际上,我有一个大型应用程序,其中我从数据库中获取令牌编号并将其显示在标签上。单击“下一步”按钮时,将显示下一个令牌。当我不在母版页内的内容页占位符中添加内容页中的标签时,这样做很好。
这是页面加载代码:
protected void Page_Load(object sender, EventArgs e)
{
LS.Text = Request.QueryString["val"];
Label24.Visible = false;
cnn.ConnectionString = "server=*****;database=****;username=****;password=******;";
cnn.Open();
PopulateDataTable();
if (!IsPostBack)
{
Label1.Text = ""; // display nothing when page is loaded
Label2.Text = "";
Label3.Text = "";
Label4.Text = "";
HiddenField1.Value = "0";
nextButton.Visible = false;
adjButton.Visible = false;
}
if (Request.Form["HiddenField1"] != null)
rowIndex = Convert.ToInt16(Request.Form["HiddenField1"].ToString());
}
这是按钮单击事件的代码:
' protected void nextButton_Click(object sender, EventArgs e) // Next Button
{
try
{
string query2 = "";
cnn.Open();
MySqlCommand Cmd = new MySqlCommand();
Cmd.CommandText = "SELECT location from login where user_id='" + LS.Text + "';";
Cmd.Connection = cnn;
string location = Cmd.ExecuteScalar().ToString();
Cmd.CommandText = "SELECT counter from login where user_id='" + LS.Text + "' and location = '"+location+"';";
Cmd.Connection = cnn;
string counter = Cmd.ExecuteScalar().ToString();
cnn.Close();
if (counter == "1")
{
if (rowIndex == dt.Rows.Count)
{
nextButton.Enabled = false;
resetButton.Enabled = false;
adjButton.Enabled = false;
msgTxt.Visible = true;
msgTxt.Text = "The maximum no of tokens for today have been reached.";
Label1.ForeColor = System.Drawing.Color.Black;
}
else if ((dt.Rows.Count > 0) && (rowIndex < dt.Rows.Count))
{
Label1.Text = dt.Rows[rowIndex]["token_id"].ToString();
query2 = "Insert into " + location + "(counter1) value (" + Label1.Text + ") ;";
}
else
{
msgTxt.Text = "The maximum no of tokens for today have been reached.";
}
}
else if (counter == "2")
{
cnn.Open();
Cmd.CommandText = "SELECT count(*) from " + location + " where counter1 > -1;";
Cmd.Connection = cnn;
int count = Convert.ToInt32(Cmd.ExecuteScalar());
cnn.Close();
if (count > 0)
{
if (rowIndex == count)
{
nextButton.Enabled = false;
resetButton.Enabled = false;
adjButton.Enabled = false;
msgTxt.Visible = true;
msgTxt.Text = "No more requests pending.";
Label2.ForeColor = System.Drawing.Color.Black;
//Label1.Font.Strikeout = true;
}
else if (rowIndex < count)
{
Label1.Text = dt.Rows[rowIndex]["token_id"].ToString();
Label2.Text = dt.Rows[rowIndex]["token_id"].ToString();
query2 = "update " + location + " set counter2=" + Label2.Text + " WHERE counter1=" + Label2.Text + ";";
}
}
else
{
msgTxt.Text = "No more requests pending.";
}
}
else if (counter == "3")
{
cnn.Open();
Cmd.CommandText = "SELECT count(*) from " + location + " where counter2 > -1;";
Cmd.Connection = cnn;
int count = Convert.ToInt32(Cmd.ExecuteScalar());
cnn.Close();
if (count > 0)
{
if (rowIndex == count)
{
nextButton.Enabled = false;
resetButton.Enabled = false;
adjButton.Enabled = false;
msgTxt.Visible = true;
msgTxt.Text = "No more requests pending.";
Label3.ForeColor = System.Drawing.Color.Black;
//Label1.Font.Strikeout = true;
}
else if (rowIndex < count)
{
Label1.Text = dt.Rows[rowIndex]["token_id"].ToString();
Label2.Text = dt.Rows[rowIndex]["token_id"].ToString();
Label3.Text = dt.Rows[rowIndex]["token_id"].ToString();
query2 = "update " + location + " set counter3=" + Label3.Text + " WHERE counter2=" + Label3.Text + ";";
}
}
else
{
msgTxt.Text = "No more requests pending.";
}
}
else if (counter == "4")
{
cnn.Open();
Cmd.CommandText = "SELECT count(*) from " + location + " where counter3 > -1;";
Cmd.Connection = cnn;
int count = Convert.ToInt32(Cmd.ExecuteScalar());
cnn.Close();
if (count > 0)
{
if (rowIndex == count)
{
nextButton.Enabled = false;
resetButton.Enabled = false;
adjButton.Enabled = false;
msgTxt.Visible = true;
msgTxt.Text = "No more requests pending.";
Label4.ForeColor = System.Drawing.Color.Black;
//Label1.Font.Strikeout = true;
}
else if (rowIndex < count)
{
Label1.Text = dt.Rows[rowIndex]["token_id"].ToString();
Label2.Text = dt.Rows[rowIndex]["token_id"].ToString();
Label3.Text = dt.Rows[rowIndex]["token_id"].ToString();
Label4.Text = dt.Rows[rowIndex]["token_id"].ToString();
query2 = "update " + location + " set counter4=" + Label4.Text + " WHERE counter3=" + Label4.Text + ";";
}
}
else
{
msgTxt.Text = "No more requests pending.";
}
}
rowIndex++;
HiddenField1.Value = ""+rowIndex;
MySqlDataAdapter da2 = new MySqlDataAdapter(query2, cnn);
cnn.Open();
da2.SelectCommand.ExecuteNonQuery();
cnn.Close();
}
catch
{
cnn.Close();
}
}'
“受保护的无效下一个按钮\u单击(对象发送者,事件参数)//下一个按钮
{
尝试
{
字符串query2=“”;
cnn.Open();
MySqlCommand Cmd=新的MySqlCommand();
Cmd.CommandText=“从登录中选择位置,其中用户_id=”+LS.Text+“;”;
Cmd.Connection=cnn;
字符串位置=Cmd.ExecuteScalar().ToString();
Cmd.CommandText=“从登录名中选择计数器,其中用户id=”+LS.Text+”,位置=“+location+”;”;
Cmd.Connection=cnn;
字符串计数器=Cmd.ExecuteScalar().ToString();
cnn.Close();
如果(计数器=“1”)
{
if(rowIndex==dt.Rows.Count)
{
nextButton.Enabled=false;
resetButton.Enabled=false;
adjButton.Enabled=false;
msgTxt.Visible=true;
msgTxt.Text=“已达到今天令牌的最大数量。”;
Label1.ForeColor=System.Drawing.Color.Black;
}
else如果((dt.Rows.Count>0)和&(rowIndex-1;所在的“+位置+”中选择计数(*);
Cmd.Connection=cnn;
int count=Convert.ToInt32(Cmd.ExecuteScalar());
cnn.Close();
如果(计数>0)
{
如果(行索引==计数)
{
nextButton.Enabled=false;
resetButton.Enabled=false;
adjButton.Enabled=false;
msgTxt.Visible=true;
msgTxt.Text=“不再有未决请求。”;
Label2.ForeColor=System.Drawing.Color.Black;
//Label1.Font.Strikeout=true;
}
else if(行索引<计数)
{
Label1.Text=dt.Rows[rowIndex][“token_id”].ToString();
Label2.Text=dt.Rows[rowIndex][“token_id”].ToString();
query2=“更新”+location+“设置计数器2=“+Label2.Text+”,其中计数器1=“+Label2.Text+”;”;
}
}
其他的
{
msgTxt.Text=“不再有未决请求。”;
}
}
否则如果(计数器=“3”)
{
cnn.Open();
Cmd.CommandText=“从计数器2>-1;所在的“+位置+”中选择计数(*);
Cmd.Connection=cnn;
int count=Convert.ToInt32(Cmd.ExecuteScalar());
cnn.Close();
如果(计数>0)
{
如果(行索引==计数)
{
nextButton.Enabled=false;
resetButton.Enabled=false;
adjButton.Enabled=false;
msgTxt.Visible=true;
msgTxt.Text=“不再有未决请求。”;
Label3.ForeColor=System.Drawing.Color.Black;
//Label1.Font.Strikeout=true;
}
else if(行索引<计数)
{
Label1.Text=dt.Rows[rowIndex][“token_id”].ToString();
Label2.Text=dt.Rows[rowIndex][“token_id”].ToString();
Label3.Text=dt.Rows[rowIndex][“token_id”].ToString();
query2=“更新”+location+“设置计数器3=“+Label3.Text+”,其中计数器2=“+Label3.Text+”;”;
}
}
其他的
{
msgTxt.Text=“不再有未决请求。”;
}
}
否则如果(计数器=“4”)
{
cnn.Open();
Cmd.CommandText=“从“+位置+”中选择计数(*),其中计数器3>-1;”;
Cmd.Connection=cnn;
int count=Convert.ToInt32(Cmd.ExecuteScalar());
cnn.Close();
如果(计数>0)
{
如果(行索引==计数)
{
nextButton.Enabled=false;
resetButton.Enabled=false;
adjButton.Enabled=false;
msgTxt.Visible=true;
msgTxt.Text=“不再有请求
if (Request.Form["HiddenField1"] != null)
rowIndex = Convert.ToInt16(Request.Form["HiddenField1"].ToString());
if (Request.Form["HiddenField1"] != null)
{
rowIndex = Convert.ToInt16(Request.Form["HiddenField1"].ToString());
HiddenField1.Value = rowIndex;
}