[英]Focus() on dynamically created server control
I am creating buttons dynamically using a SQL query: 我正在使用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++;
}
}
Because of this, I don't have specific names for them that are static on the ASP.NET side. 因此,我在ASP.NET方面没有静态的特定名称。 On postback I would like to button.focus() the one that was clicked. 在回发时,我想要button.focus()被点击的那个。
How do I achieve this? 我该如何实现?
In btn_Click()
, set a page-level variable (eg, this.clickedButtonId
) to the ID and then in createPagingButtons()
call btn.Focus()
if btn.ID==clickedButtonId
: 在btn_Click()
,将页面级变量(例如this.clickedButtonId
)设置为ID,然后在btn.ID==clickedButtonId
btn.Focus()
createPagingButtons()
调用btn.Focus()
:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.