4

I have a collection of checkbox some 40-50 nos and i have set a attribute 'attr-ID' for each checkbox which is a unique value ID in database. how can i get the control by attribute name in c# code. I want to check some of the checkbox according to dB values on page load event.

 <input type="checkbox" id="rdCervical" attr-ID='111' runat='server' />
6
  • 1
    Do they have runat="server" attribute or are they simple html elements? Commented Dec 22, 2011 at 13:54
  • HTML5 allows attributes that begin with data- but for anything else I think you'll have to write your own parser, I'm afraid. Commented Dec 22, 2011 at 13:55
  • yea they have runat server but i cannot identifiy the control id in c# Commented Dec 22, 2011 at 14:00
  • If they have runat="=server" they should be available in the code behind. are they located maybe in a custom control? Can you post some more code of the containers? Commented Dec 22, 2011 at 14:05
  • its a normal html control but i have many checkbox and identifying each checkbox id in server side have to check each id and the attribute value which i dont need. so if there some way to check the attribute and get it checked. Commented Dec 23, 2011 at 4:04

2 Answers 2

6

Is this what you want?

protected void Page_Load(object sender, EventArgs e)
{
    var cb = FindControlByAttribute<HtmlInputCheckBox>(this.Page, "attr-ID", "111");

}
public T FindControlByAttribute<T>(Control ctl, string attributeName, string attributeValue) where T : HtmlControl
{
    foreach (Control c in ctl.Controls)
    {
        if (c.GetType() == typeof(T) && ((T)c).Attributes[attributeName]==attributeValue)
        {
            return (T) c;
        }
        var cb= FindControlByAttribute<T>(c, attributeName, attributeValue);
        if (cb != null)
            return cb;
    }
    return null;
}
Sign up to request clarification or add additional context in comments.

3 Comments

He is not using server controls, but html controls HtmlInputCheckBox
@Arion : can u specify a region in your function means to find a control in a specified div like i have some 50 checkbox in a div which have a id ='dvCheckboxes'. i used your funciton but i am always getting null value. thanks for your post.. and pls help
@deepu: this function loops all the control on the page. If there is a control with the attribute attr-ID and the value of that is 111 the it will return it. If you have a div with checkboxes see so that it has runat="server". Then you can use that control in this function and it will return the checkbox
5
if (rdCervical.Attributes["attr-ID"] != null)
{
      string id = rdCervical.Attributes["attr-ID"];
      rdCervical.Checked = true;
}

I assume you are adding the checkboxes programatically. In that case, make a container <div id="checkContainer" runat="server"></div> and put all your checkboxes inside it. Then just use checkContainer.InnerHtml and parse that code with this library. You can then easily use the library API to find elements by attribute, I think the method was SelectNodes

Without this library, there is no easy way to navigate through HTML elements from code.

2 Comments

the checkbox id cannot be checked since i have many checkboxes i have a common attribute for all checkbox using it i need to identify which checkbox is need to be checked
I assume you are adding the checkboxes programatically. In that case, make a container <div id="checkContainer" runat="server"></div> and put all your checkboxes inside it. Then just use checkContainer.InnerHtml and parse that code with this library. You can then easily use the library API to find elements by attribute. htmlagilitypack.codeplex.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.