这就是我为按钮生成按钮和点击事件的方式。但是每次按下按钮都会点击它取最大值而不是点击按钮
int noofquestions = 100;
int k = 1;
for (int i = 1; i <= noofquestions / 5; i++)
{
HtmlTableRow tRow = new HtmlTableRow();
//TableRow tRow = new TableRow();
myTable.Rows.Add(tRow);
for (int j = 1; j <= 5; j++)
{
HtmlTableCell tCell = new HtmlTableCell();
tRow.Cells.Add(tCell);
Button link = new Button();
//LinkButton link = new LinkButton();
link.ID = "link" + k;
link.Text = k.ToString();
tCell.Controls.Add(link);
ViewState["qno"] = k;
link.Click += new EventHandler(link_Click);
tCell.Controls.Add(link);
//link.Click += new EventHandler(this.btn_click);
k = k + 1;
}
}
void link_Click(object sender, EventArgs e)
{
}
答案 0 :(得分:0)
取决于你如何看待价值。
我认为我的意思是最大值你在质疑当你阅读ViewState [&#34; qno&#34;]时它是最大值,因为它是它设置的那个。
取而代之的是尝试从其他一些财产中读取。
void link_Click(object sender, EventArgs e)
{
//If you do ViewState["qno"] it will be max value of K instead do following thing.
}
//执行以下更新
int noofquestions = 100;
int k = 1;
for (int i = 1; i <= noofquestions / 5; i++)
{
HtmlTableRow tRow = new HtmlTableRow();
//TableRow tRow = new TableRow();
myTable.Rows.Add(tRow);
for (int j = 1; j <= 5; j++)
{
HtmlTableCell tCell = new HtmlTableCell();
tRow.Cells.Add(tCell);
Button link = new Button();
//LinkButton link = new LinkButton();
link.ID = "link" + k;
link.Text = k.ToString();
tCell.Controls.Add(link);
ViewState[link.ID] = k;
link.Click += new EventHandler(link_Click);
tCell.Controls.Add(link);
//link.Click += new EventHandler(this.btn_click);
k = k + 1;
}
}
void link_Click(object sender, EventArgs e)
{
Button b = (sender)Button;
string value = ViewState[b.ID].ToString();
}