如何在asp.net中创建动态html表和文本框 [英] how to create dynamic html table and textbox in asp.net

查看:83
本文介绍了如何在asp.net中创建动态html表和文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个HTML表格,一些文本框和一个提交按钮。我在运行时创建HTML表格和文本框。现在我需要提交按钮上的这些文本框值。

但是当我签入我的c#代码时,它们显示为null或。当我的页面回发时,我的表格和文本框也会自动删除。

i尝试使用Ajax脚本管理器并更新面板控件也可以停止完整回发。



现在我怎样才能在回发后获得动态控制以及如何在c#中获得值。???





提前谢谢。

Hi,
I have a HTML table,some textboxes and a submit button. i am creating HTML table and textboxes at run time.now i need these textboxes value on submit button.
but when i check in my c# code, they show null or "". and when my page do post back, my table and textboxes also remove automatically.
i try to use Ajax script manager and update panel control also to stop full post back.

Now how can i get my dynamic control after post back and how i get values in c#.???


Thanks in advance.

推荐答案

嘿!

只需了解动态html表的概念。然后就可以了。

问题原因:
To elaborate on the above statement, the page is recreated each time it is posted back to the server. In other words, a new instance of the Page class is created and the class variables are set using the values from the ViewState. However during this recreation, the dynamically created controls are no longer available and hence the values are lost in the viewstate.
That's why it's showing null on submit (on postback time).



解决方案

只是你缺少一件事....那就是你需要在Page_Init()或Page_Load()中定义这个代码或方法。要覆盖这种行为,你需要以某种方式在每次回发时使这些控件可用。

喜欢。例如


Solution
Simply You are missing one thing.... That is you need to define this code or method inside the Page_Init() or Page_Load().To override this behavior, you need to somehow make these controls available on each postback.
Like. Ex.

protected void Page_Load(object sender, EventArgs e)
    {
        // Run only once a postback has occured
        if (Page.IsPostBack)
        {
            //entered by the user in the respective textboxes
          //Don't call your dynamic method here
           //don't write here dynamic control creation code
        }
        //Call outside of postback
        CreateDynamicTable(); 
    }


这篇关于如何在asp.net中创建动态html表和文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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