0

我正在使用 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() 被点击的那个。

我如何实现这一目标?

4

1 回答 1

0

btn_Click()中,将页面级变量(例如this.clickedButtonId)设置为 ID,然后createPagingButtons()调用btn.Focus()if 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;
}
于 2013-09-19T18:49:33.973 回答