定义Login控件集成按钮的click事件

时间:2012-06-18 09:44:31

标签: c# asp.net visual-studio-2008

Gooday。我有一个登录控件,带有集成的用户文本框和登录按钮。我做了这个小测试,看看它是如何工作的,而且令人惊讶的是,这样做之后:

protected void LoginButton_Click(object sender, EventArgs e)
{
            TextBox userTextBox = (TextBox)Login1.FindControl("UserName");
            userTextBox.Text = "You pressed the button";
}

userTextBox不会更改为“您按下按钮”。为什么? 谢谢。安娜

编辑:当然,这是标记(大部分是由系统在添加Login控件时自动生成的);你会注意到登录中集成了一个LoginButton按钮:

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

<asp:Panel ID="searchPanel" runat="server" DefaultButton="login1$LoginButton">
    <asp:Login ID="Login1" runat="server" 
    FailureText="Logarea a esuat. Reincercati!" LoginButtonText="Logati-va!" 
    PasswordLabelText="Parola:" 
    PasswordRequiredErrorMessage="Trebuie sa introduceti parola." 
    RememberMeText="Tine-ma minte!" TitleText="Logare" 
    UserNameLabelText="Nume de utilizator:" 
    UserNameRequiredErrorMessage="Trebuie sa introduceti numele de utilizator.">
        <LayoutTemplate>
            <table border="0" cellpadding="1" cellspacing="0" 
                style="border-collapse:collapse;">
                <tr>
                    <td>
                        <table border="0" cellpadding="0">
                            <tr>
                                <td align="center" colspan="2">
                                    Logare</td>
                            </tr>
                            <tr>
                                <td align="right">
                                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Nume 
                                    de utilizator:</asp:Label>
                                </td>
                                .........
                            <tr>
                                <td align="right" colspan="2">
                                    <asp:Button ID="LoginButton" runat="server" CommandName="Login" 
                                        Text="Logati-va!" ValidationGroup="Login1" />
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </LayoutTemplate>
</asp:Login>
</asp:Panel>
</asp:Content>

代码隐藏可以解决这个问题:

namespace Cinemax
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

                        //userTextBox.Focus(); // this sets the focus on the username textbox when the page loads
            this.Title = CinemaxConfiguration.SiteName + ": Login";


        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            TextBox userTextBox = (TextBox)Login1.FindControl("UserName");
            TextBox userPassword = (TextBox)Login1.FindControl("Password");

            //Button loginBtn = (Button)Login1.FindControl("LoginButton");
            userTextBox.Text = "You pressed me";

            if (User1.ConnAttempt(userTextBox.Text, userPassword.Text) == 1)
            {
                Session["User"] = userTextBox.Text;
                Response.Redirect("Catalog.aspx");
            }
            else
            {

            }
        }
    }
}

6 个答案:

答案 0 :(得分:1)

TestBox是否为ASP服务器控件,并且是runat = Server?

答案 1 :(得分:1)

我可能错了,但你真的把它绑回到控件上吗。

您正在创建一个从登录控件初始化的新文本框。

然后设置该文本框的文本,但我不认为这会将其绑定回控件本身。

答案 2 :(得分:0)

如果您调试应用程序,是否显示文本已设置?

另外,您是否在Page_Load方法中设置了设置文本框值的内容?你应该把东西放在

if(!IsPostBack)
{
userTextBox.Text = ""
}

语句

答案 3 :(得分:0)

检查您的控件是否为runat = server,并且您已将控件绑定到事件

    this.LoginButton.Click += new System.EventHandler(this.LoginButton_Click);

答案 4 :(得分:0)

在您的登录控制中,检查您是否在userTextBox或任何其他事件中设置了Page_Load的某些值,因为它的值似乎是从其他地方设置的。

答案 5 :(得分:0)

您必须将其绑定到表单。 。