基本上我有两个文件:
细节.html
details.cs
我想使用 details.cs
将值写入 details.html
但 html 文本框仍然保持不变。
details.html
<%@ Page Language="C#"
AutoEventWireup="true"
CodeBehind="details.cs"
Inherits="Details.DetailsHTML" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form>
<input id="txt_details" type="text" name="txt_details" runat="server"/>
</form>
</body>
</html>
details.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Data;
namespace Details
{
public partial class DetailsHTML : Page
{
//Declare controls used
protected System.Web.UI.HtmlControls.HtmlInputText txt_details;
protected void Page_Load(object sender, EventArgs e)
{
string strValue = Page.Request.Form ["txt_details"].ToString();
strValue = "test";
}
}
}
最佳答案
您无法从 .cs 文件访问 .html 中的控件。您正在尝试使用 aspx
页面,因为您继承自 Page
类但命名为 .html,您可能没有使用 visual studio。使用 details.aspx 和 details.aspx.cs。如果您没有 Visual Studio,那么您可以从 here 下载免费的快速版本。 .这link说明如何使用 Visual Studio 创建 Web 应用程序项目。 article在 Visual Studio 中使用代码分离创建基本网页将帮助您创建网页并在服务器端访问 html 控件。
txt_details.Value = "your value";
关于c# - 在 C# 中访问 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17737945/