[英]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:
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.