[英]Determining which control caused PostBack
我有這個aspx:
<asp:ImageButton ID="check" runat="server" ImageUrl="../img/process.png" OnClick="check_Click" CausesValidation="false" UseSubmitBehavior="false"/>
現在在Page_Load
我想確定是由於check
引起的PostBack
還是沒有,所以我用這個代碼跟着這個問題的方法:
if(FindControl(Page.Request.Params.Get("__EVENTTARGET"))!=check)//if not caused by "check"
//do something
但是Page.Request.Params.Get("__EVENTTARGET")
是空的。(我在UpdatePanel
使用我的ImageButton
)
我怎樣才能實現目標?
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
return;
Control control = null;
string controlName = Page.Request.Params["__EVENTTARGET"];
if (!String.IsNullOrEmpty(controlName))
{
control = Page.FindControl(controlName);
}
else
{
string controlId;
Control foundControl;
foreach (string ctl in Page.Request.Form)
{
if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
{
controlId = ctl.Substring(0, ctl.Length - 2);
foundControl = Page.FindControl(controlId);
}
else
{
foundControl = Page.FindControl(ctl);
}
if (!(foundControl is Button || foundControl is ImageButton)) continue;
control = foundControl;
break;
}
}
Label1.Text = control.ID; // label1 must be in UpdatePanel
}
試試這個:
Control ctrl = null;
string target = Page.Request.Params.Get("__EVENTTARGET");
if (!String.IsNullOrEmpty(target))
ctrl = page.FindControl(target);
if(ctrl == check){
//check is the control that caused postback
}
**更新**
好吧,結果ImageButtons功能有點不同。 用於標記:
<asp:ImageButton onClientClick="setTarget(this.id)" ID="check" runat="server" ImageUrl="../img/process.png" OnClick="check_Click" CausesValidation="false" UseSubmitBehavior="false"/>
<asp:HiddenField ID="targetId" runat="server" />
現在創建一個javascript函數,它將使用啟動PostBack的字段的ID填充我們的隱藏字段:
function SetSource(id)
{
var targetId=
document.getElementById("<%=targetId.ClientID%>");
targetId.value = id;
}
最后我們在PostBack中檢查它:
Control ctrl = null;
if (Request.Form[targetId.UniqueID] != null &&
Request.Form[targetId.UniqueID] != string.Empty)
{
ctrl = Page.FindControl(Request.Form[targetId.UniqueID]);
}
if(ctrl == check){
//check is the control that caused postback
}
參考: http : //www.aspsnippets.com/Articles/How-to-find-the-control-that-caused-PostBack-in-ASP.Net.aspx
由於這是一個更新面板,然后嘗試通過ScriptManager
獲取值,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
var updatePanelControlIdThatCausedPostBack = String.Empty;
var scriptManager = ScriptManager.GetCurrent(Page);
if (scriptManager != null)
{
var smUniqueId = scriptManager.UniqueID;
var smFieldValue = Request.Form[smUniqueId];
if (!String.IsNullOrEmpty(smFieldValue) && smFieldValue.Contains("|"))
{
updatePanelControlIdThatCausedPostBack = smFieldValue.Split('|')[1];
}
}
if (!String.IsNullOrEmpty(updatePanelControlIdThatCausedPostBack))
{
// Do something with control ID value that caused UpdatePanel postback here
}
}
如果控件導致PostBack
其ID
在請求集合中將為非null
, Form.Request[controlID]
。
對於Image
,請求集合中有兩個項目,一個用於x坐標,另一個用於y坐標,用於單擊鼠標的圖像。
所以:
if(Form.Request["Img1.x"] != null)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.