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);
}
Hogan
  • 69,564
  • 10
  • 76
  • 117
nickrenfo2
  • 85
  • 1
  • 10
  • 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. – Lukas Häfliger Mar 13 '14 at 01:07
  • These questions describe a similar situation. [(link #1)](http://stackoverflow.com/q/13675015/451944) [(link #2)](http://stackoverflow.com/q/1954590/451944) – richaux Mar 25 '14 at 09: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. – richaux Mar 25 '14 at 09:26

1 Answers1

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.

aahoogendoorn
  • 444
  • 6
  • 9