本文共 5077 字,大约阅读时间需要 16 分钟。
wpf通过控件名查找控件
In this article, we will look into the JavaScript __doPostBack function and then as a little walkthrough we will write one small program to determine the control that causes the postback
在本文中,我们将研究JavaScript
Let us take a look at the __doPostBack function shown below:
让我们看一下下面显示的__doPostBack函数:
function __doPostBack(eventTarget, eventArgument) { if (!theForm.onsubmit || (theForm.onsubmit() != false)) { theForm.__EVENTTARGET.value = eventTarget;theForm.__EVENTARGUMENT.value = eventArgument;theForm.submit();}}
The __doPostBack function takes two arguments eventTarget, and eventArgument. The eventTarget contains the ID of the control that causes the postback and the eventArgument contains any additional data associated with the control. One thng should be noted that the two hidden fields _EVENTTARGET and _EVENTARGUMENT are automatically declared. the value of the eventTarget and eventArgument are stored in the hidden fields. The two hidden variables can be accessed from code behind using the forms/params collection.
__doPostBack函数采用两个参数eventTarget和eventArgument。 和
protected void Page_Load(object sender, EventArgs e) { string controlName = Request.Params.Get("__EVENTTARGET"); Response.Write("control caused the postback is: " + controlName); }
protected void Page_Load(object sender, EventArgs e) { string controlName = Request.Params.Get("__EVENTTARGET"); Response.Write("control caused the postback is: " + controlName); }
If you see the code generated by Button (or ImageButton) <input type="submit" name="Button1" value="Do PostBack" id="Button1" /> the button control (or the ImageButton control) does not call the __doPostBack function, and hence the _EVENTTARGET will always be empty. However, you can findout the ID of the Button by looping through the form controls collection.
如果您看到由Button(或ImageButton)生成的代码<input type =“ submit” name =“ Button1” value =“ Do PostBack” id =“ Button1” />,则按钮控件(或ImageButton控件)不会调用__doPostBack函数,因此_EVENTTARGET始终为空。 但是,您可以通过遍历表单控件集合来找到Button的ID。
I have written one small function GetPostBackControl that returns control name as string datatype that caused the postback. All the statements inside the function are self explanatory. Comments have been included where-ever necessary.
我编写了一个小函数
public string GetPostBackControl(Page page) { string controlName = page.Request.Params.Get("__EVENTTARGET"); if (controlName == string.Empty) { //Means, some Button or ImageButton has been clicked. foreach (string controlItem in page.Request.Form) { Control c = Page.FindControl(controlItem); if (c is Button) { controlName = c.ID; break; } } } return controlName; }
To use the above function, write the following code in the Page_Load event:
若要使用上述功能,请在Page_Load事件中编写以下代码:
protected void Page_Load(object sender, EventArgs e) { string control = GetPostBackControl(this.Page); Response.Write("Control caused postback:" + control); }
Hope this article helps to all the readers. Happy programming!
希望本文对所有读者有所帮助。 编程愉快!
翻译自:
wpf通过控件名查找控件
转载地址:http://ggqzd.baihongyu.com/