简体   繁体   English

无法将字符串传递给 Page.SetFocus

[英]Cannot Pass String to Page.SetFocus

Client wants after a save (post back) that the cursor returns to the last text box.客户希望在保存(回发)后 cursor 返回到最后一个文本框。 I have it all setup but Page.SetFocus will not accept the variable _toFocus.我已经完成了所有设置,但 Page.SetFocus 不接受变量 _toFocus。 But if I hard code it, it works flawlessly.但是,如果我对其进行硬编码,它将完美无缺。

    protected void TextChangedCheckFail(object sender, EventArgs e)
    {
        TextBox someTxt = sender as TextBox;
        _toFocus = someTxt.ID.ToString();
        //_toFocus = "\"" + someTxt.ID.ToString() + "\"";  Does NOT work
        if (Page.IsPostBack)
        {
            lblTest.Text = _toFocus;  //This label always shows that _toFocus is accurate ID
            //sets focus to the proper control
            Page.SetFocus(_toFocus);  //Does not work
            Page.SetFocus(txtDate);   //Works perfectly

        }
        DidItFail();
    }

For the life of me I cannot figure out why you cannot pass a variable.对于我的生活,我无法弄清楚为什么你不能传递一个变量。

ANSWERING my own question to help others.回答我自己的问题以帮助他人。 Because Page.SetFocus requires you pass a CONTROL!因为 Page.SetFocus 要求你传递一个 CONTROL!

Page.SetFocus(someTxt); Page.SetFocus(someTxt); works perfect (someTxt was the TextBox i created out of sender).完美运行(someTxt 是我从发件人创建的文本框)。

As I was posting (after two hours of insanity) I figured out my problem.当我发帖时(经过两个小时的精神错乱),我发现了我的问题。

My new code我的新代码

protected void TextChangedCheckFail(object sender, EventArgs e)
{
    TextBox someTxt = sender as TextBox;
    if (Page.IsPostBack)
    {
        //sets focus to the proper control
        Page.SetFocus(someTxt);
    }
    DidItFail();
}

You can set focus by just calling focus on the textbox itself;您可以通过在文本框本身上调用焦点来设置焦点; TextBoxID.Focus();文本框ID.Focus();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM