[英].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.