.NET imagebutton - 它需要代码隐藏吗?

发布于 2024-09-12 04:17:00 字数 2678 浏览 3 评论 0 原文

我创建了一个简单的 .NET 用户控件,用于登录网站上的会员区域(该网站由 Umbraco 提供支持并使用其会员提供商)。

它工作得很好,直到我将登录用户控件上的提交按钮更改为图像按钮。现在它似乎没有提交任何内容 - 它只是在单击时重新加载页面并且不让用户登录。

目前,该控件只是作为标准 .aspx 用户控件插入,而没有编译成 DLL -它适用于标准提交按钮,但不适用于图像按钮。我认为要使图像按钮正常工作需要一个代码隐藏页面,因此我需要编译它。这是对的吗?

您可能知道,我是 .NET 的菜鸟,所以任何建议都会很好。

这是我现在拥有的代码:

<%@ Control Language="C#" AutoEventWireup="true" %>
<asp:Login ID="Login1" runat="server">
    <LayoutTemplate>
        <fieldset>
            <legend>Log in details</legend>
            <div>
                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                    ControlToValidate="UserName" ErrorMessage="User Name is required." 
                    ToolTip="User Name is required." ValidationGroup="ctl00$Login1">*</asp:RequiredFieldValidator>
            </div>
            <div>
                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" 
                    ControlToValidate="Password" ErrorMessage="Password is required." 
                    ToolTip="Password is required." ValidationGroup="ctl00$Login1">*</asp:RequiredFieldValidator>
            </div>
            <div>
                <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." />
            </div>
            <div>
                <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
            </div>
        </fieldset>

        <asp:ImageButton ID="LoginButton" runat="server" src="~/css/img/btn_log-in.png" CssClass="rollover" ValidationGroup="ctl00$Login1" />

        <br />
        <br />
        <div style="background: #fc0">STATUS (to be removed) <asp:LoginStatus ID="LoginStatus1" LoginText="Log in or register" LogoutText="Log out" runat="server" /></div>
    </LayoutTemplate>
</asp:Login>

工作版本中唯一不同的是 ... 它有这个:

<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="ctl00$Login1" />

I've created a simple .NET usercontrol for logging into a members area on a website (the site is powered by Umbraco and uses their membership provider).

It worked fine until I changed the submit button on the log-in usercontrol to an imagebutton. Now it doesn't seem to submit anything - it just reloads the page when clicked and doesn't log the user in.

At the moment, the control is just being inserted as a standard .aspx usercontrol, without being compiled into a DLL - which worked with the standard submit button but doesn't with the imagebutton. I'm thinking that to get an imagebutton working requires a code-behind page, hence I need to compile it. Is this right?

I'm a total noob at .NET as you can probably tell, so any advice would be great.

Here's the code I have now:

<%@ Control Language="C#" AutoEventWireup="true" %>
<asp:Login ID="Login1" runat="server">
    <LayoutTemplate>
        <fieldset>
            <legend>Log in details</legend>
            <div>
                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                    ControlToValidate="UserName" ErrorMessage="User Name is required." 
                    ToolTip="User Name is required." ValidationGroup="ctl00$Login1">*</asp:RequiredFieldValidator>
            </div>
            <div>
                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" 
                    ControlToValidate="Password" ErrorMessage="Password is required." 
                    ToolTip="Password is required." ValidationGroup="ctl00$Login1">*</asp:RequiredFieldValidator>
            </div>
            <div>
                <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." />
            </div>
            <div>
                <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
            </div>
        </fieldset>

        <asp:ImageButton ID="LoginButton" runat="server" src="~/css/img/btn_log-in.png" CssClass="rollover" ValidationGroup="ctl00$Login1" />

        <br />
        <br />
        <div style="background: #fc0">STATUS (to be removed) <asp:LoginStatus ID="LoginStatus1" LoginText="Log in or register" LogoutText="Log out" runat="server" /></div>
    </LayoutTemplate>
</asp:Login>

The only thing different in the version which worked was that insead of the <asp:ImageButton... it had this:

<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="ctl00$Login1" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最初的梦 2024-09-19 04:17:00

简单来说,就是需要重新编译DLL。

原因是,有一个 pagename.designer.cs 文件,当您更改标记中的控件时,该文件会更新,并且该文件是一个仅在重新编译时才会更新的代码文件。

Simply speaking, you need to recompile the DLL.

The reason being that there is a pagename.designer.cs file which gets updated when you change a control in the markup and that is a code file which would only update upon recompilation.

素染倾城色 2024-09-19 04:17:00

如果您仅将标记从 更改为 那么它会失败,因为在编译的 dll 中控件仍然是 LinkBut​​ton。

它不需要代码隐藏,但当然 .NET 会处理控件,这需要您在更改控件类型时重新编译。

LinkBut​​ton 和 ImageButton 的单击事件签名不同。

If you only changed the markup from <asp:linkbutton to <asp:imagebutton then yes it's going to fail because in the compiled dll the control is still a LinkButton.

It doesn't require code-behind but ofcourse .NET does stuff to handle the control which requires you to recompile when you change the type of the control.

The Click Event signatures are different for LinkButton and ImageButton.

只是我以为 2024-09-19 04:17:00

您的代码中有一些不同的地方 - 使用:

CommandName="Login"

.

希望这有帮助。

There is something different in your code - the use of:

CommandName="Login"

.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文