tho*_*vdb 4 asp.net asp.net-ajax
与此问题相关:在回发时,如何检查哪个控件导致Page_Init事件中的回发
如果控件包装在ASP.NET AJAX UpdatePanel中,则变量"control"为空,因为它在AJAX PostBack之后具有不同的ID.是否有解决方案来获取在ASP.NET Ajax UpdatePanel中触发回发的控件?
public static string GetPostBackControlName( Page page ) {
Control control = null;
/**
* First we will check the "__EVENTTARGET" because if the postback is made
* by controls which used the _doPostBack function, it will be available in the Request.Form collection.
*/
string ctrlname = page.Request.Params["__EVENTTARGET"];
if ( !String.IsNullOrEmpty( ctrlname ) ) {
control = page.FindControl( ctrlname );
} else {
/**
* If __EVENTTARGER is null, the control is a button-type and
* need to iterate over the form collection to find it.
*/
Control c = null;
string ctrlStr = null;
foreach ( string ctl in page.Request.Form ) {
if ( ctl.EndsWith( ".x" ) || ctl.EndsWith( ".y" ) ) {
/**
* ImageButtons have an additional "quasi-property" in their ID which identifies
* the mouse-coordinates (X and Y).
*/
ctrlStr = ctl.Substring( 0, ctl.Length - 2 );
c = page.FindControl( ctrlStr );
} else {
c = page.FindControl( ctl );
}
if ( c is Button || c is ImageButton ) {
control = c;
break;
}
}
}
if ( control != null ) {
return control.ID;
}
return string.Empty;
}
Run Code Online (Sandbox Code Playgroud)
请尝试以下方法:
public string GetAsyncPostBackControlID()
{
string smUniqueId = ScriptManager.GetCurrent(Page).UniqueID;
string smFieldValue = Request.Form[smUniqueId];
if (!String.IsNullOrEmpty(smFieldValue) && smFieldValue.Contains('|'))
{
return smFieldValue.Split('|')[1];
}
return String.Empty;
}
Run Code Online (Sandbox Code Playgroud)
上面的方法使用了ScriptManager的隐藏字段.通过使用ScriptManager的UniqueID搜索表单键,可以在服务器上访问其值.隐藏字段中的值采用格式[UpdatePanel UniqueID]|[Postback Control ID].知道了这些信息后,我们就可以检索发起异步回发的控件的ID.它也适用于提交按钮.
| 归档时间: |
|
| 查看次数: |
6842 次 |
| 最近记录: |