简体   繁体   中英

Dynamically add attribute for radiobutton

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!

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.

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>  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM