Textbox counter with another textbox counter [closed]
I have two textboxes "SerialNumber" and "SerialExtension". SerialNumber is auto generated starting with 1, which increases each time I click the "Add" button. The SerialExtension textbox has to have A,B, C,etc, concatenated with the SerialNumber value dispalyed each time I click the "Display" button. For that I gave a char counter. But each time the Seri开发者_运维知识库alNumber increases, my char counter does not reset from 'A'. Can anyone help me out? Code in C# please.
private static int counter = 1;
static char a = 'A';
protected void Page_Load(object sender, EventArgs e)
{
txtSerialNo.Text = counter.ToString("0");
}
protected void btnAdd_Click(object sender, ImageClickEventArgs e)
{
counter++;
txtSerialNo.Text = counter.ToString("0");
}
protected void btnDisplay_Click(object sender, ImageClickEventArgs e)
{
txtSerialextension.Text = txtSerialNo.Text + a.ToString();
a++;
}
How to assign values in C#:
= Operator, works for char
as well.
The reason that a
does not start over with the value 'A'
is that you will need to reassign that to a
every time that it should be reset; there is no way for the program code to guess when this should happen. In your case, you want that to happen when you click btnAdd
, so you need to add code for this in the click event handler for that button:
protected void btnAdd_Click(object sender, ImageClickEventArgs e)
{
a = 'A'; // reset a to the value 'A'
counter++;
txtSerialNo.Text = counter.ToString("0");
}
精彩评论