我在放置在母版頁內的內容頁面上有一個標籤。在點擊多個按鈕時,我想在每次點擊時更改標籤文本。當我製作一個簡單的頁面而不嵌套在母版頁中時,這種方式可以正常工作,但是當我在嵌套在母版頁中的內容頁中使用它時,標籤文本不會在多次點擊中更改。如何在asp.net內容頁面更改label.Text在多個按鈕點擊?
下面的代碼:
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();
}
}'
您可以發佈您的Page_Load方法和按鈕的Click事件代碼和你是如何設定初始值嗎? –
@rs。這是一個大問題的模型問題。其實我有一個很大的應用程序,我從數據庫中獲取令牌號並將它們顯示在標籤上。當單擊下一個按鈕時,將顯示下一個標記。當我不添加內容頁面中的標籤放置在母版頁內的內容頁面佔位符時,這工作正常。 我已經通過編輯問題添加了代碼,請看看 –
定義了哪個rowIndex?如果我將惡意SQL插入LS文本框會發生什麼?查看參數化查詢和SQL注入,*特別是*如果這是一個大型應用程序。不要太挑剔,但你應該把所有的數據訪問從你的UI中拿出來。 – ragerory