簡體   English   中英

將動態cotrol添加到aspx頁面

[英]Add Dynamic cotrol to 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">

使用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類的任何控件GirdView

在您的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/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