2012-05-01 52 views
0

有沒有辦法將額外的參數發送到SelectedIndexChanged函數?如何在SelectedIndexChanged上發送額外的參數?

<asp:RadioButtonList 
        ID="rblMeetingPlace" 
        SelectedValue = '<%# Bind("intMtgLoc") %>' 
      *OnSelectedIndexChanged = "Validate('txtMeetPlaceOther')"* 
        runat="server" 
        RepeatDirection="Horizontal" 
> 
<asp:ListItem Value="1">Workshop</asp:ListItem> 
<asp:ListItem Value="2">Service provider agency</asp:ListItem> 
<asp:ListItem Value="3">Advocacy organization</asp:ListItem> 
<asp:ListItem Value="4">Public Space</asp:ListItem> 
<asp:ListItem Value="5">Other (specify): </asp:ListItem> 
<asp:ListItem Value="" Text="" style="display: none" /> 
</asp:RadioButtonList> 
<asp:TextBox ID="txtMeetPlaceOther" Text='<%# Bind("strMtgLocOth") %>' 
runat="server" /> 

我有幾個radiobuttonlist,我想在選擇「其他」時啓用文本框。 我想發送文本框的ID來啓用它。

有什麼想法?

回答

1

您可以輕鬆地做到這一點是這樣的:

<asp:radiobuttonlist id="rbl1" runat="server" 
    RepeatDirection="Horizontal" 
    AutopostBack="true" 
    SelectedValue='<%# Bind("intMtgLoc") %>' 
    OnselectedIndexChanged="rbl1_SelectedIndexChanged"> 
     <asp:ListItem Value="1">Workshop</asp:ListItem> 
     <asp:ListItem Value="2">Service provider agency</asp:ListItem> 
     <asp:ListItem Value="3">Advocacy organization</asp:ListItem> 
     <asp:ListItem Value="4">Public Space</asp:ListItem> 
     <asp:ListItem Value="5">Other (specify): </asp:ListItem> 
     <asp:ListItem Value="" Text="" style="display:none" /> 
     </asp:radiobuttonlist> 
<asp:textbox id="txtMeetPlaceOther" text='<%# Bind("strMtgLocOth") %>' runat="server" /> 
<asp:textbox id="TextBox1" enabled="false" runat="server"></asp:textbox> 
<asp:textbox id="TextBox2" enabled="false" runat="server"></asp:textbox> 

,並在後臺代碼:

protected void rbl1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     *yourValidatorName*.Validate(); 
     if (Convert.ToInt32(rbl1.SelectedValue) == 5) 
     { 
      TextBox1.Enabled = true; 
      TextBox2.Enabled = true; 
     } 
     else 
     { 
      TextBox1.Enabled = false; 
      TextBox2.Enabled = false; 
     } 
    } 

回覆評論:你應該設置OnSelectedIndexChanged所有RadioButtonLists事件處理程序的 第一。這裏 - rbl_SelectedIndexChanged。 然後在後面的代碼:

protected void rbl_SelectedIndexChanged(object sender, EventArgs e) 
     { 

      TextBox[] textboxes = new TextBox[] { TextBox1, TextBox2 };//all your textboxes. 

      RadioButtonList whoCallEvent = sender as RadioButtonList; 


      string last = whoCallEvent.ID.ToString().Substring(whoCallEvent.ID.ToString().Length - 1, 1);//get the last symbol of object (TextBox) ID, who call event. 
      int index = Convert.ToInt32(last); 
      if (Convert.ToInt32(whoCallEvent.SelectedValue) == 5) 
      { 
       textboxes[index - 1].Enabled = true; 
      } 
      else 
      { 
       textboxes[index - 1].Enabled = false; 
      } 
     } 

但我認爲這是錯誤的概念做這種方式。最好的辦法是爲我的網頁上的所有radioButtonList創建rbl_SelectedIndexChanged

+0

使用你的方法我將不得不爲我的頁面上的所有radioButtonList創建rbl1_SelectedIndexChanged。我想創建一個可以處理TextBox啓用/禁用的單個函數 – SZT

相關問題