0

I want to add checkbox in table heads dynamically,I have created their ids dynamically but how to print it on Aspx Page ???

 <table border="1">
            <thead>
                <%string j = " Check"; %>
                <%for (int i = 0; i < 10;i++ )
                  {%>


                <th style="padding:2px; width:500px;">Table Head<br /><br />
                   <%
                      CheckBox chk = new CheckBox();
                      chk.ID = i + j;
                      chk.Text = "I am "+ i+j;



                         %>
                    <%=chk %>
                </th>


                <%} %>

            </thead>
        </table>
Alina Anjum
  • 1,178
  • 6
  • 30
  • 53
  • please check http://stackoverflow.com/questions/18914357/how-to-add-controls-dynamically-when-click-button-in-asp-net – abhi Feb 01 '16 at 06:23
  • what parts do you want to do it ?(in front end r back end ) – Willie Cheng Feb 01 '16 at 06:25
  • this link is about adding control from backend i.e from .cs page but I am trying to add this on aspx page..Kindly Help .. – Alina Anjum Feb 01 '16 at 06:28
  • Thanks willie ! But I want to do it on aspx page because I am printing "" on the aspx page and wants to bind the id from th text thats why i have to put it on aspx page – Alina Anjum Feb 01 '16 at 06:29
  • I have created the checkbox with specific Id i.e CheckBox chk = new CheckBox(); chk.ID = i + j; chk.Text = "I am "+ i+j; ------------------------------------------------------Now suggest me how can i place it on my aspx page – Alina Anjum Feb 01 '16 at 06:30

3 Answers3

1
<input type="checkbox" id='<%=i%>' name="allcheck">

Use ASP.NET Web Form, please use HTML tags more, not like this :)

CheckBox chk = new CheckBox();
chk.ID = i + j;
chk.Text = "I am "+ i+j;

example: aspx page:

<input type="hidden" id="userid" value='<%=userid>'/>
<input type="checkbox" id='<%=i%>' name="allcheck" /> with for 

js code with jquery:

function delete()
var id_array = new Array();
$('input[name="allcheck"]:checked').each(function () {
    id_array.push($(this).attr('id'));
});
var idstr = id_array.join(',');
$.ajax({
    method:"POST",
    url: "services/delete",
    data: {
        userid: $("#userid").val(), ids: idstr
    }
})

with ashx:

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    context.Response.Cache.SetNoStore();
    string userid = "";
    string ids = "";
    try
    {
        string userid = context.Request.QueryString["userid"];
        string ids = context.Request.QueryString["ids"];
        //then do your things
    }
    catch (Exception)
    {
        throw;
    }
}
Misa Lazovic
  • 2,805
  • 10
  • 32
  • 38
李允智
  • 95
  • 6
  • ok if i use this approach..How can I check on backend that the checkbox is Checked or not ? Because I have to put the values in Database depending upon Checked or Unchecked – Alina Anjum Feb 01 '16 at 06:32
  • Can u please give me an example.Thanks in advance – Alina Anjum Feb 01 '16 at 06:44
  • a new file,named such as delete.ashx $.ajax({ method:"POST", url: "services/delete.ashx",//services is a folder data: { userid: $("#userid").val(), ids: idstr } }) – 李允智 Feb 01 '16 at 07:27
1

Hopefully it can help you to deal with your problem.

Front end

<body>
    <form id="form1" runat="server">
    <table>
        <thead id="test" runat="server">

        </thead>
    </table>
    </form>
</body>

Back end

protected void Page_Load(object sender, EventArgs e)
  {
            CheckBox cb = new CheckBox();
            cb.ID="****";
            test.Controls.Add(cb);
  }
7221
  • 23
  • 5
0

If you are using ASPX page then use simply any control like GirdView or DataList or Repeater.

In that you put your checkbox, it will create dynamic ID Automatically and you can easily find that control on your back end coding..

For example check below links.

http://www.asp.net/web-forms/overview/data-access/displaying-data-with-the-datalist-and-repeater/displaying-data-with-the-datalist-and-repeater-controls-vb

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.repeatlayout(v=vs.110).aspx

Shirish
  • 1,252
  • 11
  • 20
  • No I cant use Gridview because I am Adding Columns Dynamiccaly in a Loop Not Rows :( Kindly Help – Alina Anjum Feb 01 '16 at 06:46
  • Kindly give any Example if it is Possible.Thanks in Advance – Alina Anjum Feb 01 '16 at 06:47
  • Check links in my answer, i have updated answer with links. I think second link will be perfect for your solution. – Shirish Feb 01 '16 at 06:52
  • No .. It's not helpfull for me because I dont want to involve backend code to insert data in my table I want to Add the data on aspx page and it's recommended from me that i should use html table because I have written a huge line of code now i want to put checkboxes in table heads.Cant change the whole structure of the page – Alina Anjum Feb 01 '16 at 07:00
  • the structure which you are using is not proper.. you need to make that structure dynamic by using any control other wise if you have to change anything then you have to update whole coding, I recommend to use control to manage html/back end code. – Shirish Feb 01 '16 at 07:04