2014-04-04 40 views
1

我有一個單選按鈕,它從數據庫中動態獲取它的值。當用戶檢查「其他」按鈕時,使textboxt可見

其中一個數據庫值稱爲「其他」。

如果用戶檢查此Other單選按鈕,使文本框可見,以便用戶可以鍵入它。

有誰知道如何做到這一點?

下面的代碼不起作用。

<tr> 
<td> 
    <asp:RadioButtonList ID="RadioButtonList1" runat="server"></asp:RadioButtonList> 
    <asp:TextBox ID="txtOther" runat="server" Visible="false" Font-Bold="False"></asp:TextBox> 
    <asp:CheckBoxList ID="CheckBoxList1" runat="server"></asp:CheckBoxList> 
    <asp:TextBox ID="TextBox1" runat="server" Columns="30" Font-Bold="False" Rows="5" 
              TextMode="MultiLine"></asp:TextBox> 
</td> 
</tr> 

Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound 
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then 
     Dim anstype As HiddenField = e.Item.FindControl("HiddenField1") 
     'Dim questionid As Label = e.Item.FindControl("Label3") 
     Dim questionid As HiddenField = e.Item.FindControl("HiddenField2") 
     Dim rbl As RadioButtonList = e.Item.FindControl("RadioButtonList1") 
     Dim cbl As CheckBoxList = e.Item.FindControl("CheckBoxList1") 
     Dim txt As TextBox = e.Item.FindControl("TextBox1") 
     Dim ds As DataSet = GetDataSet(questionid.Value) 
     Select Case anstype.Value 
      Case "S" 
       rbl.Visible = True 
       cbl.Visible = False 
       txt.Visible = False 
       rbl.DataSource = ds 
       rbl.DataTextField = "Choice" 
       rbl.DataValueField = "ChoiceID" 
       rbl.DataBind() 
      Case "M" 
       rbl.Visible = False 
       cbl.Visible = True 
       txt.Visible = False 
       cbl.DataSource = ds 
       cbl.DataTextField = "Choice" 
       cbl.DataValueField = "ChoiceID" 
       cbl.DataBind() 
      Case "T" 
       rbl.Visible = False 
       cbl.Visible = False 
       txt.Visible = True 
     End Select 
    End If 
End Sub 

Protected Sub RadioButton1_OnCheckedChanged(sender As Object, e As EventArgs) 
    Dim RadioButton1 As RadioButtonList = TryCast(sender, RadioButtonList) 
    If RadioButton1 IsNot Nothing Then 
     If RadioButton1.SelectedValue = "Other" Then 
      Dim datalistrow As DataList = TryCast(RadioButton1.NamingContainer, DataList) 
      Dim TxtOther As TextBox = TryCast(datalistrow.FindControl("TxtOther"), TextBox) 
      TxtOther.Visible = True 
     End If 
    End If 

End Sub 

我也將要補充的是,其他的單選按鈕的值是33。

換句話說,<input id="DataList1_RadioButtonList1_5_2_5" type="radio" name="DataList1$ctl06$RadioButtonList1" value="33" />

由於多

回答

0

Tairoc,

值你綁定單選按鈕作爲

   rbl.DataTextField = "Choice" 
       rbl.DataValueField = "ChoiceID" 

好比說數據庫值VAL1,VAL2,VAL3和其他

出的這4個選項是單選按鈕列表,你在哪裏設置的選定值..

,如果你設置你的單選按鈕列表在DataList1_ItemDataBound事件選擇的值..

你可以寫出如下代碼..

<tr> 
<td> 
    <asp:RadioButtonList ID="RadioButtonList1" runat="server"></asp:RadioButtonList> 
    <asp:TextBox ID="txtOther" runat="server" Visible="false" Font-Bold="False"></asp:TextBox> 
    <asp:CheckBoxList ID="CheckBoxList1" runat="server"></asp:CheckBoxList> 
    <asp:TextBox ID="TextBox1" Visible="false" runat="server" Columns="30" Font-Bold="False" Rows="5" 
              TextMode="MultiLine"></asp:TextBox> 
</td> 
</tr> 

Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound 
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then 
     Dim anstype As HiddenField = e.Item.FindControl("HiddenField1") 
     'Dim questionid As Label = e.Item.FindControl("Label3") 
     Dim questionid As HiddenField = e.Item.FindControl("HiddenField2") 
     Dim rbl As RadioButtonList = e.Item.FindControl("RadioButtonList1") 
     Dim cbl As CheckBoxList = e.Item.FindControl("CheckBoxList1") 
     Dim txt As TextBox = e.Item.FindControl("TextBox1") 
     Dim ds As DataSet = GetDataSet(questionid.Value) 
     Select Case anstype.Value 
      Case "S" 
       rbl.Visible = True 
       cbl.Visible = False 
       txt.Visible = False 
       rbl.DataSource = ds 
       rbl.DataTextField = "Choice" 
       rbl.DataValueField = "ChoiceID" 
       rbl.DataBind() 

     // you juss binded the radiobuttonlist but you havent set the selected value.. 
    // there should be selected value "Other" so that based on the selected value we        
    //can make the textbox1 Visible 

      Case "M" 
       rbl.Visible = False 
       cbl.Visible = True 
       txt.Visible = False 
       cbl.DataSource = ds 
       cbl.DataTextField = "Choice" 
       cbl.DataValueField = "ChoiceID" 
       cbl.DataBind() 
      Case "T" 
       rbl.Visible = False 
       cbl.Visible = False 
       txt.Visible = True 
     End Select 
    End If 
End Sub 

如果不設置單選按鈕選擇在RowDataBound事件 一旦無線電按鈕列表值加載有內容

像例如說

行1)VAL1 VAL2 VAL3其他

行2)VAL1 VAL2 VAL3其他

第3行)Val1 Val2 Val3其他

當用戶在單選按鈕列表中選擇「其他」選項,然後在Radiobutton OnSelectedIndexChanged事件中必須這樣寫。

Protected Sub RadioButtonList1_OnSelectedIndexChanged(sender As Object, e As EventArgs) 
    Dim RadioButton1 As RadioButtonList = TryCast(sender, RadioButtonList) 
    If RadioButton1 IsNot Nothing Then 
     Dim datalistrow As DataList = TryCast(RadioButton1.NamingContainer, DataList) 
     Dim TxtOther As TextBox = TryCast(datalistrow.FindControl("TxtOther"), TextBox) 

     If RadioButton1.SelectedValue = "Other" Then 

      TxtOther.Visible = True 
     Else 
      TxtOther.Visible = False 
     End If 
    End If 

End Sub 


Protected Sub TextBox1_OnTextChanged(sender As Object, e As EventArgs) 
    If TextBox1.Text IsNot Nothing Then 
     RadioButton1.Items.Add(TextBox1.Text) 
    End If 
End Sub 
+0

好從你再次聽到並非常感謝您的幫助。 首先,我得到錯誤,TextBox1和RadioButton1沒有聲明。 我在TextBox1_OncTextChanged子上得到這個錯誤。 – Tairoc

0

試試這個..

Protected Sub TextBox1_OnTextChanged(sender As Object, e As EventArgs) 
    Dim TextBox1 As TextBox = TryCast(sender, TextBox) 
    If TextBox1 IsNot Nothing Then 
     Dim RadioButton1 As RadioButtonList = TryCast(TextBox1.Parent.FindControl("RadioButton1"), RadioButtonList) 
     If TextBox1.Text IsNot Nothing Then 
      RadioButton1.Items.Add(TextBox1.Text) 
     End If 
    End If 

End Sub 
相關問題