简体   繁体   English

页面加载后使用Javascript创建ASP.NET标签

[英]Creating ASP.NET labels with Javascript after page has loaded

I have written a script which dynamically creates a html table based off server side variables passed through hidden fields. 我编写了一个脚本,该脚本根据通过隐藏字段传递的服务器端变量动态创建html表。 Ideally, I want to add a .NET label and Radio button control to each cell that I can populate server side. 理想情况下,我想向可以填充服务器端的每个单元格添加一个.NET标签和单选按钮控件。 I realize that .NET controls are all created when the page is first compiled, but is there anyway to add them later? 我意识到,.NET控件都是在首次编译页面时创建的,但是以后有没有添加它们的方法呢? Here is my code that builds the table: 这是我建立表的代码:

        <script>
        $(document).ready(function () {
            var satShifts = $('#mainContent_hidSat').val();
            var sunShifts = $('#mainContent_hidSun').val();
            var monShifts = $('#mainContent_hidMon').val();
            var tueShifts = $('#mainContent_hidTue').val();
            var wedShifts = $('#mainContent_hidWed').val();
            var thuShifts = $('#mainContent_hidThu').val();
            var friShifts = $('#mainContent_hidFri').val();

            var c = 0;

            for (var i = 0; i < satShifts; i++) {
                $('#tblSat').append('<tr><td class="individualShift">Shift: ' + ***LABEL & RAD here*** + '</td></tr>');
                c++;
            }
            for (var i = 0; i < sunShifts; i++) {
                $('#tblSun').append('<tr><td class="individualShift">Shift: ' + ***LABEL & RAD here*** + '</td></tr>');
                c++;
            }
            for (var i = 0; i < monShifts; i++) {
                $('#tblMon').append('<tr><td class="individualShift">Shift: ' + ***LABEL & RAD here*** + '</td></tr>');
                c++;
            }
            for (var i = 0; i < tueShifts; i++) {
                $('#tblTue').append('<tr><td class="individualShift">Shift: ' + ***LABEL & RAD here*** + '</td></tr>');
                c++;
            }
            for (var i = 0; i < wedShifts; i++) {
                $('#tblWed').append('<tr><td class="individualShift">Shift: ' + ***LABEL & RAD here*** + '</td></tr>');
                c++;
            }
            for (var i = 0; i < thuShifts; i++) {
                $('#tblThu').append('<tr><td class="individualShift">Shift: ' + ***LABEL & RAD here*** + '</td></tr>');
                c++;
            }
            for (var i = 0; i < friShifts; i++) {
                $('#tblFri').append('<tr><td class="individualShift">Shift: ' + ***LABEL & RAD here*** + '</td></tr>');
                c++;
            }
        });
    </script>

You cannot add them as a .NET control once the page has been loaded. 加载页面后,您不能将它们添加为.NET控件。 This is by design. 这是设计使然。

You can however add them as HTML elements and then access their values in the code-behind using Request.Form["textboxName"] . 但是,您可以将它们添加为HTML元素,然后使用Request.Form["textboxName"]在后台代码中访问它们的值。

You would then set the name value of your textboxes to whatever value you wanted to retrieve them by in the code-behind (In this case, it would be name="textboxName" ). 然后,您可以将文本框的name值设置为您想要在代码隐藏中检索它们的值(在这种情况下,它将为name="textboxName" )。

Alternatively, you could render them as ASP.NET controls from the back-end using the server side variables. 或者,您可以使用服务器端变量从后端将它们呈现为ASP.NET控件。 If this is the route you want to take, I'll elaborate further. 如果这是您要采取的路线,我将进一步详细说明。

Update : Everything you're trying to do in javascript can be done with Repeaters . 更新 :您要使用javascript进行的所有操作都可以通过Repeaters完成。

For each day on the back end, add a repeater that looks like this: 对于后端的每一天,添加一个如下所示的转发器:

<asp:Repeater ID="rptSaturdayShifts" runat="server">
   <ItemTemplate>
      <tr><td class="individualShift">Shift: <asp:Label id="lblYourLabel" runat="server" Text='<%# Eval("LabelContent") %>' /> <asp:RadioButton id="rbYourRadioButton" runat="server" /></td></tr>
   </ItemTemplate>
</asp:Repeater>

You just databind your repeater with a collection that has a "LabelContent" property and voila, your data is rendered. 您只需将中继器与具有“ LabelContent”属性的集合进行数据绑定,瞧,您的数据将被呈现。 Now, when saving, you'll have to iterate through all of the repeater rows. 现在,保存时,您必须遍历所有转发器行。 So on your "Save" button click: 因此,在“保存”按钮上,单击:

foreach (var item in rptSaturdayShifts.Items) 
{
   var yourRadioButton = (Label) item.FindControl("rbYourRadioButton");

   // You can then access it using...
   var radioButtonValue = yourRadioButton.SelectedValue;
}

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

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