使用UpdatePanel时获取动态生成的HTML控件 [英] Getting dynamically generated HTML controls when using UpdatePanel

查看:61
本文介绍了使用UpdatePanel时获取动态生成的HTML控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaScript动态生成HTML元素.这些控件位于浏览器的更新面板中.

由于某种原因,我无法访问Page.Form []数组中的那些元素.

这有原因吗?

我当前的解决方案创建一个字符串数组,将其转换为JSON字符串,并将其存储在一个隐藏变量中,该变量将在服务器端代码中之前定义了该隐藏字段的情况下发布回服务器.

标记

 < asp:UpdatePanel ID ="UpdatePanel1" runat ="server">< ContentTemplate>< div id ="dynamicInputsGoHere" class ="hiddenContent" runat ="server"></div></ContentTemplate> 

用于创建输入的JavaScript

  for(i = currentSize; i< numberInputsToCreate; i ++){html + =<输入类型='文本'id ='inputNum" + i.toString()+'/>";}$('#dynamicInputsGoHere').append(html); 

我正在尝试访问更新面板回发时由javascript生成的动态输入.

解决方案

您需要为输入元素提供

然后在服务器上,以以下方式访问它们:

 字符串值0 = Request.Form ["inputNum0"];字符串值1 = Request.Form ["inputNum1"];字符串值2 = Request.Form ["inputNum2"]; 

您最好在隐藏字段( type ="hidden" )中发送 numberInputsToCreate ,并在服务器上使用循环将这些值提取出来./p>

I'm generating HTML elements on the fly using javascript. These controls are located within an update panel on the browser.

For some reason, I'm unable to access those elements in the Page.Form[] array.

Is there a reason for this?

My current solution creates an array of strings, turns that into a JSON string, stores that in a hidden variable which gets posted back to the server as the hidden field was defined before in server side code.

Markup

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

    <ContentTemplate>
     <div id="dynamicInputsGoHere" class="hiddenContent" runat="server"></div>
    </ContentTemplate>

Javascript to create inputs

for (i = currentSize; i < numberInputsToCreate; i++) {
                html += "<input type='text' id='inputNum" + i.toString() + "' />";
            }
$('#dynamicInputsGoHere').append(html);

I'm trying to access the dynamic inputs that were generated by javascript when the update panel posts back.

解决方案

You need to give your input elements a name attribute in order for them to get submitted with the form (and available in Request.Form):

for (var i = currentSize; i < numberInputsToCreate; i++) {
    var name = "inputNum" + i;
    html += '<input type="text" id="' + name + '" name="' + name + '" />';
}

$('#dynamicInputsGoHere').append(html);

Then on the server, access them as:

string value0 = Request.Form["inputNum0"];
string value1 = Request.Form["inputNum1"];
string value2 = Request.Form["inputNum2"];

You're probably best to send numberInputsToCreate in a hidden field (type="hidden") and use a loop on the server to pluck the values out.

这篇关于使用UpdatePanel时获取动态生成的HTML控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆