如何在asp.net中获取导致PostBack
的控件的id。我用
document.getElementById("__EVENTTARGET")
但它仅提供objectHtmlInputElement
不是完整的ID。我如何才能在javascript中获取此信息,而不是在后面的代码中。我想在回发后关注那个元素。请帮忙..
答案 0 :(得分:4)
您可以使用此blog中提到的此函数来获取回发控件ID,将此ID存储在hiddenfield中并使用JS从隐藏字段中获取该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;
}
答案 1 :(得分:1)
使用document.getElementById('__EVENTTARGET').value