我有这个 C# 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
public partial class UpdateArticle : System.Web.UI.Page
{
public string ArticleTitle, ArticleBody, PostDate;
protected void Page_Load(object sender, EventArgs e)
{
string ArticleId;
try
{ ArticleId = Request.QueryString["ArticleId"].ToString();
string dpath = Server.MapPath(@"App_Data") + "/MySite.mdb";
string connectionstring = @"Data source='" + dpath + "';Provider='Microsoft.Jet.OLEDB.4.0';";
string q = string.Format("select * from tblArticles where ArticleId={0}", ArticleId);
OleDbConnection con = new OleDbConnection(connectionstring);
OleDbCommand cmd = new OleDbCommand(q, con);
con.Open();
cmd.ExecuteNonQuery();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "tbl");
con.Close();
ArticleTitle = ds.Tables[0].Rows[0]["ArticleTitle"].ToString();
ArticleBody = ds.Tables[0].Rows[0]["ArticleBody"].ToString();
PostDate = ds.Tables[0].Rows[0]["PostDate"].ToString();
}
catch
{ }
string NewArticleTitle, NewArticleDate, NewArticleBody, ArticleId1;
try
{
NewArticleTitle = Request.Form["ArticleTitle"].ToString();
NewArticleDate = Request.Form["ArticleDate"].ToString();
NewArticleBody = Request.Form["ArticleBody"].ToString();
ArticleId1 = Request.QueryString["ArticleId"].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, PostDate) values ('{0}','{1}','{2}') where ArticleId='{3}'", NewArticleTitle, NewArticleBody, NewArticleDate, ArticleId1);
OleDbCommand cmd = new OleDbCommand(QuaryString, con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "tbl");
con.Close();
}
catch { }
// Response.Redirect("ArticlesTable.aspx");
}}
我的代码中的问题是,当涉及到这一行时:
ArticleId1 = Request.QueryString["ArticleId"].ToString();
它从 try 块中退出。上面几行我写了这段代码:
ArticleId = Request.QueryString["ArticleId"].ToString();
它工作得很好。那么为什么下线不起作用呢?当我第一次调试它时:1:在ASPX页面中提交表单后:http: //i.stack.imgur.com/CfKXL.png :这是aspx代码:
<b>
כותרת המאמר :<br /></b><input id="Text1" type="text" name="ArticleTitle" value="<%=ArticleTitle %>"/><p></p>
<b>
תאריך פרסום: <br /></b><input id="Text2" type="text" name="ArticleDate" value="<%=PostDate %>"/><p></p>
<b>
תוכן המאמר: <br /></b> <textarea rows="10" cols="60" name="ArticleBody"><%=ArticleBody %></textarea>
<br />
<input id="Reset1" type="reset" value="נקה" />
<input id="Submit1" type="submit" value="שלח כתבה!" />
</form>
<p></p><p></p>
<a href="Default.aspx">חזור לדף הבית</a>
并且在提交之后(var 没有得到 Id 值!)