1

I have 4 checkboxes, below each checkbox is a div. Each checkbox is responsible for showing or hiding the checkbox that is below it. for example:

    <asp:CheckBox ID="CheckBox1" myDiv="divRegisteration" myText=" הרשמה - " runat="server" AutoPostBack="true" Font-Size="18px" Font-Bold="true" Text=" הרשמה - הצג" OnCheckedChanged="CheckBox_CheckedChanged"/>
    <div id="divRegisteration" runat="server" visible="false">

the checkbox 'CheckBox1' is responsible for showing or hiding the div "divRegisteration", which is addressed in the custom attribute "myDiv".

problem is, in the code behind, it does not find the attribute "myDiv":

if (((CheckBox)(sender)).Checked==true)
{
  CheckBox chk = (CheckBox)(sender);
  object div = FindControl(chk.Attributes["myDiv"]); //// it does not find myDiv, and therefore doesn't find the control so the program crashes.
  HtmlGenericControl addressDiv = (HtmlGenericControl)(div);
  addressDiv.Visible = true;     
}
6
  • What is the exact wording of the error message you are getting? Commented Mar 28, 2013 at 23:01
  • Object reference not set to an instance of an object. Line 21: object div = FindControl(chk.Attributes["myDiv"]); Commented Mar 28, 2013 at 23:01
  • Are you 100% sure that chk.Attributes["myDiv"] returns empty value? Commented Mar 28, 2013 at 23:05
  • yes, for some reason all the attributes that it finds are CssStyle and Keys. i dont even know where those came from. Commented Mar 28, 2013 at 23:08
  • Well, then it seems that you are targeting wrong check box... obviously since you have some attributes you did not put in there Commented Mar 28, 2013 at 23:10

3 Answers 3

3

Because the Attributes collection doesn't work that way:

Gets the collection of arbitrary attributes (for rendering only) that do not correspond to properties on the control.

If you want to have properties like that, you need to create your own custom control that has the properties you want. Or, as an alternative, create a UserControl that hosts a single CheckBox and associated div or whatnot -- then you can just reference the one related div by ID in the codebehind. Instantiate multiple instances of that control, and you're good to go.

Edit: my WebForms-fu is a bit rusty, but here goes nothing.

The control class:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace UserControlExample {
    [ParseChildren(false)]
    public class TogglePanel : UserControl {
        private CheckBox cbToggleContent = new CheckBox();
        private Panel pnlContentPlaceholder = new Panel();

        public TogglePanel() {
            Load += OnLoad;
        }
        public bool Checked { get; set; }

        private void OnLoad(object sender, EventArgs eventArgs) {
            Controls.Add(cbToggleContent);
            Controls.Add(pnlContentPlaceholder);

            if (!IsPostBack) {
                cbToggleContent.Checked = Checked;
                pnlContentPlaceholder.Visible = Checked;
            }

            cbToggleContent.AutoPostBack = true;
            cbToggleContent.CheckedChanged += (s, args) => {
                pnlContentPlaceholder.Visible = cbToggleContent.Checked;
            };
        }

        protected override void AddParsedSubObject(object obj) {
            pnlContentPlaceholder.Controls.Add((Control) obj);
        }
    }
}

And its usage:

<%@ Register TagPrefix="a" Namespace="UserControlExample" Assembly="UserControlExample" %>

<a:TogglePanel Checked="True" runat="server">
    This stuff here will be shown or hidden based on the checkbox
</a:TogglePanel>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! How do i create a user control?
1

FindControl only searches the current naming context and does not traverse a hierarchy. By calling FindControl they way you are, it is using this.FindControl. Try something like chk.Parent.FindControl(...) if the div is a sibling of the CheckBox

Edit:

Ah, well, the Attributes collection is "for rendering purposes only". It doesn't appear to be populated with attributes specified in the ASPX HTML declaration.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.attributes.aspx

1 Comment

The problem is actually that it does not find the Attribute, and not the actual control..
0

Off the top of my head - doesn't the checkbox have an InputAttributes collection?

Admittedly, an untested stab:

CheckBox chk = (CheckBox)(sender);
object div = FindControl(chk.InputAttributes["myDiv"]);

Does something like that work?

Comments

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.