How to access dynamically created controls with Literal in C#

Posted by Samirbhogayta under C# on 12/10/2013 | Points: 10 | Views : 7269 | Status : [Member] | Replies : 3
hi..

I have created dynamic controls with following codes. How to get values from that controls that checkbox is checked or not and also get values from textbox. Please reply as early as possible.
.aspx

<asp:Literal ID="ltMessageBoard" runat="server"></asp:Literal>

.cs

StringBuilder sb = new StringBuilder();
sb.Append("<table id='TableID' runat='server'>");
sb.Append("<tr>");
sb.Append("<th style='text-align:left;'><input type='checkbox' id='selectall'/>&nbsp;Select All</th>");
sb.Append("</tr>");

while (dr1.Read())
{
sb.Append("<tr>");
sb.Append("<td>");
sb.Append("<input type='checkbox' id='chk1' class='case' name='case'/>&nbsp;&nbsp;");
sb.Append("<label id='lbl1'></lable>");
sb.Append("</td>");
sb.Append("</tr>");

sb.Append("<tr>");
sb.Append("<td>");
sb.Append("<input type='text' id='txt1' style='width:500px; height:100px;text-align:top;' runat='server'/>");
sb.Append("</td>");
sb.Append("</tr>");
}
sb.Append("</table>");
ltMessageBoard.Text = sb.ToString();

SAMIR
Sr. Software Engineer



Responses

Posted by: vishalneeraj-24503 on: 12/10/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Now, you have to find first table
using document.getelementbyid('table_id');
then,find input controls using getelementbytagname('input');

then loop through that rows

check control type,then you can access control inside Table element.


Samirbhogayta, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/10/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
refer:-
http://stackoverflow.com/questions/8642065/find-a-control-s-value-inside-a-table-in-tr
http://stackoverflow.com/questions/19243874/how-to-check-what-control-is-inside-a-table-cellhttp://stackoverflow.com/questions/14443222/javascript-getting-reference-to-control-in-same-cell-of-table

Samirbhogayta, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 12/10/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Samirbhogayta,

I have seen you code and it is very default to read values of html controls form C#.
I suggest you change your dynamic control code and use HtmlGenericControl class to generate the dynamic controls and read values form them.

I have written an article on this:- Jut go through this and you will get idea on how to do this.

http://www.c-sharpcorner.com/UploadFile/8911c4/how-to-create-and-read-values-of-dynamic-controls-in-Asp-Net/

Let me know if you have any other issue.

If this helps you towards the solution, click on MARK IT AS ANSWER

Happy Coding.

Samirbhogayta, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response