Mimi
Mimi

Reputation: 389

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

Upvotes: 2

Views: 578

Answers (3)

StuartLC
StuartLC

Reputation: 107237

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);
 }

Upvotes: 3

DGibbs
DGibbs

Reputation: 14618

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

Upvotes: 4

Patrick Hofman
Patrick Hofman

Reputation: 156948

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

Upvotes: 6

Related Questions