c# - 当前上下文中不存在文本框

标签 c# asp.net umbraco

我的代码有问题。下面是一个简单的例子,没有很多不必要的 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/

相关文章:

C# - 用于此目的的 LINQ 查询

c# - 找不到方法 : 'System.Net.Http.Headers.MediaTypeHeaderValue System.Net.Http.Formatting.JsonMediaTypeFormatter.get_DefaultMediaType()'

Umbraco Contour 多步表单 - 根据用户输入决定下一步

asp.net-mvc - ImageProcessor/Windows Azure 存储问题,返回 403 Forbidden

c# - 如何为 PLINQ 编写线程感知扩展函数?

c# - 为什么模型绑定(bind)器在 ASP.NET MVC 4 中为我的列表实例化空对象?

c# - 在 MVC 4 .NET 类中获取操作 URL

javascript - 使用 jQuery.getJson 获取 Web API

asp.net - 使用 Web API 的动态路由

iphone - 如何从 iPhone 和 WCF 创建 ASP.Net/Umbraco SQL 成员资格的用户?