.net 怎末从一个页面往另一个页面的Gridview里边传值,并且将数据同时添加到数据库里
发布网友
发布时间:2023-06-30 16:09
我来回答
共4个回答
热心网友
时间:2023-10-08 22:24
后台代码:我就是这么实现的,功能跟你说的一样
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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>特殊上班时间</title>
<link href="../Css/gr.css" rel="stylesheet" type="text/css" />
<link href="../Css/yui-datatable.css" rel="stylesheet" type="text/css" />
<link href="../Css/css.css" rel="stylesheet" type="text/css" />
<script src="../js/Time.js" type="text/javascript"></script>
<script type="text/javascript">
function FindSpeciaWork(specialid) {
URL = "FindSpeciaWork.aspx?specialid=" + specialid;
window.open(URL, "FindSpeciaWork", "height=350,width=600,status=0,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no");
}
function ModifySoeciaWork(specialid) {
URL = "ModifySoeciaWork.aspx?specialid=" + specialid;
window.open(URL, "ModifySoeciaWork", "height=350,width=600,status=0,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no");
}
// function AddHoliday() {
// URL = "AddHoliday.aspx";
// window.open(URL, "AddHoliday", "height=400,width=700,status=0,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no");
// }
</script>
<script type="text/javascript" language="javascript">
function yz() {
document.getElementById("HidBtn").click();
}
function bgtimeChang() {
document.getElementById("HidBtn2").click();
}
function endtimeChang() {
document.getElementById("HidBtn3").click();
}
function btnsumit_onclick() {
}
</script>
<style type="text/css">
input.BigInput{ COLOR: #000066; BACKGROUND: #F8F8F8; border:1 solid black; BORDER-BOTTOM:1px double; FONT-SIZE: 12pt; FONT-STYLE: normal; FONT-VARIANT: normal; FONT-WEIGHT: normal; HEIGHT: 100px; LINE-HEIGHT: normal}
</style>
</head>
<body style="background-color:#bfe2ff">
<form id="form1" runat="server">
<div class="oa_content">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="4" class="title">特殊上班时间设置</td>
</tr>
<tr >
<th >补班时间:</th>
<td align="left" >
<input id="beginTime" runat="server" class="SmallInput" name="beginTime" onclick="setday(this)" readonly="readonly" size="30" type="text" /> --至--
<input id="endTime" runat="server" class="SmallInput" name="endTime" onclick="setday(this)" readonly="readonly" size="30" type="text" />
</td>
</tr>
<tr>
<th>备注:</th>
<td align="left">
<asp:TextBox runat="server" ID ="TextBox1" Width="440" Height="60"
TextMode="MultiLine" ></asp:TextBox></td>
</tr>
</table>
<div class="oa_btn">
<asp:Button ID="Button1" runat="server" Text="添加" onclick="Button1_Click" />
</div>
</div>
<div>
<div>
<asp:GridView ID="GvExamin" GridLines="None"
runat="server" CssClass="yui-datatable-theme"
AutoGenerateColumns="False" Width="95%" AllowPaging="True"
DataKeyNames="specialid" AllowSorting="True"
PageSize="3" onrowdatabound="GvExamin_RowDataBound"
onrowdeleting="GvExamin_RowDeleting">
<RowStyle CssClass="data-row" />
<AlternatingRowStyle CssClass="alt-data-row" />
<Columns>
<asp:BoundField DataField="specialid" HeaderText="编号" Visible="False" />
<asp:BoundField DataField="addstattime" HeaderText="公休补班起始日期" />
<asp:BoundField DataField="addendtime" HeaderText="公休补班结束日期" />
<asp:BoundField DataField="note" HeaderText="备注" />
<asp:TemplateField HeaderText="详细信息" Visible="true">
<ItemTemplate>
<a href="javascript:FindSpeciaWork('<%#DataBinder.Eval(Container.DataItem,"specialid")%>')">详细信息</a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="编辑" Visible="true">
<ItemTemplate>
<a href="javascript:ModifySoeciaWork('<%#DataBinder.Eval(Container.DataItem,"specialid")%>')">编辑</a>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<PagerSettings Visible="False" />
</asp:GridView>
<asp:Label ID ="notxt" runat="server"></asp:Label>
<br />
<asp:LinkButton ID="lnkbtnFrist" runat="server" onclick="lnkbtnFrist_Click" >首页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnPre" runat="server" onclick="lnkbtnPre_Click" >上一页</asp:LinkButton>
<asp:Label ID="lblCurrentPage" runat="server"></asp:Label>
<asp:LinkButton ID="lnkbtnNext" runat="server" onclick="lnkbtnNext_Click" >下一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnLast" runat="server" onclick="lnkbtnLast_Click" >尾页</asp:LinkButton>
跳转到第<asp:DropDownList ID="ddlCurrentPage" runat="server" AutoPostBack="True" onselectedindexchanged="ddlCurrentPage_SelectedIndexChanged"
>
</asp:DropDownList>页
</div>
</div>
</form>
</body>
</html>
热心网友
时间:2023-10-08 22:24
下面是个例子,我是用分层的方法实现的,不然代码不好维护啊
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", "<script>alert('添加成功!')</script>", 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();
}
热心网友
时间:2023-10-08 22:25
只能告诉方法,代码没有时间了,Add.aspx页的增加事件中,读取数据,保存到数据库,并跳转到list.aspx, List.aspx页的Page_Load()中进行数据查询,并将结果绑定到gridview;
如果仅仅是思路的话,感觉这个问题太过于简单。。。
热心网友
时间:2023-10-08 22:25
用javascript 在数据进行绑定时,注册一下javascript事件,然后就不用我说了吧,当然是点击这个事件,得到点击的值,然后传给你要传的对象哦....希望对你有帮助..