.net 怎末从一个页面往另一个页面的Gridview里边传值,并且将数据同时添加到数据库里

2024年11月22日 10:57
有4个网友回答
网友(1):

后台代码:我就是这么实现的,功能跟你说的一样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using JH.Game.ServiceLibrary.Bll;
using JH.Game.ServiceLibrary.Model;

public partial class Manage_PetEnergy_SpeciaWork : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
SpeciaWork speciawork = new SpeciaWork();
SpeciaWorkBll speciaworkBll = new SpeciaWorkBll();
public void Bind()
{
DataSet ds = speciaworkBll.Getds();
ds.AcceptChanges();
this.GvExamin.DataSource=ds;
this.GvExamin.DataKeyNames = new string[] { "specialid" };
this.GvExamin.DataBind();
this.ddlCurrentPage.Items.Clear();
for (int i = 1; i <= this.GvExamin.PageCount; i++)
{
this.ddlCurrentPage.Items.Add(i.ToString());
}
if (ds.Tables[0].Rows.Count != 0)
{
this.ddlCurrentPage.SelectedIndex = this.GvExamin.PageIndex;
}
else
{
this.notxt.Text = "没有相关数据!";
}
}

//首页
protected void lnkbtnFrist_Click(object sender, EventArgs e)
{
this.GvExamin.PageIndex = 0;
Bind();
}

//上一页
protected void lnkbtnPre_Click(object sender, EventArgs e)
{
if (this.GvExamin.PageIndex > 0)
{
this.GvExamin.PageIndex = this.GvExamin.PageIndex - 1;
Bind();
}
}

//下一页
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
if (this.GvExamin.PageIndex < this.GvExamin.PageCount)
{
this.GvExamin.PageIndex = this.GvExamin.PageIndex + 1;
Bind();
}
}

//尾页
protected void lnkbtnLast_Click(object sender, EventArgs e)
{
this.GvExamin.PageIndex = this.GvExamin.PageCount;
Bind();
}

//跳转到第几页
protected void ddlCurrentPage_SelectedIndexChanged(object sender, EventArgs e)
{
this.GvExamin.PageIndex = this.ddlCurrentPage.SelectedIndex;
Bind();
}

这里就是关键代码了:
protected void Button1_Click(object sender, EventArgs e)
{
CheckTime();
if (CheckTime() == 0)
{
speciawork.addstattime = this.beginTime.Value;
speciawork.addendtime = this.endTime.Value;
speciawork.note = this.TextBox1.Text.Trim();

int res = speciaworkBll.Add(speciawork);
if (res > 0)
{
MessageBox.Show(this.Button1, "添加成功");
this.beginTime.Value = "";
this.TextBox1.Text = "";
Bind();
}
else
{
MessageBox.Show(this.Button1, "添加失败");

}
}
//else
//{
// MessageBox.Show(this.Button1, "添加失败");
//}
}
到这里!
protected int CheckTime()
{
int checkId = 0;

//DateTime dt = DateTime.Now;

if (this.beginTime.Value == "" || this.endTime.Value == "")
{

System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('请选择开始结束时间!');", true);
checkId = -1;
}
else if (Convert.ToDateTime(this.endTime.Value) < Convert.ToDateTime(this.beginTime.Value))
{

System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('开始时间不能大于结束时间!');", true);
checkId = -1;
}
return checkId;
}

protected void GvExamin_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(GvExamin.DataKeys[e.RowIndex].Value.ToString());
int res = speciaworkBll.Delete(id);
if (res > 0)
{
GvExamin.EditIndex = -1;
Bind();
}
}

protected void GvExamin_RowDataBound(object sender, GridViewRowEventArgs e)
{
this.lblCurrentPage.Text = string.Format("当前第{0}页/总共{1}页", this.GvExamin.PageIndex + 1, this.GvExamin.PageCount);
//遍历所有行设置边框样式
foreach (TableCell tc in e.Row.Cells)
{
tc.Attributes["style"] = "border-color:Black";
}
//用索引来取得编号
if (e.Row.RowIndex != -1)
{
int id = GvExamin.PageIndex * GvExamin.PageSize + e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}

if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
// ////鼠标移动到每项时颜色交替效果
e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#bfe2ff';this.style.color='#8C4510'");
e.Row.Attributes["style"] = "Cursor:hand";
}
}
}

前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SpeciaWork.aspx.cs" Inherits="Manage_PetEnergy_SpeciaWork" %>





特殊上班时间































特殊上班时间设置
补班时间:
--至--


备注:
TextMode="MultiLine" >







runat="server" CssClass="yui-datatable-theme"
AutoGenerateColumns="False" Width="95%" AllowPaging="True"
DataKeyNames="specialid" AllowSorting="True"
PageSize="3" onrowdatabound="GvExamin_RowDataBound"
onrowdeleting="GvExamin_RowDeleting">









')">详细信息




')">编辑









首页
上一页

下一页
尾页
跳转到第 >





网友(2):

下面是个例子,我是用分层的方法实现的,不然代码不好维护啊
protected void btnAdd_Click(object sender, EventArgs e)
{
Job job = new Job(0, txtJobName.Text, txtRemark.Text);
iJobMgr.AddJob(job);//数据库实现需要自己实现业务层和数据访问层,所以这里只要调用下就可以了
ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "", false);
Server.Transfer(本页面的路径);
}
下面用了分页,其实就是拖两个控件而已
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = iJobMgr.ListJob();
this.gvw.DataSource = ds.Tables[0];
this.gvw.DataBind();
}
protected void gvw_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.gvw.PageIndex = e.NewPageIndex;
InitPage();
}

protected void InitPage()
{
DataSet ds = iJobMgr.ListJob();
this.gvw.DataSource = ds.Tables[0];
this.gvw.DataBind();
}

网友(3):

只能告诉方法,代码没有时间了,Add.aspx页的增加事件中,读取数据,保存到数据库,并跳转到list.aspx, List.aspx页的Page_Load()中进行数据查询,并将结果绑定到gridview;
如果仅仅是思路的话,感觉这个问题太过于简单。。。

网友(4):

用javascript 在数据进行绑定时,注册一下javascript事件,然后就不用我说了吧,当然是点击这个事件,得到点击的值,然后传给你要传的对象哦....希望对你有帮助..