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


相关文章
|
27天前
|
弹性计算 小程序 容灾
2025购买阿里云服务器配置选择方法:企业+个人+学生攻略
2025年阿里云服务器购买省钱攻略,涵盖个人、中小企业及高性能配置推荐。个人用户优选38元轻量或99元ECS,企业用户选199元2核4G服务器,游戏用户适合4核16G或8核32G配置,详情请参考最新活动及攻略。
281 3
|
1月前
|
缓存 监控 前端开发
详述uniapp项目部署于Nginx服务器的配置优化方法。
综上所述,uniapp项目部署于Nginx的优化方法多种多样,应根据实际情况灵活地采取合适的策略。配置后持续监控和调试,适时调整配置以保持最佳性能,并确保随着应用需求和访问模式的变化,服务器配置得到适当的更新和优化。
95 0
|
2月前
|
安全 关系型数据库 网络安全
安全加固:启动PostgreSQL 14服务器SSL加密的方法指南在CentOS 7环境中
通过上述步骤,你可以为PostgreSQL数据库服务器设置SSL加密,从而增加数据在传输中的安全性。确保维持证书的有效性,并且定期更新和管理密钥,以防止未授权访问。
114 0
|
10月前
|
Java
java小工具util系列5:java文件相关操作工具,包括读取服务器路径下文件,删除文件及子文件,删除文件夹等方法
java小工具util系列5:java文件相关操作工具,包括读取服务器路径下文件,删除文件及子文件,删除文件夹等方法
204 9
|
5月前
|
缓存 PHP 数据库
WordPress网站服务器性能优化方法,站长必备。
最后,当你将这些方法组合起来并实施时,您将发现你的WordPress网站性能有了显著的提高。别忘了,这不是一次性的任务,要定期执行,保持你的车(网站)始终在轨道上飞驰。
181 21
|
开发框架 JavaScript 前端开发
震撼!破解 ASP.NET 服务器控件 Button 执行顺序之谜,颠覆你的开发认知!
【8月更文挑战第16天】在ASP.NET开发中,通过Button控件实现先执行JavaScript再触后台处理的需求十分常见。例如,在用户点击按钮前需前端验证或提示,确保操作无误后再传递数据至后台深度处理。此过程可通过设置Button的`OnClientClick`属性调用自定义JavaScript函数完成验证;若验证通过,则继续触发后台事件。此外,结合jQuery也能达到相同效果,利用`__doPostBack`手动触发服务器端事件。这种方式增强了应用的交互性和用户体验。
127 8
|
9月前
|
SQL 存储 关系型数据库
MySQL/SqlServer跨服务器增删改查(CRUD)的一种方法
通过上述方法,MySQL和SQL Server均能够实现跨服务器的增删改查操作。MySQL通过联邦存储引擎提供了直接的跨服务器表访问,而SQL Server通过链接服务器和分布式查询实现了灵活的跨服务器数据操作。这些技术为分布式数据库管理提供了强大的支持,能够满足复杂的数据操作需求。
440 12
|
10月前
|
弹性计算 异构计算
2024年阿里云GPU服务器多少钱1小时?亲测价格查询方法
2024年阿里云GPU服务器每小时收费因实例规格不同而异。可通过阿里云GPU服务器页面选择“按量付费”查看具体价格。例如,NVIDIA A100的gn7e实例为34.742元/小时,NVIDIA A10的gn7i实例为12.710156元/小时。更多详情请访问阿里云官网。
1480 2
|
10月前
|
人工智能 弹性计算 关系型数据库
学生免费领取阿里云服务器一年的方法,以及各种活动
学生可以免费领取阿里云服务器一年,新人可获2核4G,非新人2核2G。访问链接注册并完成学生认证,领取300元无门槛优惠券,购买轻量应用服务器。此外,还有多项活动可赢取实物奖品。
3367 2
|
11月前
|
前端开发 Docker 容器
主机host服务器和Docker容器之间的文件互传方法汇总
Docker 成为前端工具,可实现跨设备兼容。本文介绍主机与 Docker 容器/镜像间文件传输的三种方法:1. 构建镜像时使用 `COPY` 或 `ADD` 指令;2. 启动容器时使用 `-v` 挂载卷;3. 运行时使用 `docker cp` 命令。每种方法适用于不同场景,如静态文件打包、开发时文件同步及临时文件传输。注意权限问题、容器停止后的文件传输及性能影响。
2786 1

热门文章

最新文章