繁体   English   中英

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

[英].Text is not working in .aspx.cs file

这是我的代码的一部分:

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

这是相关的.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>

1st .Text命令没有错误消息。 但是对于第二行,有一条错误消息:

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

我不明白为什么。

我猜您的事件处理程序和控件名称是相同的。

将控件的名称更改为例如eTextBox ,它将起作用。 eTextBox.Text事件处理程序中的引用更改为eTextBox.Text

另一种可能性是使用this关键字:

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

我怀疑:

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

包含在EventArgs e作为参数的事件中。

事件args具有范围而不是文本框

将其他文本框称为文本框。 给你的控件这么模糊的名字是不好的做法

您几乎肯定已经在一个方法中覆盖了文本框变量,该方法将EventArgs e作为方法签名。 使用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