[英]I want to view it in datagrid which the input will be added each time the user clicks the button
[英]Limit Button Clicks Based on User Input
我有一個頁面,其中用於添加管理員房間,管理員將選擇他要添加的房間數量,然后繼續命名房間ID,問題是給定數量的按鈕后我的按鈕將無法禁用。 如果管理員要添加5個房間,則只能單擊5次該按鈕以添加房間,然后單擊5次后應禁用該按鈕。 這是我的示例代碼。
int count = 0; // <-- Global Variable
protected void addBtn_Click(object sender, EventArgs e)
{
int qty = Convert.ToInt32(qtyDDL.SelectedValue); // <--number of rooms admin wants to add
count++;
roomtypeDDL.Enabled = false;
qtyDDL.Enabled = false;
if(count < qty)
{
string roomid = roomidBox.Text;
string rtype = roomtypeDDL.SelectedItem.ToString();
}
else
{
roomidBox.Enabled = false;
roomtypeDDL.Enabled = true;
addBtn.Enabled = false;
addBtn.BackColor = System.Drawing.ColorTranslator.FromHtml("#2C2A2A");
}
}
您應該使用會話,因為在asp.net中,每個請求的公共變量都將返回初始值
protected void addBtn_Click(object sender, EventArgs e)
{
if (Session["Count"]==null)
{
Session["Count"] = 0;
}
count = int.Parse(Session["Count"]);
int qty = Convert.ToInt32(qtyDDL.SelectedValue); // <--number of rooms admin wants to add
count++;
roomtypeDDL.Enabled = false;
qtyDDL.Enabled = false;
if (count < qty)
{
string roomid = roomidBox.Text;
string rtype = roomtypeDDL.SelectedItem.ToString();
}
else
{
roomidBox.Enabled = false;
roomtypeDDL.Enabled = true;
addBtn.Enabled = false;
addBtn.BackColor = System.Drawing.ColorTranslator.FromHtml("#2C2A2A");
}
Session["Count"] = count;
}
protected void qtyDDL_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (qtyDDL.SelectedIndex == 0)
{
//My Code
}
else
{
Session["Count"] = 0; // <-- sets count to 0 every click on quantity
}
}
catch(Exception ex)
{
Label1.Text = ex.Message;
}
}
protected void addBtn_Click(object sender, EventArgs e)
{
count = Convert.ToInt32(Session["Count"]); // <-- set variable
roomtypeDDL.Enabled = false;
qtyDDL.Enabled = false;
string rtype, roomid;
if(count < Convert.ToInt32(Session["qty"]))
{
string roomid = roomidBox.Text;
string rtype = roomtypeDDL.SelectedItem.ToString();
}
count++;
if (count == Convert.ToInt32(Session["qty"])) // <--Disable Add Button
{
roomidBox.Enabled = false;
roomtypeDDL.Enabled = true;
roomtypeDDL.SelectedIndex = 0;
qtyDDL.SelectedIndex = 0;
addBtn.Enabled = false;
addBtn.BackColor = System.Drawing.ColorTranslator.FromHtml("#2C2A2A");
}
Session["Count"] = count;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.