ASP.NET使用ListView資料繫結控制項和DataPager實現資料分頁顯示(二)

來源:互聯網
上載者:User

標籤: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] [email protected] WHERE [email protected]");        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實現資料分頁顯示(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.