如何獲取導致在asp.net中導致PostBack
的控件的id。我用如何獲取控件的id導致在javascript中回發
document.getElementById("__EVENTTARGET")
但它只給出objectHtmlInputElement
不是一個完整的ID。我怎樣才能得到這隻在JavaScript中,而不是在代碼後面。我希望在回發後專注於該元素。請幫助..
如何獲取導致在asp.net中導致PostBack
的控件的id。我用如何獲取控件的id導致在javascript中回發
document.getElementById("__EVENTTARGET")
但它只給出objectHtmlInputElement
不是一個完整的ID。我怎樣才能得到這隻在JavaScript中,而不是在代碼後面。我希望在回發後專注於該元素。請幫助..
您可以使用此blog提到這個函數來獲取回發控件的ID,這個ID存儲在hiddenfield和使用JS從hiddenfield獲取ID
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
HiddenField1.Value = 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;
}
謝謝你的迴應,但我沒有得到隱藏字段值的JavaScript。我用'var str = $('#HiddenField1')。val();' –
我使用的全局變量'controlid'代替隱藏字段和'control.ClientID'在功能'getPostBackControlName'並將其設置爲通過'ScriptManager.RegisterStartupScript(頁,Page.GetType(),「updateJavaScriptId」全局變量, 「controlid ='」+ getPostBackControlName()+「';」,true);'。這個對我有用。 –
使用document.getElementById('__EVENTTARGET').value
什麼你可以做的是從隱藏字段存儲控制ID從codebehind,並閱讀回發後使用JavaScript –
它不具體的控制它可以是任何東西。 –
是的,我正在談論所有的控制。檢查我的答案 –