<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" pagesize="10"
width="542px" allowpaging="true" allowsorting="true"
datakeynames="db31_1,db31_2" onrowcancelingedit="gridview1_rowcancelingedit" onrowdeleting="gridview1_rowdeleting" onrowediting="gridview1_rowediting" onrowupdating="gridview1_rowupdating" onpageindexchanging="gridview1_pageindexchanging" onrowdatabound="gridview1_rowdatabound" onselectedindexchanged="gridview1_selectedindexchanged" onsorting="gridview1_sorting" >
<columns>
<asp:templatefield headertext="序号">
<itemtemplate>
<%# this.gridview1.pageindex * this.gridview1.pagesize + this.gridview1.rows.count + 1%>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="学历代码" sortexpression="db1_1">
<edititemtemplate>
<%--<asp:textbox id="textbox1" runat="server" text=<%# bind("db1_1") %>></asp:textbox>--%>
<asp:dropdownlist id ="ddlxl" runat="server" datavaluefield=<%# bind("db1_1") %>></asp:dropdownlist>
</edititemtemplate>
<itemtemplate>
<asp:label id="label1" runat="server" text=<%# bind("xuelitext") %>></asp:label>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="学历名称" sortexpression="db1_2">
<edititemtemplate>
<asp:textbox id="textbox2" runat="server" text=<%# bind("db1_2") %>></asp:textbox>
</edititemtemplate>
<itemtemplate>
<asp:label id="label2" runat="server" text=<%# bind("db1_2") %>></asp:label>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="操作" showheader="false">
<edititemtemplate>
<asp:linkbutton id="linkbutton1" runat="server" causesvalidation="true" commandname="update"
text="更新"></asp:linkbutton>
<asp:linkbutton id="linkbutton2" runat="server" causesvalidation="false" commandname="cancel"
text="取消"></asp:linkbutton>
</edititemtemplate>
<itemtemplate>
<asp:linkbutton id="linkbutton1" runat="server" causesvalidation="false" commandname="edit"
text="编辑" onclientclick="return confirm(确认要编辑吗?);"></asp:linkbutton>
<asp:linkbutton id="linkbutton3" runat="server" causesvalidation="false" commandname="delete"
text="删除" onclientclick="return confirm(确认要删除吗?);"></asp:linkbutton>
<asp:linkbutton id="linkbutton2" runat="server" causesvalidation="false" commandname="select"
text="选择"></asp:linkbutton>
</itemtemplate>
</asp:templatefield>
</columns>
<alternatingrowstyle backcolor="aquamarine" />
</asp:gridview>
/// <summary>
/// 绑定数据到gridview
/// </summary>
private void gridviewbind()
{
检索数据库
string strsql = "select * from db1";
得到数据集
this.gridview1.datasource=conn.getds(strsql).tables[0].defaultview;
this.gridview1.databind();
}
/// <summary>
/// 编辑当前行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gridview1_rowediting(object sender, gridviewediteventargs e)
{
gridview1.editindex = e.neweditindex;
//当前编辑行背景色高亮
this.gridview1.editrowstyle.backcolor = color.fromname("#f7ce90");
gridviewbind();
}
/// <summary>
/// 取消编辑状态
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gridview1_rowcancelingedit(object sender, gridviewcancelediteventargs e)
{
gridview1.editindex = -1;
gridviewbind();
}
/// <summary>
/// 删除记录过程
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gridview1_rowdeleting(object sender, gridviewdeleteeventargs e)
{
//得到单位编号
string rowtodelete = gridview1.datakeys[e.rowindex].values[0].tostring();
//转换为整数
//int id=convert.toint32(rowtodelete);
//从数据库中删除
string str = "delete from db1 where db1_1=" + "" + rowtodelete + "" + "";
try
{
conn.runsql(str);
//重新绑定数据
gridviewbind();
}
catch (exception ex)
{
response.write("数据库错误,错误原因:" + ex.message);
response.end();
}
}
/// <summary>
/// 更新记录过程
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gridview1_rowupdating(object sender, gridviewupdateeventargs e)
{
string id = gridview1.datakeys[e.rowindex].values[0].tostring();
string db1_1 = ((textbox)gridview1.rows[e.rowindex].findcontrol("textbox1")).text;
//string db1_2 = ((textbox)gridview1.rows[e.rowindex].findcontrol("textbox2")).text;
string db1_2 = (((dropdownlist))gridview1.rows[e.rowindex].findcontrol("ddlxl")).selecteditem.text;
//判断表单项是否有空并给出提示信息
if (db1_1 == "" || db1_2 == "")
{
conn.alert("请输入完整信息!", page);
return;
}
try
{
conn.builderedit("select * from db1 where db1_1 =" + id + "");
conn.dr["db1_1"] = db1_1;
conn.dr["db1_2"] = db1_2;
conn.buildereditclose();
}
catch (oracleexception err)
{
if (err.code.tostring() == "1")
conn.alert("错误:已存在具有相同主键的记录", page);
else
conn.alert("错误:未能添加记录", page);
}
response.write("<script language=javascript>alert(数据已被保存!);</script>");
//返回浏览状态
gridview1.editindex = -1;
gridviewbind();
}
/// <summary>
/// 分页事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gridview1_pageindexchanging(object sender, gridviewpageeventargs e)
{
gridview1.pageindex = e.newpageindex;
gridviewbind();
}
/// <summary>
/// 加入鼠标效果及为dropdownlist绑定值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gridview1_rowdatabound(object sender, gridviewroweventargs e)
{
//为dropdownlist绑定值
if (((dropdownlist)e.row.findcontrol("ddlxl")) != null)
{
dropdownlist ddlxl = (dropdownlist)e.row.findcontrol("ddlxl");
ddlxl.items.clear();
ddlxl.items.add(new listitem("博士", "1"));
ddlxl.items.add(new listitem("硕士", "2"));
ddlxl.items.add(new listitem("学士", "3"));
}
//加入鼠标滑过的高亮效果
if (e.row.rowtype == datacontrolrowtype.datarow)//判定当前的行是否属于datarow类型的行
{
//当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色
e.row.attributes.add("onmouseover", "currentcolor=this.style.backgroundcolor;this.style.backgroundcolor=yellow,this.style.fontweight=;");
//当鼠标离开的时候 将背景颜色还原的以前的颜色
e.row.attributes.add("onmouseout", "this.style.backgroundcolor=currentcolor,this.style.fontweight=;");
}
//单击行改变行背景颜色
if (e.row.rowtype == datacontrolrowtype.datarow)
{
e.row.attributes.add("onclick", "this.style.backgroundcolor=#99cc00; this.style.color=buttontext;this.style.cursor=default;");
}
}
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




