在PageLoad中 检测引起回传页面的控件
来源:互联网 发布:苏大网络统一身份认证 编辑:程序博客网 时间:2024/04/30 15:06
Determining the Control that Caused a PostBack |
Many times you might need to perform some action on an ASP.NET postback based on the control that caused the postback to occur. Some scenarios for this might include a form with many regions, each having it's own CustomValidator and the ability to perform a postback when a button for the section is clicked. Another scenario might be to set focus back to the control that caused the postback.
There are two parts to the process of determining which control caused the postback. First, you access the __EVENTTARGET element of the form. If you've ever looked a the anatomy of an ASP.NET page (and if you haven't why are you reading this?), you'll notice that a hidden input tag is added to the form named __EVENTTARGET. This hidden input is set to the name of the control that was clicked in the __doPostBack JavaScript function and then the form is submitted. Looking at how the server controls are rendered as HTML tags you can see that the __doPostBack function is called by the controls to cause a postback, passing the name of the control to the function. You can access this hidden input from your code-behind as it is submitted with the form and can be found in the Params or Form collections. This the first part to getting the control that caused a postback. Once you have the name you can get a reference to the control via FindControl and use it as needed.
string ctrlname = page.Request.Params.Get("__EVENTTARGET");if (ctrlname != null && ctrlname != string.Empty){ return this.Page.FindControl(ctrlname);}
This will work, but you'll soon find that something is missing. Although this will work for CheckBoxes, DropDownLists, LinkButtons, etc, this does not work for Button controls. This is where the second part comes in. But why is it that this doesn't work for Buttons? If you again take a look at how the server controls render as HTML, you'll see that the buttons do not call the __doPostBack Javascript function so the __EVENTTARGET is never set. Instead, the Buttons render as simple input type=“submit” tags. All the button does is cause the form to submit. That's it. However, you can still get to it, just in a different way. Since the button (or input) is what causes the form to submit, it is added to the items in the Form collection, along with all the other values from the submitted form. It is important to note, that other input type=“submit” tags on the form are not added to the Form collection unless it was the one that caused the form to submit. If you were to look in the Form collection for anything that is a button then that will be what caused the postback (assuming that it was a button that caused the page to submit). If you first check the __EVENTTARGET, then if that is blank look for a button in the Form collection then you will find what caused the postback. The complete code follows:
public static Control GetPostBackControl(Page page){ Control control = null; string ctrlname = page.Request.Params.Get("__EVENTTARGET"); if (ctrlname != null && ctrlname != string.Empty) { control = page.FindControl(ctrlname); } else { foreach (string ctl in page.Request.Form) { Control c = page.FindControl(ctl); if (c is System.Web.UI.WebControls.Button) { control = c; break; } } } return control;}
This method takes a parameter which is a reference to the Page, it then uses that to look for the control that caused the postback. You can easily use this as follows:
Control c = PageUtility.GetPostBackControl(this.Page);if (c != null){ //...}
You can now do some specific action based on the control that caused the postback or use an earlier post I made to set focus to the control.
- 在PageLoad中 检测引起回传页面的控件
- 如何在页面中检测ActiveX控件的安装情况
- 如何在页面中检测ActiveX控件的安装情况
- Ajax的pageload
- 由Flex页面TextArea控件中回车换行引起的问题
- img控件中src为""时会引起页面多次调用
- 在自定义控件中调用父页面的控件
- A打开B页面,B页面的PageLoad事件第二次不能执行问题解决
- 在页面中查找控件
- 在js中改变页面中控件的值
- pageload重复执行的问题
- 如何在 ActiveX 控件中调用页面的 Jscript 函数
- 在页面中监听用户控件的按钮点击事件
- Android 应用中启动其他应用 && 在页面中寻找其他页面的控件
- 在页面中使用文字编辑控件
- 页面中控件的访问
- 在事件中引用aspx页面repeater控件所包含控件的数据
- 如何在aspx页面中使用ascx控件(用户自定义的一个控件)?
- [转].net企业级架构实战之4——Spring.net下的nHibernate数据访问模板
- JavaScript面向对象编程
- 程序员思维的好处
- Java栈与堆
- VC6.0连接sybase数据库
- 在PageLoad中 检测引起回传页面的控件
- Tomcat 启动:IOException while loading persisted sessions: java.io.EOFException异常
- 恼人的浏览器缓存
- IE编程技巧大全
- [转].net企业级架构实战之5——基于接口的访问层实现
- sqlserver2005和orcale11g相比较,那一个更好啊?
- [转].net企业级架构实战之6——Spring.net管理web services
- 不同语言和操作系统对应的薪水
- Page里的事件 执行的次序