我有一个页面,用于为管理员添加房间,管理员将选择他想要添加的房间数,然后继续命名房间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");
}
}
答案 0 :(得分:0)
你应该使用session,因为在asp.net中,public var将返回每个请求的初始值
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;
}
答案 1 :(得分:0)
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;
}