c# - 在 C# 中访问 HTML 页面

标签 c# html controls

基本上我有两个文件: 细节.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/

相关文章:

c# - 编译为空的虚拟 IEnumerable<T>

c# - 删除具有不同间距的重复字符串的最佳方法?

javascript - 无法使用 jquery 显示标签内容

c# - ListView 中的自动宽度

c# - 了解 ObjectDataSource 和选择参数

c# - 解密SSIS密码节点

c# - 添加 google font api 后仅显示安全内容

jquery - 我没有得到选择字段值

Winforms 导航栏控件 - 就像资源管理器地址栏

asp.net - 如何根据用户角色限制 asp.net 控制操作?