简体   繁体   中英

.Text is not working in .aspx.cs file

Here is some portion of my code :

//adding parameters with value cmd.Parameters.AddWithValue("@Username", d.Text); cmd.Parameters.AddWithValue("@Password", e.Text);

Here is the related .aspx code

<tr align= "center"><td class="style1">Username : 
    <asp:TextBox ID="d" runat="server"></asp:TextBox>
                </td></tr>
<tr align ="center"><td class="style3">Password : 
    <asp:TextBox ID="e" TextMode="password" runat="server"></asp:TextBox>
                </td></tr>

There is no error message for 1st .Text command. But for the 2nd line there is an error message :

Error   1   'System.EventArgs' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)

I don't understand why.

I guess your event handler and control name are the same.

Change the name of your control to for example eTextBox and it will work. Also change the reference in the event handler to eTextBox.Text .

Another possibility is the use of the this keyword:

private void Button1_Click(object sender, EventArgs e)
{
    // use this.
    string s = this.e.Text;
}

I suspect that:

 cmd.Parameters.AddWithValue("@Username", d.Text);
 cmd.Parameters.AddWithValue("@Password", e.Text);

Is contained within an event which has EventArgs e as a parameter.

The event args has scope rather than your text box

Call your textbox something else. It's poor practice to give your controls such ambiguous names

You almost certainly have overridden the text box variable in a method which has EventArgs e as a method signature. Use this to qualify that you want the class variable e , or better still, give your TextBox a decent name and avoid the issue altogether.

 void SomeBtn_Click(Object sender,
                       EventArgs e)
 {
       // Other code ....
       cmd.Parameters.AddWithValue("@Password", this.e.Text);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM