标签:
aspx里的代码,仔细看一下触发js事件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server" OnEditCommand="DataList1_EditCommand" OnUpdateCommand="DataList1_UpdateCommand" OnItemDataBound="DataList1_ItemDataBound" OnCancelCommand="DataList1_CancelCommand" OnDeleteCommand="DataList1_DeleteCommand"> <HeaderTemplate> <table border="1" width="100%"><tr><td width="100px"></td><td width="100px">编号</td><td width="250px">名称</td><td>价格</td><td width="100px">系列</td></tr></table> </HeaderTemplate> <ItemTemplate> <table border="1" width="100%"> <tr> <td width="100px"> <asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit">编辑</asp:LinkButton> <asp:LinkButton ID="LinkButton2" runat="server" CommandName="delete">删除</asp:LinkButton> </td> <td width="100px"> <%-- <asp:HiddenField ID="HiddenField1" runat="server" Value=‘<%#Eval("Code")%>‘ />--%> <asp:Label ID="Label1" runat="server" Text=‘<%#Eval("Code") %>‘></asp:Label> </td> <td width="250px"><%#Eval("Name") %></td> <td><%#Eval("Price") %></td> <%-- <td><%#Eval("Brand") %></td>--%> <td width="100px"> <asp:Literal ID="Literal1" runat="server" Text=‘ <%#Eval("Brand") %>‘></asp:Literal></td> </tr> </table> </ItemTemplate> <%-- //编辑项模板--%> <EditItemTemplate> <table border="1" width="100%"> <tr> <td width="100px"> <asp:LinkButton ID="LinkButton1" runat="server" CommandName="update">更新</asp:LinkButton> <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Cancel">取消</asp:LinkButton> </td> <td width="100px"> <asp:TextBox ID="TextBox1" runat="server" Width="100px" Text=‘<%#Eval("Code") %>‘></asp:TextBox></td> <td width="100px"> <asp:TextBox ID="TextBox2" runat="server" Width="250px" Text=‘<%#Eval("Name") %>‘></asp:TextBox></td> <td> <asp:TextBox ID="TextBox3" runat="server" Text=‘<%#Eval("Price") %>‘></asp:TextBox></td> <td width="100px"> <input id="Hidden1" type="hidden" runat="server" value=‘<%#Eval("brand") %>‘ /> <asp:DropDownList ID="DropDownList1" runat="server" Width="130px"></asp:DropDownList></td> </tr> </table> </EditItemTemplate> <FooterTemplate> </FooterTemplate> </asp:DataList> </div> <span id="span" style="display:none;"> 编号:<asp:TextBox ID="TextBox4" runat="server" Width="80px" Height="10px"></asp:TextBox> 名称:<asp:TextBox ID="TextBox5" runat="server" Width="80px" Height="10px"></asp:TextBox> 价格:<asp:TextBox ID="TextBox6" runat="server" Width="80px" Height="10px"></asp:TextBox> 系列:<asp:DropDownList ID="DropDownList2" runat="server" Width="80px" ></asp:DropDownList> <input id="Hidden1" type="hidden" value=‘<%#Eval("brand") %>‘ /><asp:Button ID="Button2" runat="server" Text="添加" OnClick="Button2_Click" /> </span> <%--触发js事件时服务器按钮不能用,应该用客户端按钮,血的教训啊 --%> <asp:Button ID="Button1" runat="server" Text="添加新信息" OnClientClick="xianshi()" OnClick="Button1_Click1" /> <%--<div id="bb"><input id="Button3" type="button" value="添加新信息" onclick="xianshi()" /></div>--%> </form> </body> </html> <script> function xianshi() { var a = document.getElementById("span"); a.style.display = "block"; //下面这两句也是改变样式的方法 //var a = document.getElementById("span"); //a.removeAttribute("style"); var b = document.getElementById("bb"); b.setAttribute("style", "visibility:hidden"); } </script>
aspx.cs里代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataList1.DataSource = new CarBF().Select(); DataList1.DataBind(); } } //点击编辑的时候触发这个事件 protected void DataList1_EditCommand(object source, DataListCommandEventArgs e) { // e.Item.ItemIndex 获取选择中的数据的索引 DataList1.EditItemIndex = e.Item.ItemIndex; DataList1.DataSource = new CarBF().Select(); DataList1.DataBind(); } //点击修改的时候触发这个事件 protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e) { //获取文本框的值,就可以调用修改方法 TextBox txt =(TextBox) e.Item.FindControl("TextBox1"); TextBox txt1 = (TextBox)e.Item.FindControl("TextBox2"); TextBox txt2 = (TextBox)e.Item.FindControl("TextBox3"); DropDownList txt3 = (DropDownList)e.Item.FindControl("DropDownList1"); Car data = new Car(); data.Code = txt.Text; data.Name = txt1.Text; data.Price =Convert.ToDecimal( txt2.Text); data.Brand = txt3.Text ; new CarBF().Update(data); //更新结束后把选项版的索引改为-1,就是关掉选项板 DataList1.EditItemIndex = -1; DataList1.DataSource = new CarBF().Select(); DataList1.DataBind(); } //当绑定数据时触发, protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { //下面是两个表查询,在数据绑定的时候触发,通过主键表里的系列号查询外键表里的系列名称 if (e.Item.ItemIndex > -1) { //当数据开始绑定的时候,判断一下,绑定的数据是否为编辑栏的那一行数据 if (e.Item.ItemType == ListItemType.EditItem) { List<Car> list = new CarBF().Select(); //因为Hidden1是服务器控件,所以获取隐藏域的值要用 HtmlInputHidden HtmlInputHidden h = (HtmlInputHidden)e.Item.FindControl("Hidden1"); DropDownList dp = (DropDownList)e.Item.FindControl("DropDownList1"); dp.DataSource = list; dp.DataTextField = "brand"; dp.SelectedValue = h.Value; dp.DataBind(); } else { Literal a = (Literal)e.Item.FindControl("Literal1"); a.Text = new BrandBF().Select(a.Text).Brand_Name; } } } //当点击取消的时候 protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e) { Response.Redirect("Default.aspx"); } //当点击删除的时候 protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e) { //获取label1里的值,然后通过这个值调用删除方法,最后再重新绑定 Label hh = (Label)e.Item.FindControl("Label1"); string code = hh.Text.ToString(); new CarBF().Delete(code); DataList1.DataSource = new CarBF().Select(); DataList1.DataBind(); } //当点击添加信息的时候 protected void Button2_Click(object sender, EventArgs e) { Car data = new Car(); data.Code = TextBox4.Text; data.Name = TextBox5.Text; data.Price =Convert.ToDecimal( TextBox6.Text); data.Brand = DropDownList2.Text; new CarBF().Insert(data); DataList1.DataSource = new CarBF().Select(); DataList1.DataBind(); } protected void Button1_Click1(object sender, EventArgs e) { this.Button1.Visible = false; List<Car> list = new CarBF().Select(); foreach (Car data1 in list) { DropDownList2.Items.Add(data1.Brand); } //DropDownList2.DataSource = list; //DropDownList2.DataBind(); } }
datalist 的用法。也是增删改查,但是比较智能。用数据绑定的方式,可以有不同的显示方法,下面是对一个表的增删改查的参考代码
标签:
原文地址:http://www.cnblogs.com/275147378abc/p/4692942.html