gpt4 book ai didi

javascript - 如何获取控件的 id 会导致 javascript 中的回发

转载 作者:行者123 更新时间:2023-11-29 17:19:30 28 4
gpt4 key购买 nike

如何在 asp.net 中获取导致 PostBack 的控件的 ID。我用

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 - 如何获取控件的 id 会导致 javascript 中的回发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14054783/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com