简体   繁体   English

将动态cotrol添加到aspx页面

[英]Add Dynamic cotrol to aspx Page

I want to add checkbox in table heads dynamically,I have created their ids dynamically but how to print it on Aspx Page ??? 我想在表头中动态添加复选框,我已经动态创建了它们的ID,但是如何在Aspx页面上打印呢?

 <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>
<input type="checkbox" id='<%=i%>' name="allcheck">

Use ASP.NET Web Form, please use HTML tags more, not like this :) 使用ASP.NET Web窗体,请更多使用HTML标记,而不是这样:)

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

example: aspx page: 示例:aspx页面:

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

js code with jquery: 带有jQuery的js代码:

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: 使用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;
    }
}

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);
  }

If you are using ASPX page then use simply any control like GirdView or DataList or Repeater . 如果您使用的是ASPX页面,则只需使用GirdViewDataListRepeater类的任何控件GirdView

In that you put your checkbox , it will create dynamic ID Automatically and you can easily find that control on your back end coding.. 在您的checkbox ,它将自动创建dynamic ID Automatically并且您可以在后端编码中轻松找到该控件。

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 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 https://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.datalist.repeatlayout(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM