IsPostBack in ASPX

本文关键字:ASPX in IsPostBack | 更新日期: 2023-09-27 17:50:41

我有这个ASPX代码:

<form name="AddArticle" action="Register.aspx" method="post">
<b>
title :<br /></b><input id="Text1" type="text" name="ArticleTitle"/><p></p>
<b>
date: <br /></b><input id="Text2" type="text" name="ArticleDate"/><p></p>
<b>
author : <br /></b><input id="Text3" type="text" name="ArticleAuthor"/><p></p>
<b>
text: <br /></b> <textarea rows="10" cols="60"  name="ArticleBody"></textarea>
<br />
<input id="Reset1" type="reset" value="clean" />
<input id="Submit1" type="submit" value="send" /></form>
我有这个aspx . c#代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string ArticleTitle = "", ArticleBody = "", ArticleAuthor = "", ArticleDate = "";
        if (IsPostBack)
        {
            try
            {
                ArticleTitle = Request.Form["ArticleTitle"].ToString();
                ArticleDate = Request.Form["ArticleDate"].ToString();
                ArticleAuthor = Request.Form["ArticleAuthor"].ToString();
                ArticleBody = Request.Form["ArticleBody"].ToString();

                string dpath = Server.MapPath(@"App_Data") + "/MySite.mdb";
                string connectionstring = @"Data source='" + dpath + "';Provider='Microsoft.Jet.OLEDB.4.0';";
                OleDbConnection con = new OleDbConnection(connectionstring);
                string QuaryString = string.Format("insert into tblArticles(ArticleTitle,ArticleBody, ArticleAuthor, PostDate) values ('{0}','{1}','{2}','{3}')", ArticleTitle, ArticleBody, ArticleAuthor, ArticleDate);
                OleDbCommand cmd = new OleDbCommand(QuaryString, con);
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch
            {
                Response.Redirect("Default.aspx");
            }
        }
    }
    }

我的问题是为什么当我点击"发送"按钮,从表单(标题,作者…)的信息不插入到数据库?我该怎么修理它?希望能有所帮助,谢谢。

IsPostBack in ASPX

最好的方法是使用按钮单击事件,但在您的情况下。您可以为每个元素使用AutoPostBack属性:

<asp:TextBox ID="ArticalTitle" runat="server" Text="Button"  AutoPostBack="true"/>

当用户改变该元素的焦点时(按TAB等),代码将回发文本框中的值。但是在这种情况下,你的值一个接一个地传递,所以你需要存储这些值,然后再尝试将它们写入数据库,或者你可以使用面板控件并在面板控件外放置一个虚拟按钮来更改焦点:

<asp:Panel ID="Panel1" runat="server" AutoPostBack="true">
    <asp:TextBox ID="TextBox1" Text="asd" runat="server" />
    <asp:TextBox ID="TextBox2" Text="asd" runat="server" />
</asp:Panel>
<asp:Button ID="Button1" runat="server" Text="Button" />

当用户点击按钮面板控制自动返回时,你可以在文本框中捕获值。

我认为你应该写一个按钮点击事件来调用函数:

cs

  protected void Button1_Click(object sender, EventArgs e)
    {
        ArticleTitle = Request.Form["ArticleTitle"].ToString();
            ArticleDate = Request.Form["ArticleDate"].ToString();
            ArticleAuthor = Request.Form["ArticleAuthor"].ToString();
            ArticleBody = Request.Form["ArticleBody"].ToString();

            string dpath = Server.MapPath(@"App_Data") + "/MySite.mdb";
            string connectionstring = @"Data source='" + dpath + "';Provider='Microsoft.Jet.OLEDB.4.0';";
            OleDbConnection con = new OleDbConnection(connectionstring);
            string QuaryString = string.Format("insert into tblArticles(ArticleTitle, PostDate) values ('{0}','{1}','{2}','{3}')", ArticleTitle, ArticleBody, ArticleAuthor, ArticleDate);
            OleDbCommand cmd = new OleDbCommand(QuaryString, con);
            con.Open();
            cmd.ExecuteNonQuery();
    }

. aspx:

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

使用StoredProcedure:连接字符串在web.config:

<add name="CGB_GlobalInfoConnectionString" connectionString="Data Source=127.0.0.1;Initial Catalog=TestDB;Persist Security Info=True;User ID=sa;Password=11111" providerName="System.Data.SqlClient" />

cs:

SqlConnection connection = new SqlConnection(DataManager.DBConnectString);
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = connection;
                cmd.CommandText = "Insert_AddSomething";
                cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = userid;
                connection.Open();
                cmd.ExecuteNonQuery();
                }