javascript - 如何访问由javascript动态创建的代码隐藏中的控件值

标签 javascript asp.net code-behind

如何访问由 JavaScript 动态创建的控件(在 CodeBehind 中)的值?

我使用以下代码动态创建了控件:

var counter = 0;
var words;
var foo;//span tag 

function add(i) {
    var counter = 0;
    var words;
    var foo;//span tag asp in page where the controls to be added

    if (i == 'ad') {
        counter++;
        //Create an input type dynamically.
        foo = document.getElementById("dynamic")
        tbnam = document.createElement("input")  //textbox
        tbdes = document.createElement("input")  //textbox
        lbnam = document.createElement("Label")
        lbdes = document.createElement("Label")
        before = document.createElement('br')
        after = document.createElement('br')

        //Assign different attributes to the element.
        wordsnam = document.createTextNode("Item")
        wordsdes = document.createTextNode("Descrip")
        tbnam.setAttribute("type", "TextBox");
        tbdes.setAttribute("type", "TextBox");
        tbnam.setAttribute("Id", "tbdynamicnam" + counter);
        tbdes.setAttribute("Id", "tbdynamicdes" + counter);
        lbnam.setAttribute("Id", "lbdynamicnam" + counter);
        lbdes.setAttribute("Id", "lbdynamicdes" + counter);
        before.setAttribute("Id", "bf" + counter);
        after.setAttribute("Id", "af" + counter);

        lbnam.appendChild(wordsnam)
        lbdes.appendChild(wordsdes)

        //Append the element in page (in span).

        foo.appendChild(before);
        foo.appendChild(lbnam);
        foo.appendChild(tbnam);
        foo.appendChild(lbdes);
        foo.appendChild(tbdes);
        foo.appendChild(after);            
    }
}

最佳答案

提交表单后,它将在 Request.Form collection 中,您可以在 Request 对象中最轻松地访问它,该对象将检查所有集合(QueryString、Form、Cookies 和 ServerVariables)

JavaScript:

tbnam = document.createElement("input")  //textbox       
tbnam.setAttribute("type", "TextBox");       
tbnam.setAttribute("Id", "tbdynamicnam" + counter);
tbnam.setAttribute("name", "tbdynamicnam" + counter);

代码隐藏:

string newval = Request["newelementname"];

如下所述 James Montagne ,您将需要一个表单元素名称。

关于javascript - 如何访问由javascript动态创建的代码隐藏中的控件值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10090439/

相关文章:

javascript - jquery 表单选择器单选按钮和复选框

c# - 每隔几分钟在 global.asax 中执行一次方法

c# - 需要有关 RSS Feed 和 wordpress 的帮助

wpf - 使用 min(width, height)/2 作为半径在 WPF 中画一个圆

c# - 如何在代码隐藏中从 GridView 访问选定的 Boundfield 值

javascript - 添加高亮效果,css float

javascript - 如何以编程方式单击 TinyMCE 工具栏的按钮?

javascript - 页面加载时只加载一个 JavaScript 函数。

asp.net - VS 2013 ASP.NET调试时无法修改代码

wpf - 如何从 ItemsControl 的 Item 后面的代码访问 ItemsControl 的 ItemsSource 元素?