2013年11月11日 星期一

取得PostBack Control

取得PostBack Control

C#

  1. public Control GetPostBackControl(Page page)
  2.  
  3.         {
  4.  
  5.             Control control = null;
  6.  
  7.             string ctrlname = page.Request.Params.Get("__EVENTTARGET");
  8.  
  9.             if (ctrlname != null && ctrlname != string.Empty)
  10.  
  11.             {
  12.  
  13.                 control = page.FindControl(ctrlname);
  14.  
  15.             }
  16.  
  17.             else
  18.  
  19.             {
  20.  
  21.                 foreach (string ctl in page.Request.Form)
  22.  
  23.                 {
  24.  
  25.                     Control mycontrol = page.FindControl(ctl);
  26.  
  27.                     if (mycontrol is System.Web.UI.WebControls.Button)
  28.  
  29.                     {
  30.  
  31.                         control = mycontrol;
  32.  
  33.                         // This gives you ID of which button caused postback
  34.  
  35.                         Response.Write(mycontrol.ID);
  36.  
  37.                         break;
  38.  
  39.                     }
  40.  
  41.                 }
  42.  
  43.             }
  44.  
  45.             return control;
  46.  
  47.         }
  48.  
  49.  
  50.  
  51.  
  52.  
  53. protected void Page_Load(object sender, EventArgs e)
  54.  
  55. {
  56.  
  57.             Control a = GetPostBackControl(this.Page);
  58.  
  59. }
  60.  
Reference: http://www.codeproject.com/Questions/123490/_EVENTTARGET-empty

沒有留言:

張貼留言