访问ASP.NET codebehind内的客户端动态控件 [英] Accessing client side dynamic controls within ASP.NET codebehind

查看:126
本文介绍了访问ASP.NET codebehind内的客户端动态控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我试图访问那些事件中动态创建的HTML控件,但我无法访问此。

Hi I'm trying to access the html controls that are created dynamically within an event but I'm unable to access this.

我用下面的code上创建使用JavaScript客户端的元素:

I'm using the following code to create the elements on the client side using Javascript:

function addInput(field)
{
...declaratives... 

var input = document.createElement("input");
input.id = field+count;
input.name = field+count;
input.type = "text";

... remainder of the code and the element is added to the DOM...

}

我也有页面上的服务器控件(一个名为Button1的按钮),我想要做的就是当在用户点击按钮我想看看用户在动态创建已进入了细节输入框,并将它们存储在数据库中。然而,在事件保护无效的button1_Click(对象发件人,EventArgs五)我无法访问输入字段。

I also have a server control on the page (a button called Button1), and what I want to do is when the user clicks on the button I want to look at the details in that the user has entered in the dynamically created input boxes and store them in a database. However, in the event protected void Button1_Click(object sender, EventArgs e) I can't access the input fields.

这可能吗?

我使用VS 2010的C#(V4.0)。

I'm using VS 2010 C# (v4.0).

推荐答案

嗯,你通过JavaScript将客户端HTML控件 - ASP.NET不会看到这些(他们没有的Runat =服务器,这需要将色彩空间转换时间,而不是之后的页面加载)。

Well your adding client-side HTML controls via JavaScript - ASP.NET will not see those (they're not runat=server, this needs to be render-time, not after the page has loaded).

你可以做的是增加名属性,所有的元素(你所做的),那么当你提交表单(与单击按钮),你可以通过<$ C $检查表单元素C>的Request.Form 集合。

What you can do is add "name" attributes to all your elements (which you've done), then when you submit the form (with the Button click), you can check the form elements via the Request.Form collection.

protected void Button1_Click(object sender, EventArgs e)
{
   var inputValue = Request.Form["someId"];
}

您可能还需要设置的AutoPostBack =真正的按钮,所以当你点击按钮,它提交表单。

You might also need to set AutoPostBack="true" on the button, so it submits the form when you click the button.

心连心

这篇关于访问ASP.NET codebehind内的客户端动态控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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