下拉选定的索引在页面加载时更改

时间:2012-09-05 19:38:07

标签: asp.net

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

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

     //etc

3 个答案:

答案 0 :(得分: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.
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

     // ....

     DropDownList1_SelectedIndexChanged(DropDownList1, e);

 }