在C#.net webforms中,如何在不同条件下设置特定文本框的焦点?

在C#.net webforms中,如何在不同条件下设置特定文本框的焦点?,c#,webforms,textbox,focus,C#,Webforms,Textbox,Focus,我有四个文本框,分为两组: txtbox\u日期\u开始 txtbox\u日期\u结束 txtbox\u值\u开始 txtbox\u值\u结束 这四个文本框都有AutoPostBack=true if (Page.IsPostBack) { //copy value of txtbox_date_start to txtbox_date_end if ((txtbox_date_start.Text != "") && (txtbox_date_end.Te

我有四个文本框,分为两组:

  • txtbox\u日期\u开始
  • txtbox\u日期\u结束
  • txtbox\u值\u开始
  • txtbox\u值\u结束

这四个文本框都有AutoPostBack=true

if (Page.IsPostBack)
{
    //copy value of txtbox_date_start to txtbox_date_end
    if ((txtbox_date_start.Text != "") && (txtbox_date_end.Text == ""))
    {
        txtbox_date_end.Text = txtbox_date_start.Text;

        // How?
        // if txtbox_date_start was focused, then set focus to txtbox_date_end
    }

    //copy value of txtbox_value_start to txtbox_value_end
    if ((txtbox_value_start.Text != "") && (txtbox_value_end.Text == ""))
    {
        txtbox_value_end.Text = txtbox_value_start.Text;

        // How?
        //if txtbox_value_start was focused, then set focus to txtbox_value_end
    }
}
我最初的伪码是这样的:

if txtbox_date_start is not empty and is focused,

--> then when press tab,

----> txtbox_date_end will be = to txtbox_date_start

------> and txtbox_date_end will be focused
txtbox\u value\u start和txtbox\u value\u end的用途相同

我也试着用

if ( txtbox_date_start.Focused == true)

但是不能在if语句中使用Focuse()。

首先,使用string.Empty而不是“”

我会按照以下思路做一些事情,并根据输入集中精力:

//copy value of txtbox_date_start to txtbox_date_end
var startDate = txtbox_date_start.Text;
var endDate = txtbox_date_end.Text;

if ((startDate != string.Empty) && (endDate.Text == string.Empty))
{
    txtbox_date_end.Text = startDate;

    // How?
    // if txtbox_date_start was focused, then set focus to txtbox_date_end
    if(startDate != string.Empty && endDate == string.Empty){
        txtbox_date_end.Focus();
    }
    else if(endDate != string.Empty && startDate == string.Empty){
        txtbox_start_end.Focus();
    }
}