简体   繁体   English

尝试更改asp.net中默认login.aspx页内的标签文本

[英]Trying to change the text of a label that is inside the default login.aspx page in asp.net

I am trying to change the text of a label I have created inside the default login.aspx page that is given to you when you create a new website in .net 4.0. 我试图更改在.net 4.0中创建新网站时给您的默认login.aspx页面中创建的标签的文本。 I cannot seem to access this label in any way. 我似乎无法以任何方式访问此标签。 The text is supposed to change when the log in button is clicked if the user is not approved. 如果未批准用户,则单击“登录”按钮时,文本应该会更改。 Here is where the label is made. 这是制作标签的地方。

    <LayoutTemplate>
        <span class="failureNotification">
            <asp:Literal ID="FailureText" runat="server"></asp:Literal>
        </span>
        <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification" 
             ValidationGroup="LoginUserValidationGroup"/>
             <%-- If the account is not approved display an error message --%>
        <asp:Label ID="NotAproved" runat="server" CssClass="failureNotification"></asp:Label>......

I have tried to access it by using FindControl but it never works so I might be doing something wrong. 我试图通过使用FindControl来访问它,但是它永远无法正常工作,因此我可能做错了什么。 Thank you in advance for your help. 预先感谢您的帮助。

EDIT: I found the way to access it in the code behind and here it is in case anyone has a similar question: 编辑:我在后面的代码中找到了访问它的方法,以防万一有人有类似的问题:

    var notApproved = (Label)LoginUser.FindControl("NotApproved");
    notApproved.Text = "Sorry Your Account has not yet Been Approved by an Administrator. Try Again Later.";

The FindControl method will only find server side controls (as in this case) that are direct descendants of the container you are searching. FindControl方法将仅找到作为您搜索容器的直接后代的服务器端控件(在这种情况下)。 From MSDN (I have added emphasis)- 从MSDN(我特别强调)-

Use FindControl to access a control from a function in a code-behind page, to access a control that is inside another container, or in other circumstances where the target control is not directly accessible to the caller. 使用FindControl可以从代码隐藏页中的函数访问控件,可以访问另一个容器内的控件,或者在其他情况下调用者无法直接访问目标控件。 This method will find a control only if the control is directly contained by the specified container; 仅当控件直接包含在指定的容器中时,此方法才会找到该控件; that is, the method does not search throughout a hierarchy of controls within controls. 也就是说,该方法不会在控件内的整个控件层次结构中进行搜索。 For information about how to find a control when you do not know its immediate container, see How to: Access Server Controls by ID. 有关如何在不知道控件的直接容器时查找控件的信息,请参见如何:按ID访问服务器控件。

http://msdn.microsoft.com/en-us/library/486wc64h.aspx http://msdn.microsoft.com/en-us/library/486wc64h.aspx

Generally you should avoid using FindControl and reference the control directly using the ID in order to return the strongly typed object and avoid a tight dependency on a particular control hierarchy, but this isn't possible for controls added within templates as you have probably found. 通常,您应该避免使用FindControl并直接使用ID引用控件,以便返回强类型对象并避免对特定控件层次结构的严格依赖,但是对于您可能发现的模板中添加的控件,这是不可能的。

It seems you have either stumbled upon or worked out the direct descendent requirement by yourself, but I would suggest the following may be safer code- 看来您是自己偶然发现或解决了直接后代要求,但我建议以下代码可能更安全-

var notApproved = LoginUser.FindControl("NotApproved") as Label;
if (notApproved != null)
{
    notApproved.Text = "Sorry Your Account has not yet Been Approved by an Administrator. Try Again Later.";
}

This deals with the NullReferenceException you would get if LoginUser.FindControl("NotApproved") doesn't find anything and returns Null (see the above MSDN link) and also a possible type cast exception where the object found is not a label and cannot be cast to one. 这处理了如果LoginUser.FindControl(“ NotApproved”)没有找到任何东西并返回Null(请参见上面的MSDN链接),并且可能的类型转换异常(如果找到的对象不是标签,并且不能投到一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.net默认login.aspx页缺少图像 - ASP.net default login.aspx page missing images 尝试更改默认情况下使用Asp.Net WebForms模板提供的Login.aspx页的母版时发生运行时错误 - Runtime error when attempting to change the Master Page for the Login.aspx page given by default with the Asp.Net WebForms template ASP.NET Owin OAuth (Google / Facebook) 正在重定向到远程登录页面的默认 login.aspx insead - ASP.NET Owin OAuth (Google / Facebook) is redirecting to default login.aspx insead of remote log in page ASP.Net aspxerrorpath = / Login.aspx - ASP.Net aspxerrorpath=/Login.aspx HTTPS仅适用于一个ASP.NET页面(Login.aspx),HTTP始终用于其余站点 - HTTPS for only one ASP.NET page (Login.aspx), HTTP always for rest of site 将ASP.net MVC项目部署到IIS并重定向到login.aspx - Deploy ASP.net MVC project to IIS and redirect to login.aspx asp.net如何知道不对login.aspx应用安全性? 反向代理给我问题 - How does asp.net know not to apply security to login.aspx? Reverse proxy is giving me issues CSS模板无法与Login.aspx页面一起使用 - CSS template not working with Login.aspx page 应用程序重定向到login.aspx页面 - Application redirects to login.aspx page 页面加载后ASP.net更改标签文本 - ASP.net change Label text after Page Load
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM