我有这个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#code:
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");
}
}
}
}
我的问题是为什么当我点击“发送”按钮时,表格中的信息提示(标题,作者......)没有插入数据库?我该如何解决? 希望得到帮助,谢谢。
答案 0 :(得分:1)
我认为您应该编写一个按钮单击事件来调用该函数:
的.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上的connectString:
<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();
}
答案 1 :(得分:1)
使用按钮点击事件的最佳方式,但在您的情况下。您可以为每个元素使用AutoPostBack属性:
<asp:TextBox ID="ArticalTitle" runat="server" Text="Button" AutoPostBack="true"/>
当用户更改此元素的焦点时(按TAB等)。代码将在文本框中回发值。但在这种情况下,您的值会逐个传递,因此您需要在尝试将这些值写入数据库之前存储这些值,或者您可以使用面板控件并在面板控件外部放置一个虚拟按钮以进行更改焦点:< / p>
<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" />
当用户单击“按钮”面板控制自动回行时,您可以捕获文本框中的值。
希望这有帮助。