Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ClientID of RadioButton in RadioButtonList

Status
Not open for further replies.

jonbatts

Programmer
Apr 12, 2005
114
US
Is there a way to get the ClientID of a RadioButton in a RadioButtonList? Or is the ClientID of a radiobutton in a RadioButtonList always the Client ID of the the RadioButtonList + "_" + the RadioButtons index? Thanks for your time, and have a great day!
 
is the ClientID of a radiobutton in a RadioButtonList always the Client ID of the the RadioButtonList + "_" + the RadioButtons index?
That is correct. However, why do you need this id? If you want to get a reference to the item, you could always use the FindByValue method.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thanks ca8msm. I originally wanted it for Javascript. i.e. (C#)
Code:
...
sScript += "if (document.getElementById(\"" + myRadioButtonList.ClientID + "_0\").checked)\n";
sScript += "{\n";
sScript += "    alert('Please select a virus you would like on your computer!');\n";
sScript += "    return false;\n";
sScript += "}\n";
sScript += "else\n";
sScript += "{\n";
sScript += "    return true;\n";
sScript += "{\n";

myButton.Attributes.Add("onclick", sScript);
...
But, I would love to not have to do this in the codebehind. I'd rather put it in script tags in the .aspx page. So I looked at this code (javascript)
Code:
...
document.getElementById("<%=GetClientID(myButton, -1)%>".onclick = myButton_onclick;
...
function myButton_onclick()
{
    if (document.getElementById("<%=GetClientID(myRadioButtonList, 0)%>").checked)
    {
        alert('Please select a virus you would like on your computer!');
        return false;
    }
    else
    {
        return true;
    }
}
And then in my CodeBehind
Code:
protected string GetClientID(Control myControl, Int16 iRadioButtonIndex)
{
    if (iRadioButtonIndex == -1)
        return myControl.ClientID;
    else
        return myControl.ClientID + "_" + iRadioButtonIndex;
}
Say I was referencing 30 controls in Javascript and getting their ClientIDs in the CodeBehind. Would this be more expensive in resources and time than doing it all in the CodeBehind using the former method? What if I was calling it for 1000 controls? Thanks for your time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top