.net 怎末从一个页面往另一个页面的Gridview里边传值,并且将数据同时添加到数据库里
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.net 怎末从一个页面往另一个页面的Gridview里边传值,并且将数据同时添加到数据库里相关的知识,希望对你有一定的参考价值。
用户往上边页面(Add.aspx)输入,点击“确定”之后数据会自动添加到下边的页面(List.aspx)的gridview里边中显示,并且用户输入的数据会在数据库Information表里保存,该怎么写啊,请高手帮忙,谢谢。
答案满意的话还会追加分的
我是初学者,可以话请回答问题者把代码附上,谢谢
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> 参考技术A 下面是个例子,我是用分层的方法实现的,不然代码不好维护啊
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();
参考技术B 只能告诉方法,代码没有时间了,Add.aspx页的增加事件中,读取数据,保存到数据库,并跳转到list.aspx, List.aspx页的Page_Load()中进行数据查询,并将结果绑定到gridview;
如果仅仅是思路的话,感觉这个问题太过于简单。。。本回答被提问者和网友采纳 参考技术C 用javascript 在数据进行绑定时,注册一下javascript事件,然后就不用我说了吧,当然是点击这个事件,得到点击的值,然后传给你要传的对象哦....希望对你有帮助..
IOS开发-UI学习-delegate(代理)的使用
代理是IOS开发中用到的一种设计模式。今天做了一个代理的小练习:
以下项目实现了两个页面之间的相互切换,并且在切换页面的时候完成了从一个页面往另一个页面的传值。从主页面往其他页面传值是容易的,但是反过来从其他页面往主页面传值就难了,要用到delegate设计模式。
新建一个ios的项目,然后再新建一个ViewController,起名为TwoViewController,这样,加上新建项目时默认生成的那个ViewController,总共有两个ViewController,其中新建项目默认生成的那个是主ViewController。
为了使用代理模式,我们要先定义一个协议,这个协议如下:
1 #import <Foundation/Foundation.h> 2 #import <UIKit/UIKit.h> 3 //定义一个协议,协议中定义一个传值的方法 4 //定义一个协议,协议中定义一个传值的方法 5 //定义一个协议,协议中定义一个传值的方法 6 @protocol Mydelegate <NSObject> 7 8 @required 9 -(void)changeValue:(NSString *)stringnow; 10 11 @end
协议中定义了一个传值的函数,用来把第二个ViewController中的值传给第一个ViewController,如果不是用代理,我们只能从第一个ViewController向第二个ViewController传值,而不能反过来,从第二个ViewController给第一个ViewController传值(具体原因没理解,以后理解了再补上)
完成协议后让第一个ViewController遵守这个协议,也就是在第一个ViewController的.h文件中声明第一个ViewController遵守这个协议<Mydelegate>:
1 #import <UIKit/UIKit.h> 2 3 //第一个页面应用Mydelegate协议 4 //第一个页面应用Mydelegate协议 5 //第一个页面应用Mydelegate协议 6 #import "Mydelegate.h" 7 @interface ViewController : UIViewController <Mydelegate> 8 9 10 @end
接下来在第一个ViewController的.m文件中实现changeValue这个传值方法:
1 #import "ViewController.h" 2 #import "TwoViewController.h" 3 @interface ViewController (){ 4 UITextField *textFieldnow; 5 6 } 7 8 @end 9 10 11 12 @implementation ViewController 13 14 //第一个页面引用Mydelegate协议,故要在.m文件中实现Mydelegate中定义的必须要实现的方法 15 //第一个页面引用Mydelegate协议,故要在.m文件中实现Mydelegate中定义的必须要实现的方法 16 //第一个页面引用Mydelegate协议,故要在.m文件中实现Mydelegate中定义的必须要实现的方法 17 -(void)changeValue:(NSString *)stringnow{ 18 textFieldnow.text=stringnow; 19 } 20 21 22 23 - (void)viewDidLoad { 24 [super viewDidLoad]; 25 // 初始化textFieldnow 26 textFieldnow = [[UITextField alloc]initWithFrame:CGRectMake(50 , 200, 200, 50)]; 27 28 // 设置textFieldnow的背景颜色 29 textFieldnow.backgroundColor = [UIColor colorWithRed:0.139 green:0.760 blue:1.000 alpha:1.000]; 30 31 // 为textFieldnow添加输入完成后让键盘消失的事件 32 [textFieldnow addTarget:self action:@selector(losefirstresponsder:) forControlEvents:UIControlEventEditingDidEndOnExit]; 33 34 // 把textFieldnow添加到view中 35 [self.view addSubview:textFieldnow]; 36 37 38 39 } 40 41 42 //本函数的功能是Textfield在输入完后按return让虚拟键盘消失 43 -(void)losefirstresponsder:(id)sender{ 44 [self resignFirstResponder]; 45 } 46 47 48 - (IBAction)TurnToNext:(id)sender { 49 TwoViewController *twoView = [[TwoViewController alloc]init]; 50 51 // 委托机制最关键的部分,在跳转到第二个界面之前把第二个界面的委托对象指向自己 52 // 委托机制最关键的部分,在跳转到第二个界面之前把第二个界面的委托对象指向自己 53 // 委托机制最关键的部分,在跳转到第二个界面之前把第二个界面的委托对象指向自己 54 twoView.delegate = self; 55 56 57 // 把第一个界面的textFieldnow的值传给第二个界面中的namestring 58 twoView.namestring = textFieldnow.text; 59 60 61 // 跳转到第二个界面 62 [self presentViewController:twoView animated:YES completion:nil]; 63 64 } 65 66 - (void)didReceiveMemoryWarning { 67 [super didReceiveMemoryWarning]; 68 // Dispose of any resources that can be recreated. 69 } 70 71 @end
第二个ViewController的.h文件如下:
1 #import <UIKit/UIKit.h> 2 #import "Mydelegate.h" 3 4 @interface TwoViewController : UIViewController 5 6 @property(nonatomic,retain )NSString *namestring; 7 //定义一个变量delegate 8 //定义一个变量delegate 9 //定义一个变量delegate 10 @property(nonatomic,weak) id <Mydelegate> delegate; 11 12 @end
第二个ViewController的.m文件如下:
1 #import "TwoViewController.h" 2 3 @interface TwoViewController (){ 4 UILabel *label; 5 UITextField *textFieldnow1; 6 7 } 8 9 @end 10 11 @implementation TwoViewController 12 @synthesize namestring; 13 @synthesize delegate; 14 - (void)viewDidLoad { 15 [super viewDidLoad]; 16 // Do any additional setup after loading the view from its nib. 17 label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 50)]; 18 [self.view addSubview:label]; 19 label.text = namestring; 20 textFieldnow1 = [[UITextField alloc]initWithFrame:CGRectMake(50 , 200, 200, 50)]; 21 22 textFieldnow1.backgroundColor = [UIColor colorWithRed:0.139 green:0.760 blue:1.000 alpha:1.000]; 23 [textFieldnow1 addTarget:self action:@selector(losefirstresponsder:) forControlEvents:UIControlEventEditingDidEndOnExit]; 24 [self.view addSubview:textFieldnow1]; 25 26 27 } 28 - (IBAction)BackToHome:(id)sender { 29 [self dismissViewControllerAnimated:YES completion:nil]; 30 [delegate changeValue:textFieldnow1.text]; 31 } 32 33 -(void)losefirstresponsder:(id)sender{ 34 [self resignFirstResponder]; 35 } 36 37 - (void)didReceiveMemoryWarning { 38 [super didReceiveMemoryWarning]; 39 // Dispose of any resources that can be recreated. 40 } 41 @end
要实现通过代理让第二个页面传递一个值给第一个页面,最关键的一点就是在第一个页面中要有一个指针指向self,也就是twoView.delegate = self;这条语句:
1 // 委托机制最关键的部分,在跳转到第二个界面之前把第二个界面的委托对象指向自己 2 // 委托机制最关键的部分,在跳转到第二个界面之前把第二个界面的委托对象指向自己 3 // 委托机制最关键的部分,在跳转到第二个界面之前把第二个界面的委托对象指向自己 4 twoView.delegate = self;
而协议的作用就是提供了一个函数,起到了桥梁的作用。
以上是关于.net 怎末从一个页面往另一个页面的Gridview里边传值,并且将数据同时添加到数据库里的主要内容,如果未能解决你的问题,请参考以下文章