访问C#中的HTML页面

本文关键字:页面 HTML 中的 访问 | 更新日期: 2023-09-27 18:23:48

基本上我有两个文件:details.htmldetails.cs

我想使用details.csdetails.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>

详细信息。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";
        }
     }
}

访问C#中的HTML页面

无法从.cs文件访问.html中的控件。您正在尝试使用aspx页面,因为您来自Page类,但命名为.html,您可能没有使用visual studio。请使用details.aspx和details.aspx。如果您没有Visual Studio,则可以从这里下载免费的快速版本。此链接说明如何使用visualstudio创建web应用程序项目。在VisualStudio中使用代码分离创建基本网页一文将帮助您在服务器端创建网页和访问html控件。

txt_details.Value = "your value";
txt_details.Value = "test";

这应该有助于

你可以通过控制的名称访问,例如:

txt_details.Value = "Test"