• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NET使用ListView数据绑定控件和DataPager实现数据分页显示(二) ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

使用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()
时间,以使在进行翻页操作时控件的数据能及时更新到相应页面。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap