星期日, 7月 04, 2010

[Asp.Net] 資料繫結控制項的相關事件關係與抓取

使用資料擊結控制項時常在驅動更新、刪除事件時常常需要擷取元件的值來做相關的處理(常常很用到的問題!!),以前都習慣在print出元件的位置(註解部份),再找出相對的值,不過其實用FindControl這個方法就可以簡單達到我們的需求了

protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        //this.Response.Write(((Button)sender).CommandArgument);
        //ctl00_ContentPlaceHolder1_ListView1_ctrl0_ctl00
        //ctl00_ContentPlaceHolder1_ListView1_ctrl0_imgUpload
        //ctl00_ContentPlaceHolder1_ListView1_ctrl0_ctl01
        //ctl00_ContentPlaceHolder1_ListView1_ctrl0_imgEdit
        //ctl00_ContentPlaceHolder1_ListView1_ctrl0_ctl02
        //ctl00_ContentPlaceHolder1_ListView1_ctrl0_imgDelete
        //ctl00_ContentPlaceHolder1_ListView1_ctrl0_ctl03
        //ctl00_ContentPlaceHolder1_ListView1_ctrl0_lblAlbumDesc
        //ctl00_ContentPlaceHolder1_ListView1_ctrl0_ctl04
        //int allControl = this.ListView1.Items[e.ItemIndex].Controls.Count;
        //for (int i = 0; i < allControl; i++)
        //{
        //    this.Response.Write(this.ListView1.Items[e.ItemIndex].Controls[i].ClientID + "
");

        //}

        //有抓到pk index,listview設DataKeyNames="ActivityAlbumID"
        //this.SqlDataSourceActivityAlbumList.UpdateParameters["ActivityAlbumID"].DefaultValue = ((ImageButton)this.ListView1.Items[e.ItemIndex].Controls[1]).CommandArgument;

          //
          //
        //this.SqlDataSourceActivityAlbumList.UpdateParameters["ActivityAlbumTitle"].DefaultValue = ((TextBox)this.ListView1.Items[e.ItemIndex].Controls[5]).Text;
        //this.SqlDataSourceActivityAlbumList.UpdateParameters["ActivityAlbumDesc"].DefaultValue = ((TextBox)this.ListView1.Items[e.ItemIndex].Controls[7]).Text;

        //擷取變更的內容,透過e.ItemIndex抓到目前編輯的項目索引值,再透過FindControl找到元件的值
        this.SqlDataSourceActivityAlbumList.UpdateParameters["ActivityAlbumTitle"].DefaultValue = ((TextBox)this.ListView1.Items[e.ItemIndex].FindControl("tbAlbumTitle")).Text;
        this.SqlDataSourceActivityAlbumList.UpdateParameters["ActivityAlbumDesc"].DefaultValue = ((TextBox)this.ListView1.Items[e.ItemIndex].FindControl("tbAlbumDesc")).Text;
    }

這裡還有一篇很祥細的文章,剛接觸.net的人很適合閱讀:

ASP.NET 資料繫結控制項裡的子控制項如何準確抓取【下】(完)

PS:資料繫結控制項除了提供有相關更新(ItemUpdateing)、刪除(ItemDeleting)等等事件供我們使用。另有ItemCommand事件來達到更新、刪除等等事件效果,二者則一都可。如果有自訂CommandName的話,使用這個事件應該比較方便!!

  protected void lvAccounts_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
ListViewDataItem currentItem;

        //如果是LayoutTemplate觸發自定的CommandName按鈕,就無法取得e.Item
        if(e.Item.ItemType == ListViewItemType.DataItem)
            currentItem = (ListViewDataItem)e.Item;
        switch (e.CommandName)
        {
            case ("Select"):
                this.ListView1.SelectedIndex = currentItem.DataItemIndex;
                break;
            case ("Edit"):
                this.ListView1.EditIndex = currentItem.DataItemIndex;
                break;
            case ("Cancel"):
                this.ListView1.EditIndex = -1;
                break;
            case ("Update"):
                ...........略略
                break;

            case ("MyButtonPost"):
               ...........略略
            break;
        }
    }

 

沒有留言:

張貼留言

留個話吧:)