简体   繁体   English

.Text在.aspx.cs文件中不起作用

[英].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 这是相关的.aspx代码

<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. 1st .Text命令没有错误消息。 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. 将控件的名称更改为例如eTextBox ,它将起作用。 Also change the reference in the event handler to eTextBox.Text . eTextBox.Text事件处理程序中的引用更改为eTextBox.Text

Another possibility is the use of the this keyword: 另一种可能性是使用this关键字:

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. 包含在EventArgs e作为参数的事件中。

The event args has scope rather than your text box 事件args具有范围而不是文本框

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. 您几乎肯定已经在一个方法中覆盖了文本框变量,该方法将EventArgs e作为方法签名。 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. 使用this来限定你想要类变量e ,或者更好的是,给你的TextBox一个合适的名字,并完全避免这个问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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