星期日, 7月 04, 2010

[Asp.net] ListView ItemDataBound下抓取樣版的欄位值

剛好需要在ItemTemplate 按下編輯按鈕時,設定下拉式選單的值為ItemTemplate Lable繫結的值。
只要在ItemDataBound判斷是否為edit mode.做以下記錄


protected void lvAccounts_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        //Verify there is an item being edited.
        if (this.lvAccounts.EditIndex >= 0)
        {
            //Get the item object.
           ListViewDataItem dataItem = (ListViewDataItem)e.Item;

           // Check for an item in edit mode.
           if (dataItem.DisplayIndex == this.lvAccounts.EditIndex)
           {

               Control hiddenCtl = e.Item.FindControl("hiddenSex");
               if (hiddenCtl != null)
               {
                   Control ddlCtl = e.Item.FindControl("ddlSex");
                   string sexName = ((System.Web.UI.HtmlControls.HtmlInputHidden)hiddenCtl).Value;
                   //根據hidden value值指定下拉式選單的selectedindex
                   if (sexName == "男")
                   {
                       if (ddlCtl != null)
                       {
                           ((DropDownList)ddlCtl).SelectedIndex = 0;
                       }
                   }
                   else
                   {
                       if (ddlCtl != null)
                       {
                           ((DropDownList)ddlCtl).SelectedIndex = 0;
                       }
                   }
               }
           }
        }

    }

沒有留言:

張貼留言

留個話吧:)