【发布时间】:2021-12-16 18:13:09
【问题描述】:
我已经查找了其他问题,但没有一个解决方案适用于这种情况。 我试图删除 Button1_Click 函数并再次添加它,我试图重命名它。 在设计中右键单击并查看代码会将我带到同一个文件,因此文件已正确连接。
有趣的是它以前可以工作。起初我收到错误:'default_aspx' 不包含'TextBox1_TextChange'的定义。我删除了 OnTextChange=TextBox1_TextChange 然后删除了 protected 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 非常陌生。 任何帮助表示赞赏。提前致谢!
编辑:我认为问题出在反斜杠上。添加了错误的图片。删除反斜杠会删除错误,但当然不能删除它。
【问题讨论】:
-
在您的代码中,在剃须刀中搜索
Button1_Click- 然后尝试找到该委托的实现... -
请尝试清理并重建项目
-
尝试复制出click方法代码,删除该方法,然后双击按钮重新创建事件,然后在代码中重新添加。
-
感谢@JobesK 的回复。我已经尝试过你的解决方案。我弄清楚了“编辑”中提到的问题是什么。就是不知道怎么解决
-
按钮问题与该连接字符串没有任何关系。 \(看起来不错)是正确的,因为在 sql server 中,它总是 SERVERNAME\SQLINSTANCE 名称,看起来正确。此外,由于此页面是母版页的子页面,我还将打开站点母版,右键单击,查看代码并检查该代码页的内部。 (怀疑按钮单击是否在 master 中 - 但它是可能的)。