本文介绍了控制不存在。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我有我的设置。渲染页面时,它抛出这个错误:名称'用户名'并不在目前情况下存在。我不明白为什么,因为我的控制是正确的拨打以上。我有一个独立的控制同样的设置,它工作得很好。任何人都可以解释一下吗?

I have my setup below. When rendering the page it throws this error: The name 'UserName' does not exist in the current context. I don't understand why because my control is right above the call. I have this same setup in a separate control and it works just fine. Can anyone explain this?

<asp:TextBox ID="UserName" runat="server" Width="136px"></asp:TextBox>

<asp:CustomValidator ID="cvUserNameOrEmailRequired" ValidationGroup="LoginForm"
    runat="server" CssClass="input-error" ErrorMessage="Username is required"
    ControlToValidate="UserName" Display="Dynamic"
    ClientValidationFunction="UsernameValidateTextBox" ValidateEmptyText="True">
    </asp:CustomValidator>

<script type="text/javascript">
    function UsernameValidateTextBox(source, arguments) {
        if (arguments.Value % 2 == 0) {
            arguments.IsValid = false;
        } else {
            arguments.IsValid = true;
        }
    }
    **//ERROR IS THROWN HERE**
    $("#<%=UserName.ClientID %>").focus(function () {
        $("#<%=cvUserNameOrEmailRequired.ClientID %>").css({ visibility: "hidden" });
    });
</script>

更新

如果我删除此呼吁: $(#&LT;%= UserName.ClientID%GT;)专注(函数(){然后我得到的一样。误差&LT;%= cvUserNameOrEmailRequired.ClientID%GT;

If I remove this call:$("#<%=UserName.ClientID %>").focus(function () { I then get the same error for <%=cvUserNameOrEmailRequired.ClientID %>

在$ C $上面c是&LT内部; ASP:登录&GT; 标签放置外删除错误

The code above is inside a <asp:Login> tag placing it outside removes the error.

更新

我移动&LT之外jQuery的code; ASP:登录&GT; 和错误走了。我用:

I moved the jQuery code outside of the <asp:Login> and the error went away. I used:

$('#<%=LoginForm.FindControl("UserName").ClientID%>').focus(function () {
    $('#<%=LoginForm.FindControl("cvUserNameOrEmailRequired").ClientID%>')
        .css({ visibility: "hidden" });
});

和没有问题。但是,为什么没有在&LT内工作; ASP:登录&GT; 标签

And no problems. But why doesn't it work within the <asp:Login> tag?

推荐答案

登录控制等如转发 GridView的,使用的模板。这需要在控制这些模板标签,就像登录的&LT ; LayoutTemplate模板&GT; ,出了 Page.Controls 列表,并把它们在登录标签的控制名单。所以,你需要在登录控件列表中的控件的引用。

The Login control, among others like Repeaters and GridViews, use templates. This takes the controls in those template tags, like the Login's <LayoutTemplate>, out of the Page.Controls list, and puts them in the Login tag's Controls list. So you need a reference to the control within the Login control's list.

这code使用它通过控制的所有直接子迭代,查找按名称来标识的的FindControl()方法。全code以下明确它施放的目标类型,但你可以转换为更通用的控制,如果它会更容易些,因为你只得到的ClientID属性:

This code uses the FindControl() method which iterates through all the direct children of the control looking for an ID by name. The full code below explicitly casts it to the target type, but you could cast to the more generic Control if it would be easier since you're only getting the ClientID property:

((控制)Login1.FindControl(用户名))。ClientID的

此外,登录控件是,该公司预计将具有特定ID某些控件有点特别,所以不会呈现文字,客户端JavaScript code在登录 LayoutTemplate模板。因此,移动文字&LT;脚本&gt;在样板之外标记。这并不能解决当然引用问题,所以你仍然必须获得与的FindControl一个参考子控件()

Also, the Login control is a little special in that it expects certain controls with specific IDs, so it won't render the literal, client-side JavaScript code in the Login LayoutTemplate. So move the literal <script> tag outside of the template. This doesn't solve the reference problem of course, so you still must get a reference to the child control with FindControl().

<asp:Login ID="Login1" runat="server">
    <LayoutTemplate>
        <asp:TextBox ID="Password" runat="server" Width="136px"></asp:TextBox>
        <asp:TextBox ID="UserName" runat="server" Width="136px"></asp:TextBox>
        <asp:CustomValidator ID="cvUserNameOrEmailRequired" ValidationGroup="LoginForm" runat="server"
            CssClass="input-error" ErrorMessage="Username is required" ControlToValidate="UserName"
            Display="Dynamic" ClientValidationFunction="UsernameValidateTextBox" ValidateEmptyText="True">
        </asp:CustomValidator>
    </LayoutTemplate>
</asp:Login>
<script type="text/javascript">
    function UsernameValidateTextBox(source, arguments) {
        if (arguments.Value % 2 == 0) {
            arguments.IsValid = false;
        } else {
            arguments.IsValid = true;
        }
    }
    $("#<%= ((TextBox)Login1.FindControl("UserName")).ClientID %>").focus(function () {
        $("#<%=((CustomValidator)Login1.FindControl("cvUserNameOrEmailRequired")).ClientID %>").css({ visibility: "hidden" });
    });
</script>

这篇关于控制不存在。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:48