《ASP.NET1200例》ListView控件之修改,删除与添加

aspx

 

<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListView ID="ListView1" runat="server"  
            OnItemEditing="ListView1_ItemEditing" onitemupdating="ListView1_ItemUpdating" 
            onitemcanceling="ListView1_ItemCanceling"
            onpagepropertieschanging="ListView1_PagePropertiesChanging" 
              DataKeyNames="id" onitemdeleting="ListView1_ItemDeleting">
         <ItemTemplate>
             <asp:ImageButton id="ProductImage" ImageUrl='<%#DataBinder.Eval(Container.DataItem, "imageUrl")%>' Height="150px" Width ="200px" runat="server"/>
             <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">Edit</asp:LinkButton>
             <asp:LinkButton ID="LinkButton4" runat="server" CommandName="Delete">Delete</asp:LinkButton>
             <br />
         </ItemTemplate>
         <EditItemTemplate>
             <asp:ImageButton id="ProductImage" ImageUrl='<%#DataBinder.Eval(Container.DataItem, "imageUrl")%>' Height="150px" Width ="200px" runat="server"/>
               图片名称: <asp:TextBox ID="txtimageName" runat="server" Text='<%#Eval("imageName")%>'></asp:TextBox>
              图片路径: <asp:TextBox ID="txtimageUrl" runat="server" Text='<%#Eval("imageUrl")%>'></asp:TextBox>
             <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update">Update</asp:LinkButton>
             <asp:LinkButton ID="LinkButton3" runat="server" CommandName="Cancle">Cancle</asp:LinkButton>
             <br />
         </EditItemTemplate>

        </asp:ListView>
        <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="2">
            <Fields>
              <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                        ShowLastPageButton="True" />
            </Fields>
        </asp:DataPager>
    </div>
    </form>
</body>

 

 

aspx.cs

 public partial class _240ListViewEdit : System.Web.UI.Page
    {
        ShowImageBll ShowImageBll = new BLL.ShowImageBll();
       protected void Page_Load(object sender, EventArgs e)
      {
            if (!IsPostBack)
            {
                bindDL();

            }
        }
        public void bindDL()
        {      
            //绑定数据源
            DataSet ds = ShowImageBll.GetList();
            ListView1.DataSource = ds;
            ListView1.DataBind();

        }

        protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            this.DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);

            bindDL();
        }

        protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            ListView1.EditIndex = e.NewEditIndex;
            bindDL();
        }

        protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            int id =int.Parse(ListView1.DataKeys[e.ItemIndex].Value.ToString()); 
            TextBox txtimageName = ListView1.Items[e.ItemIndex].FindControl("txtimageName") as TextBox;
            TextBox txtimageUrl = ListView1.Items[e.ItemIndex].FindControl("txtimageUrl") as TextBox;
            string imageName = txtimageName.Text;// Read in the imageUr
            string imageUrl = txtimageUrl.Text;// Read in the imageUr
            ShowImageBll.UpdateList(id, imageUrl, imageName); // Call the ShowImageBll's  UpadteDataLis method...
            Response.Write("<script>alert('更新成功!')</script>");
            bindDL();
        }

        protected void ListView1_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            ListView1.EditIndex = -1;

            bindDL();

        }

        protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            int id = int.Parse(ListView1.DataKeys[e.ItemIndex].Value.ToString());
            ShowImageBll.DeleteSingleList(id);
            Response.Write("<script>alert('删除成功!')</script>");
            bindDL(); ;//重新绑定数据库
        }

 

总结

【1】在Page_Load事件中,要把控件绑定数据的方法放在ispostback方法里面,以避免在页面加载的时候首

都要加载原来的数据,修改的数据无法更新的情况。

【2】使用DataPager控件给ListView控件分页时,需要编写ListView控件的lv_PagePropertiesChanging()

时间,以使在进行翻页操作时控件的数据能及时更新到相应页面。

【3】遗留的问题:Cancle按钮不起作用

posted @ 2013-12-03 10:56 Miracle_lucy 阅读(...) 评论(...) 编辑 收藏