主页 :
<%@ Register TagPrefix="A" TagName="AA" Src="~/UserControls/ab.ascx" %>
<A:AA id="a1" runtat="server" />
<asp:Button ID="btn" Visible="true" runat="Server" OnClick="Btn_Click" OnClientClick="javascript:PreLoad();" />
<script type="text/javascript">
function PreLoad() {
var empty = '<%= (a1.FindControl("ddl")).ClientID %>';
// the above line giving error as 'ddl is inaccessible due to its protection level'
}
</script>
在“用户控制”页面中:
<%@ ControlLanguage="C#" AutoEventWireup="true" CodeBehind="ab.ascx.cs" inherits="ab.ascx.designer.cs" %>
<asp:DropDownList ID="ddl" runat="server"> </asp:DropDownList>
在ab.ascx.designer.cs中
protected global::System.Web.UI.WebControls.DropDownList ddl;
有人请解释在JavaScript函数PrePre()中访问“ ddl”的简洁明了的正确方法吗?
将标签更改为:
<asp:DropDownList ID="ddl" runat="server" ClientIDMode="static"> </asp:DropDownList>
这意味着将使用您指定的确切ID呈现控件。然后更改您的JavaScript以使用控件的确切ID,并通过以下方式访问它:
document.getElementById("ddl");
或使用jQuery:
$("#ddl");
理想情况下,您也应该将此JavaScript代码也移至外部文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句