将动态 cotrol 添加到 aspx 页

本文关键字:aspx 添加 动态 cotrol | 更新日期: 2023-09-27 18:32:38

我想在表头中动态添加复选框,我已经动态创建了他们的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>

将动态 cotrol 添加到 aspx 页

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

使用 ASP.NET Web表单,请多使用HTML标签,而不是像这样:)

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

例:ASPX 页:

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

带有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
    }
})

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

希望它可以帮助您解决问题。

前端

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

后端

protected void Page_Load(object sender, EventArgs e)
  {
            CheckBox cb = new CheckBox();
            cb.ID="****";
            test.Controls.Add(cb);
  }
如果您

使用的是ASPX页面,则只需使用任何控件,例如GirdViewDataListRepeater

在你把你的checkbox,它将创建dynamic ID Automatically,你可以很容易地在你的后端编码上找到那个控件。

例如,检查下面的链接。

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