简体   繁体   English

如何从后面的代码中找到更新面板中的客户端元素?

[英]How to find a client side element in an update panel from code behind?

While developing a user control in asp.net I found major difficulty to find html client side element positioned within an update panel. 在asp.net中开发用户控件时,我发现很难找到位于更新面板中的html客户端元素。

The .ascx side contains this: .ascx端包含以下内容:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
   <ContentTemplate>
      <div id="<%=ClientID%>_MyElement">
      </div>
   </ContentTemplate>
</asp:UpdatePanel>

And I need to get a reference to that div in my code-behind. 我需要在我的代码后面获得对该div的引用。

protected void Page_Load(object sender, EventArgs e)
{
    if(IsPostBack)
    {
        //var c = UpdatePanel1.FindControl("<%=ClientID%>_MyElement"); //<-not working.
        //:
        //get some values from c
        //:
    }
}

Now, since there is also Ajax (JavaScript) manipulating that div: 现在,由于还有Ajax(JavaScript)操纵该div:

  1. I cannot set runat="server" for that div. 我不能为该div设置runat =“ server”。
  2. I must use <%=ClientID%>_MyElement as id'ing convention. 我必须使用<%= ClientID%> _ MyElement作为标识约定。

So assuming the div is going to stay as it is - is there any way to fetch reference to it from the code behind? 因此,假设div将保持原样-是否有任何方法可以从后面的代码中获取对它的引用?

Try this: 尝试这个:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
   <ContentTemplate>
      <div id="MyElement" runat="server">
      </div>
   </ContentTemplate>
</asp:UpdatePanel>

in code behind 在后面的代码中

protected void Page_Load(object sender, EventArgs e)
{
    if(IsPostBack)
    {
        var c = MyElement;

    }
}

in javascript 在JavaScript中

var MyElement=document.getElementById('<%= MyElement.ClientID');

Try to access your div from your UpdatePanel . 尝试从UpdatePanel访问div Example: var div = UpdatePanel1.Controls[0]; 示例: var div = UpdatePanel1.Controls[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到面板并通过其背后的代码对其添加控件 - How to find a panel and add control to it from it's code behind 如何从文件后面的代码调用javascript客户端功能 - how to call javascript client side function from code behind file 从更新面板后面的代码中设置面板在更新面板内部的可见性 - Set Visibility of Panel Inside Update Panel From Code behind From An Update Panel 使用“更新”面板从代码后面更改CSS类-不起作用 - Change CSS class from code behind using Update panel - not working 如何访问文本框,使用asp.net Web表单从代码后面的更新面板内标签 - How to access textbox, label inside update panel from code behind using asp.net web forms Update Panel和Anthem如何在客户端隐藏方法名称 - How Update Panel And Anthem hide method Name in client side 使用javascript从代码隐藏到客户端获取DataTable值 - Get DataTable values from code behind to client side using javascript 从客户端脚本在代码隐藏中调用方法 - call a method in code-behind from client side script 从MVC中的代码调用客户端jquery弹出窗口 - Call client side jquery popup from code behind in MVC 如何从asp.net中的代码引发客户端“重置”按钮的点击事件? - How to raise click event of a client side `reset ` button from code behind in asp.net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM