我正在使用SQL查询动态创建按钮:
private void createPagingButtons(DateTime firstDayofWeek, DateTime lastDayofWeek)
{
int i = 1;
//get query that holds all of the names for a date range
SqlDataReader returnedQuery = getDefaultUser(firstDayofWeek, lastDayofWeek);
while (returnedQuery.Read())
{
string buttonName = returnedQuery["Person"].ToString();
string[] splitString = buttonName.Split('(');
Button btn = new Button();
btn.ID = buttonName;
btn.Click += new EventHandler(btn_Click);
btn.Text = splitString[0];
btn.Width = Convert.ToInt32(splitString[0].Length)*9;
btn.CssClass = "dynamicButtons";
pagingPanel.Controls.Add(btn);
i++;
}
}
因此,我没有在ASP.NET端静态的特定名称。在postback上我想button.focus()被点击的那个。
我如何实现这一目标?
答案 0 :(得分:0)
在btn_Click()
中,设置页面级变量(例如this.clickedButtonId
)到ID,然后在createPagingButtons()
调用btn.Focus()
,如果btn.ID==clickedButtonId
:< / p>
string clickedButtonId;
private void createPagingButtons(DateTime firstDayofWeek, DateTime lastDayofWeek)
{
int i = 1;
SqlDataReader returnedQuery = getDefaultUser(firstDayofWeek, lastDayofWeek);
while (returnedQuery.Read())
{
string buttonName = returnedQuery["Person"].ToString();
Button btn = new Button();
btn.ID = buttonName;
btn.Click += new EventHandler(btn_Click);
//...
pagingPanel.Controls.Add(btn);
if (btn.ID==this.clickedButtonId) btn.Focus();
i++;
}
}
private void btn_Click(object s, EventArgs e)
{
this.clickedButtonId = ((Button) s).ID;
}