HTML到SQL使用POST方法

时间:2015-07-22 07:57:12

标签: c# asp.net sql-server

设置本地服务器后,我想将数据从SQL server发送到HTML page。我正在使用C#ASP.NET。但问题是,虽然我正在使用post方法,但它只显示空白结果,而没有正确地将string输入发送到我的mssql服务器。

以下是我的Write.aspx文件的一部分,主要从用户处获取信息 HTML文字输入。我使用post方法处理Write_Check.aspx中的信息(实际上在代码后面,Write_Check.aspx.cs

<%@ Page Language = "C#" AutoEventWireup = "true" 
CodeFile = "Write.aspx.cs" Inherits = "Article_Write" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
    <title>Title</title>
    <script language = "javascript">
    </script>
</head>
<body>
    <form id = "form1" method = "post" action = "Write_Check.aspx">
        <center>
            <table width = "100%" border = "0" cellpadding = "2" cellspacing = "3">
                <tbody><tr align = "left">
                    <td colspan = "3">
                        <h2>Write Article</h2>
                    </td>
                </tr>
                <tr align = "left">
                    <td width = "100px">
                        Writer
                    </td>
                    <td width = "600px">
                        <input type = "text" id = "writername" name = writer_name />
                    </td>
                </tr>

我的Write_Check.aspx只是一行。

<%@ Page Language = "C#" AutoEventWireup = "true" CodeFile = "Write_Check.aspx.cs" Inherits = "Save_Check" %>

这是我的Write_Check.aspx.csCWebBase只是数据库连接方法的集合,因此我也可以轻松地将其用于其他cs文件。

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Save_Check : CWebBase
{

    protected void Page_Load(object sender, EventArgs e)
    {
        string name, password, title, content;
        //find form name
        name = Request.Form["writer_name"];
        password = Request.Form["password_check"];
        title = Request.Form["title_check"];
        content = Request.Form["content_check"];

        CDatabase _db = new CDatabase();

        string sql = "INSERT INTO dbo.article (writerName, passwords, title, content) values('"
            + name + "','" + password + "','" + title + "','" + content + "')";

        _db.ExecuteSQL(sql);
        _db.Dispose();

        Response.Redirect("List.aspx");

    }
}

我不确定为什么我在这里使用的post方法正常工作。

1 个答案:

答案 0 :(得分:1)

好的,所以,从我所看到的你并没有真正提交表格(不是你所发布的代码)。
为了查看后面代码中的值,您必须通过单击按钮(或其他任何您想要的内容)来提交表单。
例如:

<form id="form1" runat="server">
//data to submit
<asp:Button id="button1" Text="Click me!" runat="server" OnClick="submit" />
</form>

提交表单后,您可以像尝试一样检查值:
name = Request.Form["writer_name"];