如何通过多次点击按钮更改asp.net内容页面中的label.Text?

时间:2015-06-03 19:00:20

标签: c# asp.net

我在内容页面中有一个标签放在母版页面内。点击多次按钮,我想在每次点击时更改标签文字。当我创建一个简单的页面而不将其嵌套在母版页中时,这很好用,但是当我在嵌套在母版页中的内容页面中使用它时,标签文本在多次点击时不会改变。

以下是代码:

Label1.Text = " "+ (i++);

i是一个全局变量。)

这是一个大问题的模型问题。实际上我有一个大型应用程序,我从数据库中获取令牌号并在标签上显示它们。单击下一个按钮时,将显示下一个标记。当我不在主页中的内容页面占位符中放置的内容页面中添加标签时,哪个工作正常。 继承人PageLoad代码:

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();
        }
    }'

1 个答案:

答案 0 :(得分:0)

ssh在哪里递增?您将HiddenFieldValue1.Value设置为等于此值,但我看不到它增加的位置,因此它看起来会保持不变。

rowIndex

应该是:

if (Request.Form["HiddenField1"] != null)
    rowIndex = Convert.ToInt16(Request.Form["HiddenField1"].ToString());