模板字段控件在页面回发上消失 [英] Template Fields control disappears on page postback

查看:79
本文介绍了模板字段控件在页面回发上消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在工作gridview时遇到一个奇怪的问题。这是问题描述:



我使用的是gridview,它包含boundfields和templatefields。 templatefields包含一些控件,如复选框和图像。我的gridview在最后一个colummn包含一个模板字段。我的要求是在运行时在最后一个模板字段之前添加N nnumber的boundfields。我添加了类似这样的代码:

Hi ,

I am facing a strange problem while working gridview .Here is the problem description:

I am using a gridview which consist of boundfields and templatefields. templatefields contains some controls like check boxes and image. My gridview contains one template field at last colummn. My requirement is to add the N nnumber of boundfields before last templatefield at runtime. I have added the code something like this:

Grid.datasource=ds
grid.columns.insert(index,column)
grid.databind()



现在gridview显示所有boundfields和tempaltefields(其中添加了控件)以及新添加的boundfields。但是模板字段中的控件在页面回发中消失,但是新添加的边界字段正确显示。



我试图禁用gridview viewstate并在onInit / PreRender事件页面上绑定网格,在这种情况下控件是可见的,但这里遇到了一些其他问题。在这种情况下,gridview.rows.count为零,gridview行不可读。



任何人都可以帮我解决这个奇怪的问题。





谢谢

SSthakur



已添加代码块[/ Edit]


Now the gridview displays all the boundfields and tempaltefields(with controls added within it) along with the newly added boundfields. But the controls within templatefield disappears on page postback however newly added boundfields are displayed correctly.

I tried to disable the gridview viewstate and binded the grid on onInit/PreRender event of page in this case controls are visible but here faced some other problem. in this case the gridview.rows.count is zero and gridview rows are not readable.

Can anyone help me solving this strange problem.


Thanks
S.S.thakur

Code block added[/Edit]

推荐答案

这是ASP.net Web表单动态控制的默认行为。因为ASP.net不能使用viewstate记住动态对象。例如,如果您从load事件动态创建文本框,并使用if!Page.IsPostBack等条件将其添加到任何面板,那么第一次在表单中看到TextBox但下次回发时文本框将被销毁。您将在创建该文本框并添加到面板控件时需要执行的每个回发/非回发操作。 GridView中的BoundField控件也发生了同样的事情。所以每次你应该创建那个BoundField并附加到网格时你就可以做到。
It is default behavior for dynamic control for ASP.net web form. Because ASP.net can not remember dynamic object using viewstate. For example if you create a textbox dynamically from your load event and add that to any panel using condition like if !Page.IsPostBack then first time you see the TextBox in your form but next time when it postback then the textbox will be destroyed. What you will do every postback/non postback you need to create that textbox and add to the panel control. Same thing happen here for Your BoundField control in GridView. So you can do is everytime you should create that BoundField and attached to grid.


这篇关于模板字段控件在页面回发上消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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