【问题标题】:Access HTML page in C#在 C# 中访问 HTML 页面
【发布时间】:2013-07-19 04:04:44
【问题描述】:

基本上我有两个文件: details.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";
        }
     }

}

【问题讨论】:

    标签: c# html controls


    【解决方案1】:

    无法从 .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";
    

    【讨论】:

      【解决方案2】:
      txt_details.Value = "test";
      

      这应该会有所帮助

      【讨论】:

        【解决方案3】:

        您可以通过控制名称进行访问,例如:

        txt_details.Value = "Test"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-29
          • 2015-12-19
          • 2017-09-02
          相关资源
          最近更新 更多