0

Gooday. I have a login control, with an integrated user textbox and login button. I did this little test to see how it works, and surprisingly, after doing this:

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

the userTextBox doesn't change to "You pressed the button". Why? Thanks. Anna

EDIT: Sure, here is the markup (most of it is generated automatically by the system when adding the Login control); you will notice a button LoginButton integrated in the Login:

<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>

The code-behind goes liek this:

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
            {

            }
        }
    }
}
4

6 回答 6

1

我可能是错的,但你有没有真正将它绑定回控件。

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

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

于 2012-06-18T09:49:19.553 回答
1

TestBox 是一个 ASP 服务器控件,并且是 runat=Server 吗?

于 2012-06-18T09:47:33.237 回答
0

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

另外,您在 Page_Load 方法中有设置文本框值的内容吗?你应该把东西放在一个

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

陈述

于 2012-06-18T09:49:01.890 回答
0

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

    this.LoginButton.Click += new System.EventHandler(this.LoginButton_Click);
于 2012-06-18T09:50:37.227 回答
0

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

于 2012-06-18T09:51:33.327 回答
0

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

于 2012-06-18T10:08:02.310 回答