如何在page_load方法判断是服务器端控件引发的page_load方法

简介:

动态获取单击的服务器端控件的id值

private string getPostBackControlName()
{
Control control=null;
string ctrlname = Page.Request.Params["__EVENTTARGET"];
if (ctrlname != null && ctrlname != String.Empty)
{
control = Page.FindControl(ctrlname);
}
else
{
Control c;
foreach (string ctl in Page.Request.Form)
{
if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
{
c = Page.FindControl(ctl.Substring(0, ctl.Length - 2));
}
else
{
c = Page.FindControl(ctl);
}
if (c is System.Web.UI.WebControls.Button ||
c is System.Web.UI.WebControls.ImageButton)
{
control = c;
break;
}
}
}
if (control != null)
return control.ID;
else
return string.Empty;
}

 

 

ps:

There are two types of controls which make post back in ASP.NET. One button type control like image button, button (whose type is “submit”), and another type use javascript function “_doPostBack” for the post back.

If post back control is button type then it will be added in the Request.Form collection means if there are two button in a page named button1 and button2 and if button1 make post back then only button1 will be in Request.Form Collection not button2 (but Request.Form collection can contains other server controls also like if page contains few textbox, dropdown list etc.) and if post back made by the button2 then only button2 will be available in Request.Form collection not button1(with other server control as I discuss earlier).

So if you want to catch which button type control made a post back in page load event, you have to just iterate the Request.Form collection.

I’ll show you demo latter in this article.

Another category of server control who make post back use the client side javascript function _doPostBack like if we made autopostback true for dropdown list, radio button etc.

 

If you look view source you will found _doPostBack javascript function.

 

function __doPostBack(eventTarget, eventArgument)

{

if (!theForm.onsubmit || (theForm.onsubmit() ! = false ))

{

theForm.__EVENTTARGET.value = eventTarget;

theForm.__EVENTARGUMENT.value = eventArgument; theForm.submit();

}

}

 

As you can see this function take two arguments “eventTarget” and “eventArgument”. “eventTarget” used for the control ID who is responsible for the postback and “eventArgument” used for the additional information about the control.

 

If you look at the view source you will also found these two hidden fields.

 

<input type= "hidden" name= "__EVENTTARGET" id= "__EVENTTARGET" value= "" />

<input type= "hidden" name= "__EVENTARGUMENT" id= "__EVENTARGUMENT" value= "" />

 

Lets add one dropdown server control in page and make autopostback true also add some dummy data. If you look at the view source you will found

 

<select name="DropDownList1" onchange="javascript:setTimeout('__doPostBack(\'DropDownList1\',\'\')', 0)" id="DropDownList1">

<option value="1">abc</option>

<option value="2">xyz</option>

 

</select>

 

ASP.NET engine automatically add onchange event and call the _doPostBack function and pass the appropriate parameter.

 

_doPostBack function first set the value of those hidden field and then submit the form. So if you want to know whether this dropdown list make post back or not you have to just check the value of “__EVENTTARGET” hidden field from the form parameter collection. I’ll show you code as well.

 

In my example I am adding two buttons, one image button, one dropdown list, one checkbox and two image buttons. I’ll try to print the control name which makes the post back. All I’ll try to find in page load event. So be ready for the ride.

 

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (IsPostBack)

Response.Write(getPostBackControlName());

}

 

 

private string getPostBackControlName()

{

Control control = null;

//first we will check the "__EVENTTARGET" because if post back made by the controls

//which used "_doPostBack" function also available in Request.Form collection.

string ctrlname = Page.Request.Params["__EVENTTARGET"];

if (ctrlname != null && ctrlname != String.Empty)

{

control = Page.FindControl(ctrlname);

}

// if __EVENTTARGET is null, the control is a button type and we need to

// iterate over the form collection to find it

else

{

string ctrlStr = String.Empty;

Control c = null;

foreach (string ctl in Page.Request.Form)

{

//handle ImageButton they having an additional "quasi-property" in their Id which identifies

//mouse x and y coordinates

if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))

{

ctrlStr = ctl.Substring(0, ctl.Length - 2);

c = Page.FindControl(ctrlStr);

}

else

{

c = Page.FindControl(ctl);

}

if (c is System.Web.UI.WebControls.Button ||

c is System.Web.UI.WebControls.ImageButton)

{

control = c;

break;

}

}

}

return control.ID;

 

}

}

本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1081851


相关文章
|
1月前
|
安全 网络安全 API
163邮箱IMAP服务器设置方法
```markdown 使用IMAP协议同步163邮箱:登录邮箱→设置→账户→IMAP/SMTP→开启服务→配置服务器(imap.163.com:993, SSL/TLS)→设置用户名和密码→保存并在邮件客户端添加账号。确保多设备邮件同步,定期更新设置。[≤240字符] ```
|
1月前
|
弹性计算 安全 Linux
新手一键开服!阿里云3分钟成功幻兽帕鲁联机服务器流程方法
新手一键开服!阿里云3分钟成功幻兽帕鲁联机服务器流程方法,如何自建幻兽帕鲁服务器?基于阿里云服务器搭建幻兽帕鲁palworld服务器教程来了,一看就懂系列。本文是利用OOS中幻兽帕鲁扩展程序来一键部署幻兽帕鲁服务器,阿里云百科分享官方基于阿里云服务器快速创建幻兽帕鲁服务器教程:
|
1月前
|
Windows
LabVIEW中ActiveX控件、ActiveX服务器和类型库注册
LabVIEW中ActiveX控件、ActiveX服务器和类型库注册
31 4
|
1月前
|
存储 缓存 负载均衡
优化服务器响应时间的方法如下
【4月更文挑战第25天】
45 5
|
1月前
|
弹性计算 Ubuntu Windows
阿里云自建《幻兽帕鲁Palworld》多人游戏专属服务器,搭建方法分享
对于《幻兽帕鲁》的忠实粉丝来说,与好友一同在游戏中探险、生存无疑增加了更多的乐趣。而为了实现这一愿望,搭建一个专属的多人游戏服务器就显得尤为重要。今天,我将为大家带来一篇极简教程,教您如何在三次点击内,轻松搭建《幻兽帕鲁》的专属服务器。
|
1月前
|
弹性计算 安全 Linux
ECS最省钱的购买方法,从业务出发
随着云计算技术的发展,越来越多的个人和企业开始使用云服务器ECS。但是,选择适合自己业务和需求的云服务器ECS并不容易,需要考虑多个因素。本文将介绍如何选择适合自己的云服务器ECS,并提供一些工作中总结的实用的建议。
29854 10
|
1月前
|
弹性计算 Ubuntu Linux
Palworld/幻兽帕鲁游戏多人组队!Palworld/幻兽帕鲁联机服务器搭建方法分享,超容易
想要和好友一起畅游幻兽帕鲁的世界吗?别再为联机而烦恼,因为搭建自己的服务器其实并不难!今天,就为大家带来一篇超详细的幻兽帕鲁服务器搭建教程,保证让你轻松上手!
|
1月前
|
弹性计算 运维 安全
带你读《从基础到应用云上安全航行指南》——来上课!一文掌握守住ECS网络安全的最佳方法(2)
带你读《从基础到应用云上安全航行指南》——来上课!一文掌握守住ECS网络安全的最佳方法(2)
49 2
|
1月前
|
弹性计算 Linux 数据安全/隐私保护
2024年幻兽帕鲁服务器搭建,全网最简单的方法
基于阿里云搭建幻兽帕鲁服务器超简单方法,全自动一键部署,3分钟即可成功创建一台Palworld专属服务器,成本仅需26元,阿里云百科分享2024年新版基于阿里云搭建幻兽帕鲁服务器教程
|
1月前
|
弹性计算 Linux 数据安全/隐私保护
【免费搭建】幻兽帕鲁服务器,阿里云免费搭建Palworld的方法
【免费搭建】幻兽帕鲁服务器,阿里云免费搭建Palworld的方法,阿里云幻兽帕鲁服务器免费搭建方案,先在阿里云高校计划「云工开物」活动领取学生专享300元无门槛代金券,幻兽帕鲁专用服务器4核16G配置26元1个月、149元半年,直接使用这个无门槛300元代金券抵扣即可免费搭建幻兽帕鲁服务器。阿里云百科分享详细免费搭建幻兽帕鲁Palworld服务器详细流程
119 1