我的一个编辑页面(Edit.aspx)即作数据的新增界面,又作编辑界面,通过调用时带的QueryString区分。
调用Edit.aspx?Opt=N 表示新增
调用Edit.aspx?Opt=E 表示编辑问题是,我在这界面新增点保存按钮保存成功后,界面的Url应该从Edit.aspx?Opt=N变成Edit.aspx?Opt=E
不然再点保存就又新增了一条同样的数据了。我的需求是,在.cs代码中怎么改变即将传递给自己的Request的值,而不改变其它内容(如.cs中对某些控件的
赋值还是要的)请不要告诉我使用Reponse.Redirect,这样Url是改了,但.cs中对界面控件的赋值丢失了。不知道我的问题是否描述清楚了,请尽量帮助解决!解决后立即给分
调用Edit.aspx?Opt=N 表示新增
调用Edit.aspx?Opt=E 表示编辑问题是,我在这界面新增点保存按钮保存成功后,界面的Url应该从Edit.aspx?Opt=N变成Edit.aspx?Opt=E
不然再点保存就又新增了一条同样的数据了。我的需求是,在.cs代码中怎么改变即将传递给自己的Request的值,而不改变其它内容(如.cs中对某些控件的
赋值还是要的)请不要告诉我使用Reponse.Redirect,这样Url是改了,但.cs中对界面控件的赋值丢失了。不知道我的问题是否描述清楚了,请尽量帮助解决!解决后立即给分
可以把 操作标识 放到viewstate
如果viewstate没有值(即第一次载入)就读QueryString
保存或编辑后写入viewstate
{
if(!ispostback)
{
if(Request.QueryString["Opt"] == null){ ViewState["Opt"] = "";}
else{ViewState["Opt"] = Request.QueryString["Opt"] ;}
}
}
SaveBtnClick(object sender, EventArgs e)
{//判斷ViewState["Opt"] 的操作
if(ViewState["Opt"] == "N")
{
save("N");
ViewState["Opt"]="E";
}
else if(ViewState["Opt"] == "E")
{
save("E");
ViewState["Opt"]="E";
}
}Page_OnPreRender()
{
BindData();
}
BindData()
{
if(ViewState["Opt"] == "N"){//綁定}
else if(ViewState["Opt"] == "E"){//綁定}
}
调用Edit.aspx?Opt=E 表示编辑
设计上有问题
编辑页没有编辑信息的ID,如何知道是编辑的哪个信息?
在列表页,做一个添加,一个编辑。
添加的时候直接用Edit.aspx
编辑的时候用Edit.aspx?editID=信息ID
到页面后判断参数editID是否存在即可
我是这么做的一个列表页面(list.aspx),一个新增页面(编辑)(content.aspx)。当点击新增或者编辑列表中的某项时,就打开第二个页面(content.aspx)。操作成功后,跳转到第一个列表界面(list.aspx)。楼上是否是以上两个页面的一个综合体?那样的话, 就搞一个repeater来整吧。
http://www.51aspx.com/CV/CaiCaiGuestBook/
编辑:利用gridview自带的COmmandfiled的编辑功能
protected void gridView_RowEditing(object sender, GridViewEditEventArgs e)
{
int ID = Convert.ToInt32(gridView.DataKeys[e.NewEditIndex].Value); //获取ID
string url = "Edit.aspx?action=edit&id=" + ID;
Response.Redirect(url, true);
}添加:在页面添加一个添加按钮,然后这么写:
Response.Redirect("Edit.aspx", true);--------------------------------------------------------------------------------
在edit.aspx页面这么写:
public partial class Edit : System.Web.UI.Page
{
private static int Tid = 0;
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack)
{
if (Request["action"] == "edit")
{
Tid = Convert.ToInt32(Request["id"].ToString());
DataTable dt = cp.showCorpInfo(Tid);
this.txtName.Text = dt.Rows[0]["Name"].ToString();
this.txtAddress.Text = dt.Rows[0]["Address"].ToString();
this.txtTel.Text = dt.Rows[0]["Tel"].ToString();
this.txtFax.Text = dt.Rows[0]["Fax"].ToString();
自己写自己的代码 }
else
{
Tid = 0;
}
}
}
在保存按钮里这么写:
protected void baocun_Click(object sender, EventArgs e)
{ string script = "";
script = "biaojiid=" + Tid + ";window.parent.document.all('map').src='../iframemap.aspx?corp=true¢erX=" + txtX.Text + "¢erY=" + txtY.Text + "&level=" + level.Text + "';window.parent.document.all('manage').src='CorpInfoAdd.aspx';";
if (Tid == 0)//添加
{
string[] corp = new string[11];
corp[0] = small.Value;
corp[1] = this.big_class.SelectedItem.Value; ;
corp[2] = this.txtName.Text.ToString().Trim();
corp[3] = this.txtAddress.Text.ToString().Trim();
corp[4] = this.txtTel.Text.ToString().Trim();
corp[5] = this.txtLinkman.Text.ToString().Trim();
corp[6] = this.txtFax.Text.ToString().Trim();
corp[7] = this.txtPostCode.Text.ToString().Trim();
corp[8] = this.txtEmail.Text.ToString().Trim();
corp[9] = this.txtWebsite.Text.ToString().Trim();
corp[10] = this.txtX.Text.ToString().Trim();
if (cp.insertCorp(corp))
{
ConfirmManage("添加成功 ! 是否继续添加?", "gridview所在页面.aspx");
}
else
{
Alert("失败");
return;
} 自己写自己的代码 }
else//编辑
{
string[] corp = new string[11];
DataTable dt = cp.showCorpInfo(Tid);
corp[0] = small.Value;
corp[1] = this.big_class.SelectedItem.Value;
corp[2] = this.txtName.Text.ToString().Trim();
corp[3] = this.txtAddress.Text.ToString().Trim();
corp[4] = this.txtTel.Text.ToString().Trim();
corp[5] = this.txtLinkman.Text.ToString().Trim();
corp[6] = this.txtFax.Text.ToString().Trim();
corp[7] = this.txtPostCode.Text.ToString().Trim();
corp[8] = this.txtEmail.Text.ToString().Trim();
corp[9] = this.txtWebsite.Text.ToString().Trim();
corp[10] = Convert.ToString(Convert.ToInt32(this.txtX.Text.ToString().Trim()) + 2);
if (cp.updateCorp(corp))
{
ConfirmManage("修改成功 ! 是否继续添加?", ”gridview所在页面.aspx");
}
else
{
Alert("失败");
}
}public void Confirm(string msg, string url)
{
msg = msg.Replace("'", @"\'");
url = url.Replace("'", @"\'");
string script = "var retValue=window.confirm('" + msg + "');" + "if(retValue){window.location='" + url + "';}";
RegisterStartupScript(script);
}}
这样添加保存后就可以如果用户选择“是”就可以停留在添加页面继续添加,这个时候控件的值被清空,用户可以添加新的数据,如果用户选择“取消”,它就可以回到开始的gridview所在的页面,这个时候用户如果想编辑的话,可以选择要编辑的数据。
你的意思我明白,你是要在添加保存后,然后让你添加的这条记录变成编辑状态,你想想如果你的ID是自动增长的,你在这个Edit.aspx页面刚添加好数据你在这个页面怎么可能立刻就取到你添加的那条数据的ID,就算你能取到,也是很麻烦的,建议你采用我上面的那种,我就是那么用的,很好用的
我说的够详细了吧,我花了不少时间给你整理,你记得要加分啊
编辑:利用gridview自带的COmmandfiled的编辑功能
protected void gridView_RowEditing(object sender, GridViewEditEventArgs e)
{
int ID = Convert.ToInt32(gridView.DataKeys[e.NewEditIndex].Value); //获取ID
string url = "Edit.aspx?action=edit&id=" + ID;
Response.Redirect(url, true);
} 添加:在页面添加一个添加按钮,然后这么写:
Response.Redirect("Edit.aspx", true); --------------------------------------------------------------------------------
在edit.aspx页面这么写:
public partial class Edit : System.Web.UI.Page
{
private static int Tid = 0;
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack)
{
if (Request["action"] == "edit")
{
Tid = Convert.ToInt32(Request["id"].ToString());
DataTable dt = cp.showCorpInfo(Tid);
this.txtName.Text = dt.Rows[0]["Name"].ToString();
this.txtAddress.Text = dt.Rows[0]["Address"].ToString();
this.txtTel.Text = dt.Rows[0]["Tel"].ToString();
this.txtFax.Text = dt.Rows[0]["Fax"].ToString();
自己写自己的代码 }
else
{
Tid = 0;
}
}
}
在保存按钮里这么写:
protected void baocun_Click(object sender, EventArgs e)
{ if (Tid == 0)//添加
{
string[] corp = new string[11];
corp[0] = small.Value;
corp[1] = this.big_class.SelectedItem.Value; ;
corp[2] = this.txtName.Text.ToString().Trim();
corp[3] = this.txtAddress.Text.ToString().Trim();
corp[4] = this.txtTel.Text.ToString().Trim();
corp[5] = this.txtLinkman.Text.ToString().Trim();
corp[6] = this.txtFax.Text.ToString().Trim();
corp[7] = this.txtPostCode.Text.ToString().Trim();
corp[8] = this.txtEmail.Text.ToString().Trim();
corp[9] = this.txtWebsite.Text.ToString().Trim();
corp[10] = this.txtX.Text.ToString().Trim();
if (cp.insertCorp(corp))
{
Confirm("添加成功 ! 是否继续添加?", "gridview所在页面.aspx");
}
else
{
Alert("失败");
return;
} 自己写自己的代码 }
else//编辑
{
string[] corp = new string[11];
DataTable dt = cp.showCorpInfo(Tid);
corp[0] = small.Value;
corp[1] = this.big_class.SelectedItem.Value;
corp[2] = this.txtName.Text.ToString().Trim();
corp[3] = this.txtAddress.Text.ToString().Trim();
corp[4] = this.txtTel.Text.ToString().Trim();
corp[5] = this.txtLinkman.Text.ToString().Trim();
corp[6] = this.txtFax.Text.ToString().Trim();
corp[7] = this.txtPostCode.Text.ToString().Trim();
corp[8] = this.txtEmail.Text.ToString().Trim();
corp[9] = this.txtWebsite.Text.ToString().Trim();
corp[10] = Convert.ToString(Convert.ToInt32(this.txtX.Text.ToString().Trim()) + 2);
if (cp.updateCorp(corp))
{
Confirm("修改成功 ! 是否继续添加?", ”gridview所在页面.aspx");
}
else
{
Alert("失败");
}
} public void Confirm(string msg, string url)
{
msg = msg.Replace("'", @"\'");
url = url.Replace("'", @"\'");
string script = "var retValue=window.confirm('" + msg + "');" + "if(retValue){window.location='" + url + "';}";
RegisterStartupScript(script);
} }
这样添加保存后就可以如果用户选择“是”就可以停留在添加页面继续添加,这个时候控件的值被清空,用户可以添加新的数据,如果用户选择“取消”,它就可以回到开始的gridview所在的页面,这个时候用户如果想编辑的话,可以选择要编辑的数据。
你的意思我明白,你是要在添加保存后,然后让你添加的这条记录变成编辑状态,你想想如果你的ID是自动增长的,你在这个Edit.aspx页面刚添加好数据你在这个页面怎么可能立刻就取到你添加的那条数据的ID,就算你能取到,也是很麻烦的,建议你采用我上面的那种,我就是那么用的,很好用的
我说的够详细了吧,我花了不少时间给你整理,你记得要加分啊
刚刚有些地方不需要的我把它去了