- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在新安装的 Visual Studio 2017 Community 中创建了一个 ASP.NET 空网站。我正在尝试复制以下教程所示的步骤:https://youtu.be/5dCAXwhjIYU
我只是在表单上添加了三个文本框以及一个提交按钮。当我双击按钮时,IDE 只是在页面的源 View 中突出显示按钮的 HTML。它不会如视频中所示和预期那样在代码隐藏中创建事件处理程序。
我在后面的代码中手动为单击按钮创建了事件处理程序。当我尝试运行代码时,它告诉我我引用的字段不存在:
Error CS0103 The name 'UserName' does not exist in the current context
这是我的 HTML 代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Users.aspx.cs" Inherits="Users" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
UserName<br />
<input id="UserName" type="text" /><br />
UserEmail<br />
<input id="UserEmail" type="text" /><br />
UserCampus<br />
<input id="UserCampus" type="text" /><br />
<input id="SaveUser" type="submit" value="submit" /></div>
</form>
</body>
</html>
C#代码:
public partial class Users : System.Web.UI.Page
{
protected void SaveUser_Click(object sender, EventArgs e)
{
SqlConnection BIGateConnection = new SqlConnection("Data Source=xxxxxxx;Initial Catalog=xxxxxxx;Integrated Security=True");
{
SqlCommand BIGateSQLCmd = new SqlCommand("Insert into [BIGateway].[dbo].[User] (UserName, userEmail, userCampus) VALUES (@userName, @userEmail, @userCampus)", BIGateConnection);
BIGateSQLCmd.Parameters.AddWithValue("@", UserName.Text);
BIGateSQLCmd.Parameters.AddWithValue("@", UserEmail.Text);
BIGateSQLCmd.Parameters.AddWithValue("@", UserCampus.Text);
}
}
}
我到底做错了什么?
最佳答案
如果你取的是HTML文本框那么代码如下:
<input id="UserName" type="text" /><br />
在这里你必须自己添加一行 runat="server"
它将如下所示:
<input id="UserName" type="text" runat="server"/><br />
然后你就可以在服务器端使用它了。
希望对您有所帮助。
关于c# - Codebehind 不创建按钮单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46122876/
如果我在 .aspx 页面的代码隐藏中创建一个对象,我希望该对象可以存活多长时间?它会跨回传吗?我能以某种方式将它传递到另一个页面吗?我可以让它与 session 对象一样长吗? 我在网上搜索,希望找
我在新安装的 Visual Studio 2017 Community 中创建了一个 ASP.NET 空网站。我正在尝试复制以下教程所示的步骤:https://youtu.be/5dCAXwhjIYU
我有我的主页: $(docume
我有一个列表框,其中 DataTemplate 分配给它的 ItemTemplate,如下所示:
当我从代码初始化控件属性时,绑定(bind)到 XAML 上定义的同一属性不起作用。为什么? 例如,我在启动时使用以下语句设置控件属性: myControl.SetValue(UIElement.Vi
我想在 asp.net 代码隐藏中复制一个控件。 在我的代码中: HtmlGenericControl divRowHeader = new HtmlGenericControl("div"); di
我必须在 C# CodeBehind 中 100% 创建一个 GridView。我让它选择一行并使用此代码发回: void dataGrid_ItemCreated(object sender
我想添加 从我的代码隐藏到页面上。我网站上的用户将上传 X 张图片,我不想添加 图片上传前。 我想添加 的原因就是给用户展示上传的图片。 如何做到这一点? 我已经看到正在使用的占位符 Control
我有以下代码: [WebMethod] public static string GenerateHtml(string id) { //return id; DataView vwM
我在使用添加到代码隐藏的一些 JavaScript 时遇到了问题。我在这里试图实现的目标是在页面加载上进行另一次回发。现在,你可能会觉得这很奇怪,但我的疯狂是有办法的。 在我的 ASP.Net 向导中
protected void Page_Load(object sender, EventArgs e) { System.IO.File.WriteAllText(@
任何人都可以通过示例告诉我 Codebehind="MyCode.aspx.cs" 和 Src="MyCode.aspx.cs" 之间的区别是什么? 最佳答案 代码隐藏 Specifies the n
我正在尝试创建一种方法来在两个用户控件之间绘制一条线(路径)。我发现某人的帖子为我提供了有关如何执行此操作的一般指示,我成功地实现了代码并开始根据我的需要对其进行调整。 我在访问用户控件时遇到问题:
我有 gridView,如果用户单击按钮,我想将其信息提供给弹出窗口。 这是打开和关闭弹出窗口的 JavaScript function ShowModalPopup() {
我正在使用 Expression Blend。 假设我得到了: Public string FirstName{get;set;} 编辑:感谢您的回答,但恐怕人们不明白我的问题。我确实知道如何在代码或
我正在尝试从 JavaScript 代码调用代码隐藏方法。这工作正常,但我不确定如何将 JavaScript 参数传递给该方法。如果我在括号之间放置任何值,它将作为 CodeBehind 上下文而不是
我知道,太疯狂了。 所以我有一个 asp 标签,我想用 jQuery 填充它。稍后,在代码隐藏中,我想访问此文本以进行其他操作。 我拥有的是: function runMe () {
我对我已经做过一百万次的一件简单的事情感到很麻烦,但是这次,我做不到。 我的 Gridview 中有 LinkButtons 我有六次。总是一个 Edit
我想将代码隐藏中的值分配给 angularjs 变量。做这个的最好方式是什么。 (假设我在 app.js 中有一个变量 var UserInfo)。 如果页面是 Default.aspx,我会在 De
伙计们我正在尝试将 Style.xaml 中的样式用于我后面的代码按照我的风格,我有这样的代码 文件样式.xaml 在我的 Apptest.xaml.cs 文件中有这样的代码 txt.Foregr
我是一名优秀的程序员,十分优秀!