2

In my markup (Default.aspx), I have a simple table:

<asp:Table id="myAspTable" runat="server" />

I my code behind (Default.aspx.cs), I have an integer (n) which can be anything from 1 to 100:

int n = getValueOfN();

Based on this number, I can create checkboxes and textboxes dynamically and add them to my page:

CheckBox[] checks = new CheckBox[n];
TextBox[] texts = new TextBox[n];

for (int i=0; i<n; i++)
{
    checks[i] = new CheckBox();
    texts[i] = new TextBox();

    tblrow = new TableRow();
    tblcell = new TableCell();
    tblcell.Controls.Add(checks[i]);
    tblcell.Controls.Add(texts[i]);
    tblrow.Controls.Add(tblcell);
    myAspTable.Controls.Add(tblrow);
}

I now want to add the following functionality: Each checkbox i must enable or disable textbox i, when checked/unchecked respectively. How do I do this 100% in the code-behind?

Here is what I have tried:

checks[i].AutoPostBack = true;
checks[i].CheckedChanged += new EventHandler(this.CheckToggleEnable);


public void CheckToggleEnable(object sender, EventArgs e)
{
    // Implementation here
}

But this doesn't work because I have no way of referencing Textbox i in the CheckToggleEnable function. Also, I was hoping to do this without a post-back.

1
  • Please don't just ask us to solve the problem for you. Show us how you tried to solve the problem yourself, then show us exactly what the result was, and tell us why you feel it didn't work. See "What Have You Tried?" for an excellent article that you really need to read. Commented Sep 2, 2014 at 19:51

2 Answers 2

3

On each checkbox set:

AutoPostBack="True"

Which posts back to the server everytime one is clicked. You can attach to the CheckChanged event, and then find the correct textbox and set:

Enabled="True"
Enabled="False"

Depending on the condition.

I would advise, if you have a lot of checks, to do this in client-side JavaScript because of the performance of posting back to the server repeatedly... it can be intensive on the server.

Sign up to request clarification or add additional context in comments.

Comments

2

In ASP.Net, if you create a control dynamically, you need to recreate it back when it is posted back to server.

Basically, n should not be dynamic in your code. Otherwise, we do not know how many control we need to recreate in postback.

The following code will retrieve the sibling TextBox once a CheckBox is checked.

<asp:Table ID="myAspTable" runat="server" />

protected void Page_Load(object sender, EventArgs e)
{
    int n = 3;

    for (int i = 0; i < n; i++)
    {
        var checkBox = new CheckBox();
        var textBox = new TextBox();

        var tblrow = new TableRow();
        var tblcell = new TableCell();
        tblcell.Controls.Add(checkBox);
        tblcell.Controls.Add(textBox);
        tblrow.Controls.Add(tblcell);
        myAspTable.Controls.Add(tblrow);

        checkBox.AutoPostBack = true;
        checkBox.CheckedChanged += CheckBox_CheckedChanged;
    }
}

protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
    var checkbox = sender as CheckBox;

    var textbox = checkbox.Parent.Controls.OfType<TextBox>()
        .Select(control => control)
        .FirstOrDefault();

    if (textbox != null)
    {
        string value = textbox.Text;
    }
}

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.