2

I currently have a table with two rows. Each row contains a checkbox (except the first) contains a checkbox in one column, a label in the next, and a text area in the third column.

I'm currently having two problems, one is my "Add Row" button will only add one row (and if I click it again, it won't add an additional row). I commented out the ID field thinking that might be why it won't add another row (don't want two items sharing and ID), however, that was not the case.

The other thing I don't know how to do is make it so that this function will be automatically called as soon as I start typing into the textbox "mcOpt1". Ideally I want it so that it will add a row with textbox named "mcOpt2", which will again add another row as soon as that one has text / is not empty.

I'm quite new to these languages, so how can this be achieved, if at all?

The Table:

   <asp:Table ID="mcOptTable" runat="server" CssClass="halfwide">
        <asp:TableRow runat="server" ID="question">
            <asp:TableCell runat="server"></asp:TableCell>
            <asp:TableCell runat="server">Question:</asp:TableCell>
            <asp:TableCell runat="server">
                <asp:TextBox ID="mcQuestion" runat="server" TextMode="MultiLine"></asp:TextBox>
            </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow runat="server" ID="opt1">
            <asp:TableCell runat="server">
                <asp:CheckBox ID="mcOpt1IsCorrect" runat="server" CssClass="leftmargin10" />
            </asp:TableCell>
            <asp:TableCell runat="server">Option 1:</asp:TableCell>
            <asp:TableCell runat="server">
                <asp:TextBox ID="mcOpt1" runat="server" TextMode="MultiLine"></asp:TextBox>
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>

The Button

<asp:Button ID="addRow" runat="server" Text="Add a Row" OnClick="addRow_Click" />

The C# Add Table Row Code

protected void addRow_Click(object sender, EventArgs e)
{
    TableRow row = new TableRow();
    TableCell optText = new TableCell();
    TableCell tBox = new TableCell();
    TableCell isCor = new TableCell();
    optText.Text = "Option 2:";
    tBox.Text = "text box here";
    //tBox.ID = "opt2";
    isCor.Text = "?";
    //isCor.ID = "opt2IsCorrect";
    row.Cells.Add(isCor);
    row.Cells.Add(optText);
    row.Cells.Add(tBox);
    mcOptTable.Rows.Add(row);
}
3
  • As for automatically call your function have a look at the events that get raised by the textbox. Subscribe a delegate to every textbox and call your desired method in it. Commented Mar 13, 2014 at 1:07
  • These questions describe a similar situation. (link #1) (link #2) Commented Mar 25, 2014 at 9:23
  • The new row is being added but because of the stateless nature of asp.net (more details in linked questions) the previous row is being destroyed. If you change "text box here" to String.Format("text box here {0}", DateTime.Now) you can see this more readily. Commented Mar 25, 2014 at 9:26

1 Answer 1

0

The problem is that you don't "remember" the added rows between postbacks. The first time you add a row (server side), it gets rendered on postback. However the next time you want to add a row (server side) the previous row isn't there anymore, basically because the table gets rendered in its original state again.

What you need to do is "remember" the rows you've added, and make sure that all of them are rendered on every postback.

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

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.