我用了Gridview,也绑定了数据库,但不知道该怎样在后台写代码?

 我来答
宋高通
2010-12-28 · 超过89用户采纳过TA的回答
知道答主
回答量:242
采纳率:0%
帮助的人:178万
展开全部
后台:
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;
using System.Data.SqlClient;
using JH.Game.ServiceLibrary;

public partial class Manage_Examin_FillBlankManage : System.Web.UI.Page
{
private int moduleid = 12;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

if (this.Session["UserName"] == null)
{
Response.Write(" <script type=text/javascript> alert('您没有登录或登录超时,请重新登录!');window.top.opener=null; window.top.close(1);window.open('../../Login.aspx'); </script> ");
}
else
{
this.form1.Visible = false;
Authority(moduleid);
}
Bind();
Bind1();
Session.Remove("newdb");
}
}

public void Authority(int moduleid)
{
Userinfo users = new Userinfo();
UserinfoBll usersbll = new UserinfoBll();
string UserName = Session["UserName"].ToString();
users = usersbll.GetModelByName(UserName);
int uid = users.id;
UserRole usersrole = new UserRole();
UserRoleBll usersrolebll = new UserRoleBll();
usersrole = usersrolebll.GetModelByUser(uid);
if (usersrole == null)
{
Response.Write("<script> alert('您暂无该权限!');location='firstWeb.aspx';</script>");
}
else
{
string rid = usersrole.urroleid.ToString();
RoleuserAuth roleuAuth = new RoleuserAuth();
RoleuserAuthBll roleuAuthBll = new RoleuserAuthBll();
roleuAuth = roleuAuthBll.GetModelById(Convert.ToInt32(rid), moduleid);
if (roleuAuth == null)
{
Response.Write("<script> alert('您暂无该权限!');location='firstWeb.aspx';</script>");
}
else
{
string flag = roleuAuth.flag.ToString();
if (flag.Equals("0"))
{
this.form1.Visible = true;
Bind();
}
else
{
Response.Write("<script> alert('您暂无该权限!');location='firstWeb.aspx';</script>");
}
}
}
}

FillBlankProblem fillbleanproblem = new FillBlankProblem();
FillBlankProblemBll fillbleanproblemBll = new FillBlankProblemBll();
public void Bind()
{
DataSet da = fillbleanproblemBll.Gets();
da.AcceptChanges();
this.GvExamin.DataSource = da;
this.GvExamin.DataBind();
this.ddlCurrentPage.Items.Clear();

for (int i = 1; i <= this.GvExamin.PageCount; i++)
{
this.ddlCurrentPage.Items.Add(i.ToString());
}
this.ddlCurrentPage.SelectedIndex = this.GvExamin.PageIndex;

if (da.Tables[0].Rows.Count == 0)
{
this.notxt.Text = "没有相关数据!";
}

}
public void Bind1()
{
CourseBll courseBll = new CourseBll();
DataSet da = courseBll.Gets();
this.ddlCourse.DataSource = da;
ddlCourse.DataValueField = "ID";
ddlCourse.DataTextField = "Name";
ddlCourse.DataBind();
ddlCourse.Items.Insert(0, new ListItem("--请选择--", ""));
}

protected void ddlCourse_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet res;
string id = this.ddlCourse.SelectedValue;
if (this.ddlCourse.SelectedIndex == 0)
{
res = fillbleanproblemBll.Gets();
}
else
{
res = fillbleanproblemBll.GetByID(int.Parse(id));
}
this.GvExamin.DataSource = res;
this.GvExamin.DataBind();

string newdb = "123";
Session.Add("newdb", newdb);
this.ddlCurrentPage.Items.Clear();
for (int i = 1; i <= this.GvExamin.PageCount; i++)
{
this.ddlCurrentPage.Items.Add(i.ToString());
}

if (this.GvExamin.Rows.Count != 0)
{
this.ddlCurrentPage.SelectedIndex = this.GvExamin.PageIndex;
}
else
{

this.notxt.Text = "没有相关数据!";
}
}

protected void GvExamin_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GvExamin.PageIndex = e.NewPageIndex;
GvExamin.DataBind();
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)
{
//鼠标移动到每项时颜色交替效果
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";
}

//如果是绑定数据行
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('你确认要删除吗?')");
}
}
}
protected void ddlCurrentPage_SelectedIndexChanged(object sender, EventArgs e)
{
this.GvExamin.PageIndex = this.ddlCurrentPage.SelectedIndex;
Bind();
if (Session["newdb"] == "123")
{
ddlCourse_SelectedIndexChanged(sender, e);
}
}

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

if (Session["newdb"] == "123")
{
ddlCourse_SelectedIndexChanged(sender, e);
}
}
protected void lnkbtnPre_Click(object sender, EventArgs e)
{
if (this.GvExamin.PageIndex > 0)
{
this.GvExamin.PageIndex = this.GvExamin.PageIndex - 1;
Bind();

if (Session["newdb"] == "123")
{
ddlCourse_SelectedIndexChanged(sender, e);
}
}
}
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
if (this.GvExamin.PageIndex < this.GvExamin.PageCount)
{
this.GvExamin.PageIndex = this.GvExamin.PageIndex + 1;
Bind();

if (Session["newdb"] == "123")
{
ddlCourse_SelectedIndexChanged(sender, e);
}
}
}
protected void lnkbtnLast_Click(object sender, EventArgs e)
{
this.GvExamin.PageIndex = this.GvExamin.PageCount;
Bind();

if (Session["newdb"] == "123")
{
ddlCourse_SelectedIndexChanged(sender, e);
}
}
protected void GvExamin_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int ID = Convert.ToInt32(GvExamin.DataKeys[e.RowIndex].Value.ToString());
int res = fillbleanproblemBll.Delete(ID);
if (res > 0)
{
GvExamin.EditIndex = -1;
Bind();
}
}
}

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

<!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" />

</head>
<body style="background-color:#bfe2ff">
<form id="form1" runat="server">

<div>

<table width ="99%">

<tr>
<td align="center" style=" height:30px; line-height:30px; font-size:15px; font-weight:bold; text-align:center">百问百答</td>
</tr>
<tr>
<td align ="left">
问题类型:<asp:DropDownList ID="ddlCourse" runat="server" AutoPostBack="True" onselectedindexchanged="ddlCourse_SelectedIndexChanged"
>
</asp:DropDownList>
       
     </td>
</tr>
</table>

<asp:GridView ID="GvExamin" GridLines="None"
runat="server"
AutoGenerateColumns="False" Width="99%" AllowPaging="True"
CssClass="yui-datatable-theme"
DataKeyNames="ID"
PageSize="12" onpageindexchanging="GvExamin_PageIndexChanging"
onrowdatabound="GvExamin_RowDataBound"
onrowdeleting="GvExamin_RowDeleting" >
<RowStyle CssClass="data-row" />
<AlternatingRowStyle CssClass="alt-data-row" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="序号" />
<asp:BoundField DataField="CourseID" HeaderText="所属" Visible="false" />
<asp:BoundField DataField="FrontTitle" HeaderText="问题" />
<asp:BoundField DataField="Answer" HeaderText="答案"/>
<asp:HyperLinkField DataNavigateUrlFormatString="FillBlankUpdate.aspx?ID={0}"
HeaderText="详细信息" Text="详细信息" DataNavigateUrlFields="ID" />
<asp:HyperLinkField DataNavigateUrlFormatString="FillBlankUpdate.aspx?ID={0}"
HeaderText="编辑" Text="编辑" DataNavigateUrlFields="ID" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<%--<FooterStyle CssClass="EG_FooterCSS" />
<SelectedRowStyle CssClass="GridSelectedItem" />
<AlternatingRowStyle CssClass="EG_AlternatingItemCSS" />

<HeaderStyle CssClass="EG_HeaderCSS" />
<RowStyle CssClass="EG_ItemCSS" />
<PagerStyle CssClass="GridViewPagerStyle" />--%>
<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>
</form>
</body>
</html>
heyao87
2010-12-28
知道答主
回答量:27
采纳率:0%
帮助的人:24.6万
展开全部
Gridview控件的后码主要写在编辑模板上的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
nz1989
2010-12-28 · TA获得超过106个赞
知道答主
回答量:172
采纳率:0%
帮助的人:51.4万
展开全部
后台代码一般为数据库的增删查改而已。
录入数据你不会么?
删数据你不会么?
数据修改呢?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
晨烟_暮霭
2010-12-28 · 超过11用户采纳过TA的回答
知道答主
回答量:30
采纳率:0%
帮助的人:0
展开全部
gridview.datasource=数据源;
gridview.databind();
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
奉旨剪辑
2010-12-28 · 超过31用户采纳过TA的回答
知道答主
回答量:85
采纳率:0%
帮助的人:72.4万
展开全部
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
bindGV();
}
}

public void bindGV()
{
DataTable dt=new DataTable();
dt=这里写你调用的方法;
Gridview.DataSourct=dt;
Gridview.DataBind();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
goblin_y
2010-12-28 · 超过39用户采纳过TA的回答
知道答主
回答量:186
采纳率:0%
帮助的人:117万
展开全部
一般不建议直接通过Gridview控件绑定数据源,可以通过
Gridview.DataSourct=dt;//dt是datatable
Gridview.DataBind();来实现数据绑定,这样在需要进行Gridview数据进行操作时,只用操作dt,操作完dt之后,再对Gridview重新进行数据绑定就行了
来自:求助得到的回答
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(5)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式