在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
使用ListView控件进行修改,删除与添加操作 <asp:ListView ID="lv2" runat="server" onpagepropertieschanging="lv_PagePropertiesChanging2" onitemcanceling="lv2_ItemCanceling" onitemediting="lv2_ItemEditing" onitemupdating="lv2_ItemUpdating" onitemdeleting="lv2_ItemDeleting" DataKeyNames="id"> <EditItemTemplate> <div class="lvData"> <div class="id"><%#eval_r("id")%></div> <div class="proId"><%#eval_r("provinceID")%></div> <div class="proName"> <asp:TextBox ID="txt" runat="server" Text='<%#eval_r("province")%>'></asp:TextBox></div> <div class="edit"> <asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Update" >更新</asp:LinkButton> <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Cancel" >取消</asp:LinkButton> </div> </div> </EditItemTemplate> <ItemTemplate> <div class="lvData"> <div class="id"><%#eval_r("id")%></div> <div class="proId"><%#eval_r("provinceID")%></div> <div class="proName"><%#eval_r("province")%></div> <div class="edit"> <asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Edit" >编辑</asp:LinkButton> <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete" >删除</asp:LinkButton> </div> </div> </ItemTemplate> <AlternatingItemTemplate> <div class="lvData alterStyle"> <div class="id"><%#eval_r("id")%></div> <div class="proId"><%#eval_r("provinceID")%></div> <div class="proName"><%#eval_r("province")%></div> <div class="edit"> <asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Edit" >编辑</asp:LinkButton> <asp:LinkButton ID="LinkButton3" runat="server" CommandName="Delete" >删除</asp:LinkButton> </div> </div> </AlternatingItemTemplate> </asp:ListView> <asp:DataPager ID="DataPager2" PagedControlID="lv2" runat="server"> <Fields> <asp:NextPreviousPagerField ShowFirstPageButton="True" ShowLastPageButton="True" ShowNextPageButton="True" ShowPreviousPageButton="True" /> </Fields> </asp:DataPager> 2.后置代码: public partial class index : System.Web.UI.Page { DBHelper db = new DBHelper(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack ) { Lv2Bind();//更新值 } } protected void lv_PagePropertiesChanging2(object sender, PagePropertiesChangingEventArgs e) { this.DataPager2.SetPageProperties(e.StartRowIndex, e.MaximumRows, false); Lv2Bind(); } protected void lv2_ItemEditing(object sender, ListViewEditEventArgs e) { lv2.EditIndex = e.NewEditIndex; Lv2Bind(); } protected void lv2_ItemUpdating(object sender, ListViewUpdateEventArgs e) { int index = e.ItemIndex; TextBox txtbox = lv2.Items[index].FindControl("txt") as TextBox; string proName = txtbox.Text; string id=lv2.DataKeys[index].Value.ToString(); string sql = string.Format("UPDATE [Public].[dbo].[Province] SET [province] =@province WHERE id=@id"); SqlParameter[] paras = { new SqlParameter("@province",proName), new SqlParameter("@id",id) }; if (db.ExecuteNonQuery(sql, paras) > 0) { this.divAlert.Attributes.CssStyle.Add("display", "block"); Lv2Bind(); } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>更新失败</script>"); Lv2Bind(); } } protected void lv2_ItemCanceling(object sender, ListViewCancelEventArgs e) { lv2.EditIndex = -1; Lv2Bind(); } protected void lv2_ItemDeleting(object sender, ListViewDeleteEventArgs e) { } private void Lv2Bind() { this.lv2.DataSource = GetDT(); this.lv2.DataBind(); } } 注意要点: |
请发表评论