开发者

Dynamically add attribute for radiobutton

开发者 https://www.devze.com 2023-03-25 19:09 出处:网络
I have a usercontrol with 3 radiobuttons and textbox. If RB1 is selected, I will display the textbox. For the remaining two options, I will hide the textbox.

I have a usercontrol with 3 radiobuttons and textbox. If RB1 is selected, I will display the textbox. For the remaining two options, I will hide the textbox.

    <asp:RadioButton ID="rbAPL" runat="server" Checked="true" CssClass="tablecelldata"
                GroupName="ServiceType" Text="Testing of Animal Pathogens & Veterinary Biologics" /><br />
    <asp:RadioButton ID="rbVPHL" runat="server" CssClass="tablecelldata" GroupName="ServiceType"
                Text="Food Testing and Export Health Certification (Veterinary Health Certificate for Meat, Fish & Dairy Products)"  /><br />
    <asp:RadioButton ID="rbPHL" runat="server" CssClass="tablecelldata" GroupName="ServiceType"
          开发者_如何转开发      Text="Plant Health Diagnosis Services" />

And the textbox

    <asp:TextBox ID="tbxApplicationRefNo" runat="server" Width="350px"></asp:TextBox>

I want to dynamically set the attributes of radiobuttons to show or hide the textbox. How can I do so?

Thanks in advance for your reply!


1 Add handler to js-onclick event of radiobutton

2 For show/hide using jquery (for example: "$('#id').show();")

3 Pay attention initially TextBox is invisible cos RB1 is cheked

<asp:RadioButton ID="rbAPL" runat="server"  Checked="true" CssClass="tablecelldata" GroupName="ServiceType" Text="Testing of Animal Pathogens & Veterinary Biologics" onclick='<%# string.Format("$(&#39;#{0}&#39;).hide();", tbxApplicationRefNo.ClientID) %>' />
<br />
<asp:RadioButton ID="rbVPHL" runat="server" CssClass="tablecelldata" GroupName="ServiceType" Text="Food Testing and Export Health Certification (Veterinary Health Certificate for Meat, Fish & Dairy Products)" onclick='<%# string.Format("$(&#39;#{0}&#39;).show();", tbxApplicationRefNo.ClientID) %>' />
<br />
<asp:RadioButton ID="rbPHL" runat="server" CssClass="tablecelldata" GroupName="ServiceType" Text="Plant Health Diagnosis Services" onclick='<%# string.Format("$(&#39;#{0}&#39;).show();", tbxApplicationRefNo.ClientID) %>' />
<br />
<asp:TextBox style="display:none" ID="tbxApplicationRefNo" runat="server" Width="350px" Text="hello :)"></asp:TextBox>  


I would do something like this in the code behind:

public void rbAPL_CheckedChanged(object sender, EventArgs e)
{
    RadioButton button = sender as RadioButton;

    if (button.Checked)
    {
        tbxApplicationRefNo.Visible = true;
    }
}

Set the Textbox as Visible when it is clicked.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号