标签:style blog color io os 使用 ar strong for
使用ListView控件进行修改,删除与添加操作
1.页面代码:
<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(); } }
注意要点:
1.在Page_Load事件中,要把控件绑定数据的方法放在ispostback方法里面,以避免在页面加载的时候首
都要加载原来的数据,修改的数据无法更新的情况。
2.使用DataPager控件给ListView控件分页时,需要编写ListView控件的lv_PagePropertiesChanging()
时间,以使在进行翻页操作时控件的数据能及时更新到相应页面。
ASP.NET使用ListView数据绑定控件和DataPager实现数据分页显示(二)
标签:style blog color io os 使用 ar strong for
原文地址:http://www.cnblogs.com/xyyt/p/3978903.html