我正在尝试编写一个简单的按钮,当您单击它时,它会添加1并更新标签,但仅在第一次单击时起作用,此后它将不再更新标签。
protected void Page_Load(object sender, EventArgs e)
{ }
protected void ClickerButton_Click(object sender, ImageClickEventArgs e)
{
Lbl_PairsCooked.Text = CookSneaker(NumOfPairs).ToString();
}
public int CookSneaker(int num)
{
num += 1;
return num;
}
我制作的图像按钮仅在第一次单击时起作用...
答案 0 :(得分:0)
有关如何使其正常工作,请参见下面的代码段。最重要的是,您需要在IsPostBack上阅读链接 https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.page.ispostback?view=netframework-4.8了解区别。
public partial class _Default : Page
{
int NumOfPairs;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
//this line reads the text from label, converts and assigns to NumPairs. The UI is updated on the button click which takes this new NumOfPairs.
int.TryParse(Label1.Text, out NumOfPairs);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = CookSneaker(NumOfPairs).ToString();
}
public int CookSneaker(int num)
{
num += 1;
return num;
}
}