我查了其他問題,但沒有一個解決方案適用于這種情況。我試圖洗掉 Button1_Click 函式并再次添加它,我試圖重命名它。在設計中右鍵單擊并查看代碼會將我帶到同一個檔案,因此檔案已正確連接。
有趣的是,它以前是有效的。起初我收到錯誤:'default_aspx' 不包含 'TextBox1_TextChange' 的定義。我洗掉了OnTextChange=TextBox1_TextChange然后洗掉了 受保護的 void TextBox1_TextChange(object sender, EventArgs e) {}
我重新編譯并開始接收“default_aspx”不包含“Button1_Click”的定義
代碼如下:
默認.aspx.cs*
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace APP_FullStack_C.Sharp_.Net_MSSQL
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP\SQLEXPRESS;Initial Catalog=StudentRecords;Integrated Security=True");
con.Open();
// Sql connection string
SqlCommand comm = new SqlCommand("Insert into dbo.StudentInfo_Tab values('" int.Parse(TextBox1.Text) "','" TextBox2.Text "','" DropDownList1.SelectedValue "','" double.Parse(TextBox3.Text) "','" TextBox4.Text "')", con);
comm.ExecuteNonQuery();
con.Close();
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully Inserted');", true);
LoadRecord();
}
// To show inserted values in the grid view
void LoadRecord()
{
SqlCommand comm = new SqlCommand("select * from StudentInfo_Tab", con);
SqlDataAdapter d = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
d.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
默認.aspx*
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="APP_FullStack_C.Sharp_.Net_MSSQL._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div>
<div style="font-size:x-large; height: 51px;" align="center" > Student Info Manage Forms</div>
<table class="nav-justified">
<tr>
<td style="width: 435px"> </td>
<td class="modal-sm" style="width: 186px">Student ID</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Font-Size="Medium" ></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 435px"> </td>
<td class="modal-sm" style="width: 186px">Student Name</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Font-Size="Medium" ></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 435px"> </td>
<td class="modal-sm" style="width: 186px">Address</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="width: 435px"> </td>
<td class="modal-sm" style="width: 186px">Age</td>
<td>
<asp:TextBox ID="TextBox3" runat="server" Font-Size="Medium" ></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 435px; height: 20px"></td>
<td class="modal-sm" style="width: 186px; height: 20px">Contact</td>
<td style="height: 20px">
<asp:TextBox ID="TextBox4" runat="server" Font-Size="Medium"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 435px; height: 20px;"></td>
<td class="modal-sm" style="width: 186px; height: 20px;"></td>
<td style="height: 20px"></td>
</tr>
<tr>
<td style="width: 435px"> </td>
<td class="modal-sm" style="width: 186px"> </td>
<td>
<asp:Button ID="Button1" runat="server" BackColor="#E1E1E8" Font-Bold="True" ForeColor="Black" OnClick="Button1_Click" Text="Insert" Width="79px" />
</td>
</tr>
<tr>
<td style="width: 435px"> </td>
<td class="modal-sm" style="width: 186px"> </td>
<td>
<asp:GridView ID="GridView1" runat="server" Width="538px">
</asp:GridView>
</td>
</tr>
</table>
<br />
</div>
</asp:Content>
我對 C# 和 .Net 非常陌生。任何幫助表示贊賞。提前致謝!
編輯:我認為這個問題是因為反斜杠。添加了錯誤的圖片。洗掉反斜杠會洗掉錯誤,但當然洗掉它不是一個選項。
uj5u.com熱心網友回復:
我只想洗掉按鈕點擊標記:
<asp:Button ID="Button1" runat="server"
BackColor="#E1E1E8" Font-Bold="True" ForeColor="Black"
OnClick="Button1_Click" Text="Insert" Width="79px" />
所以,在上面,洗掉 onclick 事件。例如;
<asp:Button ID="Button1" runat="server"
BackColor="#E1E1E8" Font-Bold="True" ForeColor="Black"
Text="Insert" Width="79px" />
此時,錯誤可能會消失。
但是,如果沒有,則在上述頁面上右鍵單擊-> 查看代碼。在代碼中,找到按鈕單擊事件的代碼存根。注釋掉(不要洗掉)。
現在嘗試頁面。它應該可以作業 - 但當然那個按鈕不起作用。
此時編譯錯誤消失了嗎?
然后在設計器中 - 雙擊該按鈕。它將為您重新創建事件代碼。您現在可以將之前注釋掉的代碼剪切/粘貼/移動到該代碼中。Humpty dumpty 現在應該沒問題了,所有國王的男人在這種情況下看起來都可以修復 humpty dumpty!
編輯:================================================ ========
哦,哎呀,再看看錯誤訊息——它是表單上文本框 1 的事件存根。這個:
<asp:TextBox ID="TextBox1" runat="server" Font-Size="Medium" ></asp:TextBox>
在后面的代碼中,有一個 TextBox1 的代碼存根,稱為:
TextBox1_TextChange
所以錯誤資訊實際上很清楚,而且是正確的。進入后面的代碼并找到 TextChange 事件。你可以注釋掉。現在回到設計器,顯示文本框的屬性表。
例如這個:

Double click in that area where the arrow points. you jump to code editor. It should place your cursor in the correct code stub. If it created a new one, then copy code from the existing Text changed event in your code.
My bad, my sorry - did not read that question close, and I thought this was a button issue, but it clearly was a text box changed event issue.
That code stub will not fire unless the text box has a autopostback=true, so it not all that clear what was supposed to occur.
Anyway, look for a code behind text changed event for text box 1.
uj5u.com熱心網友回復:
使用 2 個反斜杠代替單個反斜杠。變成
資料源=DESKTOP-KJ3D7HA\SQLEXPRESS
來源:https : //docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs1009
非常感謝所有幫助過的人!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/346619.html
上一篇:連接WCFCryptography.CryptographicException:指定的初始化向量(IV)與此演算法的塊大小不匹配
