动态获取单击的服务器端控件的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 p_w_picpath 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" οnchange="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 p_w_picpath button, one dropdown list, one checkbox and two p_w_picpath 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;

}

}

转载于:https://blog.51cto.com/linzheng/1081851

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

  1. c#判断右键菜单(ContextMenuStrip)是从哪个控件弹出来的方法

    在前面有一篇文章中,逍遥一生已经介绍了如何在c#窗体程序中给控件添加右键菜单以及给不同的子菜单添加不同的事件: c#窗体(winform)程序给控件添加右键菜单及给不同子菜单添加不同事件的方法 在这篇 ...

  2. 17Web服务器端控件

    Web服务器端控件 Web服务器端控件 ASP.Net提供了两类服务器端控件:Html服务器端控件和Web服务器端控件.由于Web服务器端控件功能更强大,和Windows应用程序的控件使用方法类似,容 ...

  3. Ajax Control Toolkit 32个服务器端控件

    1. Accordion [功能概述]Accordion可以让你设计多个panel 并且一次只显示一个Panel .在页面上的显示效果就像是使用了多个CollapsiblePanels只不过每一次只展 ...

  4. Ajax之三 Ajax服务器端控件

    视频课:https://edu.csdn.net/course/detail/27107 [学习目标] 理解并掌握ScriptManager控件的使用 了解ScriptManager控件的使用 理解并 ...

  5. Ajax Control Toolkit 34个服务器端控件

    1. Accordion [功能概述] Accordion可以让你设计多个panel 并且一次只显示一个Panel .在页面上的显示效果就像是使用了多个CollapsiblePanels只不过每一次只 ...

  6. .net学习之母版页执行顺序、jsonp跨域请求原理、IsPostBack原理、服务器端控件按钮Button点击时的过程、缓存、IHttpModule 过滤器...

    1.WebForm使用母版页后执行的顺序是先执行子页面中的Page_Load,再执行母版页中的Page_Load,请求是先生成母版页的控件树,然后将子页面生成的控件树填充到母版页中,最后输出 2.We ...

  7. Asp.net服务器端控件CheckBoxList的使用心得

    1.用JavaScript获得Asp.net服务器端控件CheckBoxList选中得值 Asp.net服务器端控件CheckBoxList在客户端没有生成value值,所以就想在客户端通过JS获得选 ...

  8. 关于服务器端控件的attributes属性的奇怪问题

    我在做一个页面时,通过attributes为服务器端控件添加了一个客户端事件 UploadButton.Attributes.Add("onclick", "ShowBa ...

  9. 细数Ajax Control Toolkit 34个服务器端控件

    1. Accordion [功能概述] Accordion可以让你设计多个panel 并且一次只显示一个Panel .在页面上的显示效果就像是使用了多个CollapsiblePanels只不过每一次只 ...

最新文章

  1. NSTimer用法,暂停,继续,初始化
  2. HashMap是如何实现快速存取的
  3. 网络安全04_互联网发展史_网线+网卡+协议栈_中继器_集线器_网桥_路由器_AC/AP_防火墙_流控_家庭网络_小型创业公司网络_园区网_政务网络_数据中心网络拓扑_电信网/互联网_Mac地址
  4. 尽力去帮助一个陌生人
  5. 关于c++跟java区别的几个总结
  6. 禁用应用中Android系统的导航栏(特别是平板)
  7. 少走弯路,给3~5年程序员的唯一一条建议
  8. OpenShift 4 Tekton - 用Webhook实现CI/CD
  9. #自定义多级菜单_怎么搞定排版中的多级列表问题?
  10. 很口语I'll be back
  11. Dynamic Set Up the Web Reference Url To WebService
  12. 华佳慧科技:OSN500设备ERPS相切环组网介绍
  13. js实现数字转换大写金额
  14. MSF的辅助扫描模块(信息搜集)
  15. Java实习生常规技术面试题每日十题Java基础(七)
  16. 清除浮动的四种样式写法
  17. vlookup匹配 匹配结果错误_明明有数据,为什么我的VLOOKUP总是匹配不出来?
  18. 微信python天天学_用python玩微信
  19. Unity3D战争迷雾效果
  20. 2022安全员-B证考试题库及在线模拟考试

热门文章

  1. 微信小程序开发之scroll-view上拉加载数据实现
  2. Python 中 pass的使用
  3. c++ 出现1.#IND、1.#INF
  4. 【Linux】22.当前运行的docker修改环境后,想在本地保存为镜像的方法
  5. MySQL 优化之 index merge(索引合并)
  6. 初学Java Web(4)——Servlet学习总结
  7. 在Qt调用OpenCV库编写GUI程序
  8. 从“架构师书单”讲开去
  9. 利用Matlab优化工具箱求解旅行商最短路径问题
  10. Scala基础教程(五):函数、闭包