0

我有一个下拉框,它根据其值填充文本框值。它在更改时触发,但在页面加载时不会触发。如何让它在页面加载时触发?

   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {     
   TextBox3.Text = DropDownList1.SelectedValue;
        TextBox12.Text = DropDownList1.SelectedValue;
        TextBox21.Text = DropDownList1.SelectedValue;

     //etc
4

3 回答 3

2

Tim Schmelter 对钱的评论是正确的。

// Wire up to the page load event
protected void Page_Load(object sender, System.EventArgs e)
{
   updateTextBoxes();
}

// Wire up to the select index-changed event
protected void DropDownList1_SelectIndexChanged( object sender, EventArgs e )
{
    updateTextBoxes();
}


// your workhorse method
protected void updateTextBoxes()
{
   TextBox3.Text  = DropDownList1.SelectedValue;
   TextBox12.Text = DropDownList1.SelectedValue;
   TextBox21.Text = DropDownList1.SelectedValue;

   // etc.
}
于 2012-09-05T20:07:26.010 回答
1

它不会在页面加载时自动调用,您必须“手动”调用它:

 void Page_Load(object sender, System.EventArgs e) {

     // ....

     DropDownList1_SelectedIndexChanged(DropDownList1, e);

 }
于 2012-09-05T20:06:41.267 回答
0

SelectedIndexChanged响应用户驱动的更改而触发。将分配逻辑移动到另一个方法,Page_Load并从您的事件处理程序手动调用它。

于 2012-09-05T19:54:56.457 回答