c# - HtmlGeneric 控件不包含定义
问题描述
我只想在 c# webforms 中创建一个带有 SQL 服务器的插入表单链接。不幸的是,我得到了这个错误并且没有定义它们的想法。
错误 CS1061 'HtmlGenericControl' 不包含 'Text' 的定义,并且找不到接受“HtmlGenericControl”类型的第一个参数的可访问扩展方法“Text”(您是否缺少 using 指令或程序集引用?)
请帮助我以正确的方式定义“文本”。
这是我的 Contact.aspx:
<form id="form1" runat="server">
<div id="title">
<h1>REGISTER PAGE</h1>
</div>
<div id ="teble"></div>
<table class="auto-style1">
<tr>
<td>
<aspLabel ID="Label1" runat="server" Text="name"></aspLabel></td>
<td>
<aspTextBox ID="TextBox1" runat="server"></aspTextBox></td>
</tr>
<tr>
<td>
<aspLabel ID="Label2" runat="server" Text="email"></aspLabel></td>
<td>
<aspTextBox ID="TextBox2" runat="server"></aspTextBox></td>
</tr>
<tr>
<td>
<aspLabel ID="Label3" runat="server" Text="subject"></aspLabel></td>
<td>
<aspTextBox ID="TextBox3" runat="server"></aspTextBox></td>
</tr>
</table>
<div id="button">
<aspButton ID="Button1" runat="server" Text="submit" OnClick="Button1_Click" BackColor="Yellow" />
</div>
<div id="sim"></div>
<p>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Table]"></asp:SqlDataSource>
</p>
这是我的 Contact.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class Contact : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
string checkuser = "select count(*) from Contact where name='" + TextBox1.Text + "'";
SqlCommand cmd = new SqlCommand(checkuser, conn);
int temp = Convert.ToInt32(cmd.ExecuteScalar().ToString());
if (temp == 1)
{
Response.Write("Student Already Exist");
}
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
string insertQuery = "insert into Table(Name,Email,Subject)values (@name,@email,@subject)";
SqlCommand cmd = new SqlCommand(insertQuery, conn);
cmd.Parameters.AddWithValue("@name", TextBox1.Text);
cmd.Parameters.AddWithValue("@email", TextBox2.Text);
cmd.Parameters.AddWithValue("@subject", TextBox3.Text);
cmd.ExecuteNonQuery();
Response.Write("Student registeration Successfully!!!thank you");
conn.Close();
}
catch (Exception ex)
{
Response.Write("error" + ex.ToString());
}
}
}
解决方案
请检查您的 aspx 代码。
所有asp控件的格式都不是很好。
标签应该是
<asp:Label ID="Label2" runat="server" Text="email"></asp:Label>
代替
<aspLabel ID="Label1" runat="server" Text="name"></aspLabel>
同样适用于所有其他控件,例如文本框和按钮
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="submit" OnClick="Button1_Click" BackColor="Yellow" />
所有的asp控件都应该像<asp:Textbox>, <asp:Label>, <asp:Button>
这样装饰。
希望这会有所帮助。
推荐阅读
- reactjs - 如何在 reactjs 中修复这个 Eslint 警告
- c++ - 将类方法作为另一个类中函数的参数传递
- networking - 通过“信号、发射和统计”功能进行统计收集,无需修改 omnet++ 模型(TicToc 教程 5.3)
- asp.net-core - 如何使用属性将项目添加到具有 ASP.NET Core 的附加值集合 ModelMetadata?
- r - 对于条形图,如何从 R 上显示的 x 轴获取所有标签?
- javascript - jQuery:引导模式未显示
- google-sheets - 每隔几周设置 Google 表格背景颜色
- python - 对在 Pytorch 中使用 super() 构建神经网络感到困惑
- javascript - 如何使用 MapBox 获取单个坐标数组?
- c++ - 在 constexpr 类中使用构造函数之外的构造函数参数