如何在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


相关文章
|
3月前
|
开发框架 JavaScript 前端开发
震撼!破解 ASP.NET 服务器控件 Button 执行顺序之谜,颠覆你的开发认知!
【8月更文挑战第16天】在ASP.NET开发中,通过Button控件实现先执行JavaScript再触后台处理的需求十分常见。例如,在用户点击按钮前需前端验证或提示,确保操作无误后再传递数据至后台深度处理。此过程可通过设置Button的`OnClientClick`属性调用自定义JavaScript函数完成验证;若验证通过,则继续触发后台事件。此外,结合jQuery也能达到相同效果,利用`__doPostBack`手动触发服务器端事件。这种方式增强了应用的交互性和用户体验。
45 8
|
16天前
|
弹性计算 异构计算
2024年阿里云GPU服务器多少钱1小时?亲测价格查询方法
2024年阿里云GPU服务器每小时收费因实例规格不同而异。可通过阿里云GPU服务器页面选择“按量付费”查看具体价格。例如,NVIDIA A100的gn7e实例为34.742元/小时,NVIDIA A10的gn7i实例为12.710156元/小时。更多详情请访问阿里云官网。
56 2
|
18天前
|
人工智能 弹性计算 关系型数据库
学生免费领取阿里云服务器一年的方法,以及各种活动
学生可以免费领取阿里云服务器一年,新人可获2核4G,非新人2核2G。访问链接注册并完成学生认证,领取300元无门槛优惠券,购买轻量应用服务器。此外,还有多项活动可赢取实物奖品。
88 2
|
1月前
|
弹性计算 安全 Linux
阿里云国际版使用ping命令测试ECS云服务器不通的排查方法
阿里云国际版使用ping命令测试ECS云服务器不通的排查方法
|
27天前
|
Web App开发 安全 网络安全
tplink虚拟服务器设置方法
为了更全面地理解云服务及其在企业应用中的角色,推荐访问,他们提供了一系列高性能、安全稳定的云服务器解决方案,包括但不限于香港云服务器、高防服务器等,特别适合寻求全球化业务扩展的企业。蓝易云不仅拥有全球化的基础设施布局,还提供针对各种行业定制的全栈云解决方案,助力企业实现云端部署,跨越传统界限,即刻启航云端之旅。
31 0
|
2月前
|
Java
java小工具util系列5:java文件相关操作工具,包括读取服务器路径下文件,删除文件及子文件,删除文件夹等方法
java小工具util系列5:java文件相关操作工具,包括读取服务器路径下文件,删除文件及子文件,删除文件夹等方法
54 4
|
1月前
|
前端开发 Docker 容器
主机host服务器和Docker容器之间的文件互传方法汇总
Docker 成为前端工具,可实现跨设备兼容。本文介绍主机与 Docker 容器/镜像间文件传输的三种方法:1. 构建镜像时使用 `COPY` 或 `ADD` 指令;2. 启动容器时使用 `-v` 挂载卷;3. 运行时使用 `docker cp` 命令。每种方法适用于不同场景,如静态文件打包、开发时文件同步及临时文件传输。注意权限问题、容器停止后的文件传输及性能影响。
131 0
|
1月前
|
弹性计算 数据安全/隐私保护 Windows
阿里云国际版无法远程连接Windows服务器的排查方法
阿里云国际版无法远程连接Windows服务器的排查方法
|
1月前
|
域名解析 弹性计算 安全
无法ping通ECS服务器公网IP的排查方法
无法ping通ECS服务器公网IP的排查方法
|
2月前
|
数据安全/隐私保护
服务器备份的常见方法包括完全备份、增量备份、差异备份和实时备份
服务器备份的常见方法包括完全备份、增量备份、差异备份和实时备份
417 3