最近一直在研究ASP.NET相关控件GridView和DetailsView的使用,通过各种失败,现在把自己做的贴出来共享【持续更新】。因为时间原因,并没有对控件所有的用法都试验,这只是九牛一毛。有错误欢迎指正。【效果图明天补】
参考资料:
1)GridView:http://blog.csdn.net/haitaodoit/article/details/7463472
2)DetailsView:http://www.jb51.net/article/25061.htm
说明:
1、实现功能
- GridView显示用户列表的具体信息,admin可对该表进行删除、编辑更新。
- DetailsView一般用来显示一个项更具体的信息,可是我只单纯的想用它来协助GridView插入数据。
2、表结构
- users(userid,username,useraddress,buildid)
- build(buildid,buildname)
3、具体代码实现
- WebForm1.aspx.cs文件内容:
namespace WebForm { public partial class WebForm1 : System.Web.UI.Page { public OracleDataClass myOracle = new OracleDataClass(); public ExcelDataClass myExcel = new ExcelDataClass(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GridViewBind(); DetailsViewBind(); } } /// <summary> /// GridView换页触发事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; GridViewBind(); } /// <summary> /// GridView绑定数据源 /// </summary> private void GridViewBind() { string sqlstr = "select * from users"; DataSet ds = myOracle.ReadFromOracle(sqlstr); if (ds != null) { GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); } } /// <summary> /// GridView取消编辑 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; GridViewBind(); } /// <summary> /// GridView 行数据项绑定时触发事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if ((Label)e.Row.FindControl("lblindex") != null) { Label index = (Label)e.Row.FindControl("lblindex"); index.Text = (e.Row.RowIndex +GridView1.PageIndex*GridView1.PageSize+ 1).ToString(); } if ((DropDownList)e.Row.FindControl("ddlbuildid") != null) { DropDownList ddlbuildid = (DropDownList)e.Row.FindControl("ddlbuildid"); string sqlstr = "select * from build"; DataSet ds = myOracle.ReadFromOracle(sqlstr); ddlbuildid.DataSource = ds.Tables[0]; ddlbuildid.DataTextField = "buildid"; ddlbuildid.DataValueField = "buildid"; ddlbuildid.DataBind(); ddlbuildid.SelectedValue = ((HiddenField)e.Row.FindControl("hdfbuildid")).Value; } } /// <summary> /// GridView 行数据删除时触发事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { Response.Write("<Script. Language=JavaScript>if(confirm('你确认要删除吗?')){window.navigate('doDelete.aspx');} </Script>"); /* string userid = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); string sqlstr = "delete from users where userid='" + userid + "'"; myOracle.WriteToOralce(sqlstr); GridView1.EditIndex = -1; GridViewBind();*/ } /// <summary> /// GridView 行数据编辑时触发事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; GridViewBind(); } /// <summary> /// GridView 行数据更新时触发事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string userid = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); string useraddress = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtuseraddress")).Text; string username = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtusername")).Text; string buildid = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlbuildid")).Text; string sqlstr = "update users set username='" + username + "',useraddress='" + useraddress + "',buildid='" + buildid + "' where userid='" + userid + "'"; myOracle.WriteToOralce(sqlstr); GridView1.EditIndex = -1; GridViewBind(); } public void SelectAll_Click(object sender,EventArgs e) { } /// <summary> /// DetailsView 绑定数据源 /// </summary> private void DetailsViewBind() { if (this.GridView1.DataSource != null) { DetailsView1.DataSource = GridView1.DataSource; DetailsView1.DataBind(); } else { string sqlstr = "select * from users"; DataSet ds = myOracle.ReadFromOracle(sqlstr); if (ds != null) { DetailsView1.DataSource = ds.Tables[0]; DetailsView1.DataBind(); } } try { ((Label)this.DetailsView1.FindControl("lblindex")).Text = (DetailsView1.Rows.Count + 1).ToString(); } catch { } lblFailureText.Text = ""; } /// <summary> /// DetailsView 模式改变触发事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e) { //this.DetailsView1.DefaultMode = e.NewMode; } /// <summary> /// 下拉菜单数据绑定触发事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void ddlbuildid_DataBinding(object sender, EventArgs e) { if (DetailsView1.CurrentMode == DetailsViewMode.Insert) { DropDownList ddl = (DropDownList)this.DetailsView1.FindControl("ddlbuildid"); if (ddl != null) { string sqlstr = "select * from build"; DataSet ds = myOracle.ReadFromOracle(sqlstr); ddl.DataSource = ds.Tables[0]; ddl.DataTextField = "buildid"; ddl.DataValueField = "buildid"; } } } /// <summary> /// DetailsView 插入项时 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e) { //先判断合理性,在插入 try { string userid = ((TextBox)this.DetailsView1.FindControl("txtuserid")).Text.Trim(); string username = ((TextBox)this.DetailsView1.FindControl("txtusername")).Text.Trim(); string useraddress = ((TextBox)this.DetailsView1.FindControl("txtuseraddress")).Text.Trim(); string buildid = ((DropDownList)this.DetailsView1.FindControl("ddlbuildid")).SelectedValue; if (userid == "") { lblFailureText.Text = "*请输入身份证号"; } else if (username == "") { lblFailureText.Text = "*请输入姓名"; } else { string sqlstr = "select count(*) from users where userid = '" + userid + "'"; DataSet ds = myOracle.ReadFromOracle(sqlstr); if (Convert.ToInt32(ds.Tables[0].Rows[0].ItemArray[0].ToString()) > 0) { lblFailureText.Text = "*身份证号已存在,请重新输入"; } else { sqlstr = "insert into users (userid,username,useraddress,buildid)" + " values('" + userid + "','" + username + "','" + useraddress + "','" + buildid + "')"; if (myOracle.WriteToOralce(sqlstr) > 0) { lblFailureText.Text = "插入成功"; GridViewBind(); } else { lblFailureText.Text = "插入失败"; } } } } catch { } } /// <summary> /// DetailsView 有项command时,例如Edit,Update,Insert,Delete /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e) { if (e.CommandName == "Cancel") { try { TextBox tbx = (TextBox)this.DetailsView1.FindControl("txtusername"); tbx.Text = ""; tbx = (TextBox)this.DetailsView1.FindControl("txtuserid"); tbx.Text = ""; tbx = (TextBox)this.DetailsView1.FindControl("txtuseraddress"); tbx.Text = ""; DropDownList ddl = (DropDownList)this.DetailsView1.FindControl("ddlbuildid"); ddl.SelectedIndex = 0; } catch { } DetailsViewBind(); } } } }
- WebForm1.aspx文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebForm.WebForm1" %> <!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> <table cellpadding="0" cellspacing="5" border="0" width="80%" style="font-size: 11px"> <tr> <td align="center" style="width:70%"> <asp:GridView ID="GridView1" runat="server" Width="100%" CellPadding="4" ForeColor="#333333" AutoGenerateColumns="False" AllowPaging="True" PageSize="5" DataKeyNames="userid" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound" > <Columns> <asp:BoundField HeaderText="身份证号" DataField="userid" Visible="false" /> <asp:TemplateField HeaderText="序号"> <ItemTemplate> <asp:Label ID="lblindex" runat="server" Width="10px" /> </ItemTemplate> <ItemStyle Width="30px" /> </asp:TemplateField> <asp:TemplateField HeaderText="姓名"> <ItemTemplate> <%# Eval("username") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtusername" Text='<%# Eval("username") %>' runat="server" Width="90px" /> </EditItemTemplate> <ItemStyle Width="100px"/> </asp:TemplateField> <asp:TemplateField HeaderText="身份证号"> <ItemTemplate> <%# Eval("userid")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtuserid" Text='<%#Eval("userid") %>' runat="server" Width="140px" /> </EditItemTemplate> <ItemStyle Width="150px" HorizontalAlign="Center"/> </asp:TemplateField> <asp:TemplateField HeaderText="地址"> <ItemTemplate> <%# Eval("useraddress") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtuseraddress" Text='<%# Eval("useraddress") %>' runat="server" Width="140px" /> </EditItemTemplate> <ItemStyle Width="150px" HorizontalAlign="Center"/> </asp:TemplateField> <asp:TemplateField HeaderText="楼号"> <ItemTemplate> <%#Eval("buildid") %> </ItemTemplate> <EditItemTemplate> <asp:HiddenField ID="hdfbuildid" runat="server" Value='<%# Eval("buildid") %>' /> <asp:DropDownList ID="ddlbuildid" runat="server" Width="90px" /> </EditItemTemplate> <ItemStyle Width="100px" HorizontalAlign="Center" /> </asp:TemplateField> <asp:CommandField ShowDeleteButton="true" ShowEditButton="true" HeaderText="操作" ItemStyle-HorizontalAlign="Center"/> </Columns> <PagerSettings FirstPageText="" LastPageText="" NextPageText="" PreviousPageText="" /> <RowStyle Height="20px" BackColor="#F7F6F3" ForeColor="#333333" /> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#999999" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> </asp:GridView> </td> <td align="center" style="width:auto"> <h5>可在此区域创建一个新的用户</h5> <asp:DetailsView ID="DetailsView1" runat="server" Width="100%" CellPadding="4" ForeColor="#333333" DataKeyNames="userid" AutoGenerateRows="false" onmodechanging="DetailsView1_ModeChanging" oniteminserting="DetailsView1_ItemInserting" DefaultMode="Insert" onitemcommand="DetailsView1_ItemCommand" > <Fields> <asp:TemplateField HeaderText="序号" HeaderStyle-Width="50px" HeaderStyle-HorizontalAlign="Right" > <ItemTemplate> <asp:Label ID="lblindex" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="姓名" HeaderStyle-Width="50px" HeaderStyle-HorizontalAlign="Right" > <EditItemTemplate> <asp:TextBox ID="txtusername" runat="server"/> </EditItemTemplate> <ItemStyle/> </asp:TemplateField> <asp:TemplateField HeaderText="身份证号" HeaderStyle-Width="50px" HeaderStyle-HorizontalAlign="Right" > <EditItemTemplate> <asp:TextBox ID="txtuserid" runat="server"/> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="地址" HeaderStyle-Width="50px" HeaderStyle-HorizontalAlign="Right" > <EditItemTemplate> <asp:TextBox ID="txtuseraddress" runat="server" /> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="楼号" HeaderStyle-Width="50px" HeaderStyle-HorizontalAlign="Right" > <EditItemTemplate> <asp:DropDownList ID="ddlbuildid" runat="server" OnDataBinding="ddlbuildid_DataBinding" /> </EditItemTemplate> </asp:TemplateField> <asp:CommandField ShowInsertButton="true" HeaderText="操作" ItemStyle-HorizontalAlign="Center"/> </Fields> <PagerSettings FirstPageText="" LastPageText="" NextPageText="" PreviousPageText="" /> <RowStyle Height="20px" BackColor="#F7F6F3" ForeColor="#333333" /> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#999999" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> </asp:DetailsView> <asp:Label ID="lblFailureText" runat="server" Text="" ForeColor="Red" Font-Size="Small"></asp:Label> </td> </tr> </table> </div> </form> </body> </html>
4、效果图