我的代码有问题。下面是一个简单的例子,没有很多不必要的 html 标签。只有重要的事情,才能显现出问题。
当我进入页面时,我收到以下错误:
Error creating usercontrol (usercontrols/own/profilEdit.ascx)
c:\inetpub\wwwroot\umbraco\usercontrols\own\profilEdit.ascx(705): error CS0103: The name 'satbCountry' does not exist in the current context
此错误与 <script>
有关代码(无论如何,这个js代码是正确的,在另一个子页面上工作得很好,但是当这个代码在页面上时,它会崩溃)。为什么我收到此消息?
这里的代码:
<asp:View ID="vSpecialist" runat="server">
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
(..)
<asp:ListView runat="server" ID="lvAddressess" ItemPlaceholderID="phAddress" OnItemDataBound="lvAddressess_ItemDataBound">
<LayoutTemplate>
<asp:PlaceHolder ID="phAddress" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<script>
var input = document.getElementById('<%=satbCountry.ClientID %>');
var options = {
types: ['(regions)']
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
</script>
<asp:TextBox CssClass="textbox" type="text" runat="server" ID="satbCountry"></asp:TextBox>
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
</asp:View>
最佳答案
我认为这是因为satbCountry
在模板中定义(即可以重复多次)。获取clientID
要使用此控件,您需要使用服务器端代码找到每个实例(例如使用 FindControl("satbCountry")
,然后获取客户端 ID。
简而言之,您需要删除 document.getElementById('<%=satbCountry.ClientID %>');
加载页面,然后替换为
document.getElementById('<%# Container.FindControl("satbCountry").ClientID %>');
关于c# - 当前上下文中不存在文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13441270/