我用了Gridview,也绑定了数据库,但不知道该怎样在后台写代码?
7个回答
展开全部
后台:
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>
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>
展开全部
Gridview控件的后码主要写在编辑模板上的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
后台代码一般为数据库的增删查改而已。
录入数据你不会么?
删数据你不会么?
数据修改呢?
录入数据你不会么?
删数据你不会么?
数据修改呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
gridview.datasource=数据源;
gridview.databind();
gridview.databind();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
bindGV();
}
}
public void bindGV()
{
DataTable dt=new DataTable();
dt=这里写你调用的方法;
Gridview.DataSourct=dt;
Gridview.DataBind();
}
{
if(!IsPostBack)
{
bindGV();
}
}
public void bindGV()
{
DataTable dt=new DataTable();
dt=这里写你调用的方法;
Gridview.DataSourct=dt;
Gridview.DataBind();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一般不建议直接通过Gridview控件绑定数据源,可以通过
Gridview.DataSourct=dt;//dt是datatable
Gridview.DataBind();来实现数据绑定,这样在需要进行Gridview数据进行操作时,只用操作dt,操作完dt之后,再对Gridview重新进行数据绑定就行了
Gridview.DataSourct=dt;//dt是datatable
Gridview.DataBind();来实现数据绑定,这样在需要进行Gridview数据进行操作时,只用操作dt,操作完dt之后,再对Gridview重新进行数据绑定就行了
来自:求助得到的回答
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询