Focus() 在动态创建的服务器控件上

本文关键字:服务器控件 创建 动态 Focus | 更新日期: 2023-09-27 18:32:07

>我正在使用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 端静态的特定名称。在回发时,我想 button.focus() 单击的那个。

我如何实现这一点?

Focus() 在动态创建的服务器控件上

btn_Click() 中,将页面级变量(例如,this.clickedButtonId)设置为 ID,然后在createPagingButtons()调用 btn.Focus() 如果btn.ID==clickedButtonId

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;
}