
ASP.NET C#关于GridView控件 请各位大大们帮帮忙 15
我的思路是在页面1中添加一个GridView控件显示数据库中的内容,然后当我点击GridView控件中某一行时把这一行中的所有值传递到页面2中的table中各自的td中。...
我的思路是在页面1中添加一个GridView控件显示数据库中的内容,然后当我点击GridView控件中某一行时把这一行中的所有值传递到页面2中的table中各自的td中。
请各位帮帮忙最好是完整的代码帖上来或者是直接把程序发伙邮箱里81577017@qq.com
万分感谢~!!!!!! 展开
请各位帮帮忙最好是完整的代码帖上来或者是直接把程序发伙邮箱里81577017@qq.com
万分感谢~!!!!!! 展开
4个回答
展开全部
很简单啊。
我现在做的项目的一个实例给你吧。
1.Gridview
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="PlantDBID"
OnRowDeleting="GridView1_RowDeleting" HeaderStyle-BackColor="Green"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField HeaderText="数据库ID" DataField="PlantDBID" />
<asp:BoundField HeaderText="数据库标题" DataField="PlantDBName" />
<asp:BoundField HeaderText="图片" DataField="Image" />
<asp:BoundField HeaderText="介绍" DataField="Introduction" />
<asp:TemplateField>
<HeaderTemplate>
操作</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
Text="删除" OnClientClick='<%# "if (!confirm(\"你确定要删除" + Eval("PlantDBName").ToString() + "吗?\")) return false;"%>'></asp:LinkButton>
<a id="A1" href='<%#"DisplantPlantDB.aspx?id="+Eval("PlantDBID") %>' runat="server">修改</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
</asp:GridView>
这里的重点是: <a id="A1" href='<%#"DisplantPlantDB.aspx?id="+Eval("PlantDBID") %>' runat="server">修改</a>
这句话的意思说,把当前的行的ID 传递到DisplantPlantDB.aspx这个页面中。
2.在DisplantPlantDB.aspx这个页面中
放进table
<table>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
</table>
3..在DisplantPlantDB.aspx这个页面中 绑定数据
3.1 string id = Request["id"]; 获取那边传过来的ID
3.2if (id != null)
{
BindData(id);
}
3.3 绑定事件
protected void BindData(string id)
{
PlantDBBLL plantBLL = new PlantDBBLL();//三层架构的一个业务处理层
PlantDB plant = plantBLL.GetPlantDBByID(id);//获取数据
TextBox1.Text = plant.PlantDBName;//开始绑定
TextBox2.Text = plant.Introduction;
}
很简单吧。有问题在讨论,节日快乐。
我现在做的项目的一个实例给你吧。
1.Gridview
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="PlantDBID"
OnRowDeleting="GridView1_RowDeleting" HeaderStyle-BackColor="Green"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField HeaderText="数据库ID" DataField="PlantDBID" />
<asp:BoundField HeaderText="数据库标题" DataField="PlantDBName" />
<asp:BoundField HeaderText="图片" DataField="Image" />
<asp:BoundField HeaderText="介绍" DataField="Introduction" />
<asp:TemplateField>
<HeaderTemplate>
操作</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
Text="删除" OnClientClick='<%# "if (!confirm(\"你确定要删除" + Eval("PlantDBName").ToString() + "吗?\")) return false;"%>'></asp:LinkButton>
<a id="A1" href='<%#"DisplantPlantDB.aspx?id="+Eval("PlantDBID") %>' runat="server">修改</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
</asp:GridView>
这里的重点是: <a id="A1" href='<%#"DisplantPlantDB.aspx?id="+Eval("PlantDBID") %>' runat="server">修改</a>
这句话的意思说,把当前的行的ID 传递到DisplantPlantDB.aspx这个页面中。
2.在DisplantPlantDB.aspx这个页面中
放进table
<table>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
</table>
3..在DisplantPlantDB.aspx这个页面中 绑定数据
3.1 string id = Request["id"]; 获取那边传过来的ID
3.2if (id != null)
{
BindData(id);
}
3.3 绑定事件
protected void BindData(string id)
{
PlantDBBLL plantBLL = new PlantDBBLL();//三层架构的一个业务处理层
PlantDB plant = plantBLL.GetPlantDBByID(id);//获取数据
TextBox1.Text = plant.PlantDBName;//开始绑定
TextBox2.Text = plant.Introduction;
}
很简单吧。有问题在讨论,节日快乐。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
-------页面1------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True"
onselectedindexchanging="GridView1_SelectedIndexChanging"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="CustomerName" HeaderText="CustomerName" />
<asp:BoundField DataField="telephone" HeaderText="telephone" />
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//绑定数据
using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=ComputerShop"))
{
SqlDataAdapter da = new SqlDataAdapter("select CustomerName,telephone from dbo.CustomerInfo", conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
//实例化实体类
Customer c = new Customer();
//将GridView值传入实体类中
c.Customername = GridView1.Rows[e.NewSelectedIndex].Cells[0].Text;
c.Telephone = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
//将实体类存入Session对象
Session["c"] = c;
//页面跳转
Server.Transfer("Default2.aspx");
}
}
------------页面2--------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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 runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr><td> 客户姓名</td><td>客户电话</td></tr>
<tr>
<td><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
<td><asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Customer c = Session["c"] as Customer;
Label1.Text = c.Customername;
Label2.Text = c.Telephone;
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True"
onselectedindexchanging="GridView1_SelectedIndexChanging"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="CustomerName" HeaderText="CustomerName" />
<asp:BoundField DataField="telephone" HeaderText="telephone" />
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//绑定数据
using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=ComputerShop"))
{
SqlDataAdapter da = new SqlDataAdapter("select CustomerName,telephone from dbo.CustomerInfo", conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
//实例化实体类
Customer c = new Customer();
//将GridView值传入实体类中
c.Customername = GridView1.Rows[e.NewSelectedIndex].Cells[0].Text;
c.Telephone = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
//将实体类存入Session对象
Session["c"] = c;
//页面跳转
Server.Transfer("Default2.aspx");
}
}
------------页面2--------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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 runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr><td> 客户姓名</td><td>客户电话</td></tr>
<tr>
<td><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
<td><asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Customer c = Session["c"] as Customer;
Label1.Text = c.Customername;
Label2.Text = c.Telephone;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
风逝19900311 正解,思路:点击GridView触发事件,可以是PageIndexChanging,updateing,selecting等都可以,关键是GridViewPageEventArgs e可以获得你选择的那行,还可以用GridView.rows[e.index].cells[i]或得改行每列的数据
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<asp:GridView ID="gv" runat="server"
CellPadding="3"
GridLines="Horizontal" BackColor="White" BorderColor="#E7E7FF"
BorderStyle="None" BorderWidth="1px" Height="150px" Width="329px"
AllowPaging="True" onpageindexchanging="gv_PageIndexChanging" PageSize="6" >
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
string connString = "uid=sa;pwd=1;database=shuju;server=.";
SqlConnection conn = new SqlConnection(connString);
DataTable dt = new DataTable();
string s = "select * from jieshao";
da = new SqlDataAdapter(s, conn);
da.Fill(dt);
this.gv.PageIndex = e.NewPageIndex;
gv.DataSource = dt;
gv.DataBind();
}
gv是grid view的简写,希望你能看懂,能帮到你。
CellPadding="3"
GridLines="Horizontal" BackColor="White" BorderColor="#E7E7FF"
BorderStyle="None" BorderWidth="1px" Height="150px" Width="329px"
AllowPaging="True" onpageindexchanging="gv_PageIndexChanging" PageSize="6" >
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
string connString = "uid=sa;pwd=1;database=shuju;server=.";
SqlConnection conn = new SqlConnection(connString);
DataTable dt = new DataTable();
string s = "select * from jieshao";
da = new SqlDataAdapter(s, conn);
da.Fill(dt);
this.gv.PageIndex = e.NewPageIndex;
gv.DataSource = dt;
gv.DataBind();
}
gv是grid view的简写,希望你能看懂,能帮到你。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询