0

I have dynamically created the controls (textbox and checkbox) through javascript. i.e each time I click the addrow button a new row is generated.

But I am unable to access the newly created elements through code behind.

Please assist :)


-----------aspx file--------------- Add/Remove dynamic rows in HTML table

<script language="javascript">
    function addRow(tableID) {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);
        var cell1 = row.insertCell(0);
        var element1 = document.createElement("input");
        element1.type = "checkbox";
        element1.name = "chkbox[]";
        cell1.appendChild(element1);
        var cell2 = row.insertCell(1);
        cell2.innerHTML = rowCount + 1;
        var cell3 = row.insertCell(2);
        var element2 = document.createElement("input");
        element2.type = "text";
        element2.name = "txtbox[]";
        cell3.appendChild(element2);
    }

</script>

 </head>
 <body>
<form id="form1" runat="server">
<div>
    <input type="button" value="Add Row" onclick="addRow('dataTable')" />&nbsp;
    <table id="dataTable" width="350px" border="1">
        <tr>
            <td>
                <input type="checkbox" name="chk" />
            </td>

            <td>
                <input type="text" />
            </td>
        </tr>
    </table>
</div>
</form>
  </body>

2 Answers 2

1

Though it is not possible to get the dynamically created controls using java-script by ID.

But I thing you can use name property of the control in while creating it through java-script.

And I think then you can get in Request.Forms collection.

The id property only identifies the tag within the DOM. The name property allows the tag to be posted back as part of a form. An ASP.NET control, or an HTML element with runat=server, automatically gets both the name and id properties assigned.

But the controls created by java-script you need to have the name property specified.

References :- Form Post Values using plain html controls in ASP.NET web forms

Another link

http://www.quepublishing.com/articles/article.aspx?p=28493

Edit -1

suppose I have created a input type='text' name='companyname' then I can get values as follows

string companyname = Request.Form["companyname"];

Suppose I am creating an input as in question

var element2 = document.createElement("input");
element2.name='companyname'

Add this element to form

cell3.appendChild(element2);

And you can get it on code behind as

 string companyname = Request.Form["companyname"];
Sign up to request clarification or add additional context in comments.

8 Comments

Though it is not possible to get the dynamically created controls using java-script by ID Please read the ClientIDMode property
The OP is creating controls in java-script not in code behind.
Check his code with Request.Form[]. It gives null. Not working
@Murali at which event you are looking??
Request.Forms will be accessible through out the page life cycle, what ever the event it is. :)
|
0

You are not able to access the control values from code behind because those are not server controls.the controls which have the attribute runat="server" can only be accessed from code behind.

As a workaround, you can send the values to a web service using AJAX, and you can process it from there.

1 Comment

Wrong. You can access them if they are submitted via a POST request, but you don't get the automatic assignment to a server-side control because they weren't in the Viewstate. Use Request.Form["controlname"] to access the posted data.

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.