乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      GridView里的Button控件用法

       昵稱10525020 2012-10-12
      GridView里的Button控件用法

      網(wǎng)格查看里的按鈕控件用法

      http://www.cnblogs.com/strivers/archive/2011/01/26/1945287.html

       

      當(dāng)用戶點(diǎn)擊一個(gè)Button(位于FormView內(nèi)部EdiTemplate)時(shí),頁面會(huì)回發(fā),FormView的ItemCommand event被激發(fā).我們可以為這個(gè)事件創(chuàng)建一個(gè)event handler ,用來在Button 被點(diǎn)擊時(shí)執(zhí)行自定義代碼。注意:任何時(shí)候FormView里的任何Button, LinkButton, 或 ImageButton被點(diǎn)擊時(shí),ItemCommand 事件都會(huì)被激發(fā).這意味著當(dāng)用戶在FormView里從一個(gè)頁面跳到另一個(gè)頁面時(shí),ItemCommand 事件會(huì)被激發(fā).當(dāng)用戶點(diǎn)擊一個(gè)支持inserting, updating, 或 deleting的FormView里的New, Edit, 或 Delete 時(shí),ItemCommand 事件會(huì)被激發(fā)。(要熟悉FormView的構(gòu)造)   既然無論點(diǎn)擊什么button時(shí), ItemCommand 都會(huì)被激發(fā),那么在event handler里我們需要判斷是“Discontinue All Products” Button 被點(diǎn)擊了還是其它的button.為了達(dá)到這個(gè)目的,可以通過設(shè)置Button 的CommandName來識(shí)別. 當(dāng)Button 被點(diǎn)擊后,CommandName 的值被傳到ItemCommand 的event handler,我們通過這個(gè)值來判斷被點(diǎn)擊的button是否是“Discontinue All Products” Button。設(shè)置“Discontinue All Products” Button的CommandName為“DiscontinueProducts”。

      C# 
      protected void Suppliers_ItemCommand(object sender, FormViewCommandEventArgs e)
      {
      if (e.CommandName.CompareTo("DiscontinueProducts") == 0)
           {
                  // The "Discontinue All Products" Button was clicked.
                  // Invoke the ProductsBLL.DiscontinueAllProductsForSupplier(supplierID) method
                  // First, get the SupplierID selected in the FormView
             int supplierID = (int)Suppliers.SelectedValue;
                  // Next, create an instance of the ProductsBLL class
             ProductsBLL productInfo = new ProductsBLL();   //此處為何不使用Adapter?
                  // Finally, invoke the DiscontinueAllProductsForSupplier(supplierID) method
             productInfo.DiscontinueAllProductsForSupplier(supplierID);
            }
      }

       

          GridView, DetailsView, 和FormView都可以包含Buttons, LinkButtons, 或ImageButtons.這些button被點(diǎn)擊時(shí),頁面回發(fā),并激發(fā)FormView 和DetailsView 的ItemCommand 事件,GridView的RowCommand 事件.除了可以執(zhí)行本身內(nèi)置的功能外,還可以使用執(zhí)行自定義代碼的button.為了達(dá)到這個(gè)目的,需要為ItemCommand 或 RowCommand 創(chuàng)建一個(gè)event handler(事件處理程序). 在這個(gè)event handler 里我們首先檢查CommandName 的值來判斷哪個(gè)button被點(diǎn)擊了,然后執(zhí)行相應(yīng)的自定義代碼.

          注意:任何時(shí)候FormView里的任何Button,LinkButton, 或 ImageButton被點(diǎn)擊時(shí),ItemCommand 事件都會(huì)被激發(fā).這意味著當(dāng)用戶在FormView里從一個(gè)頁面跳到另一個(gè)頁面時(shí),ItemCommand 事件會(huì)被激發(fā).當(dāng)用戶點(diǎn)擊一個(gè)支持inserting, updating, 或 deleting的FormView里的New,Edit, 或 Delete 時(shí),ItemCommand 事件會(huì)被激發(fā). 以下為自定義功能實(shí)現(xiàn)的代碼段:

      C#
      protected void SuppliersProducts_RowCommand(object sender, GridViewCommandEventArgs e)
      {
      if (e.CommandName.CompareTo("IncreasePrice") == 0 ||e.CommandName.CompareTo("DecreasePrice") == 0)
           {
                    // The Increase Price or Decrease Price Button has been clicked
                    // Determine the ID of the product whose price was adjusted
             int productID =(int)SuppliersProducts.DataKeys[Convert.ToInt32(e.CommandArgument)].Value;

      /*e.CommandArgument是用來獲取button所在row的rowIndex。此時(shí)e.CommandName為Button所設(shè)置的CommandName。DataKeys獲取一個(gè) DataKey(數(shù)據(jù)綁定控件中某個(gè)記錄的一個(gè)或多個(gè)主鍵字段) 對(duì)象集合,這些對(duì)象表示 GridView 控件中的每一行的數(shù)據(jù)鍵值。類CommandEventArgs有兩個(gè)公共屬性,CommandName(用來獲取命令的名稱)和CommandArgument(用來獲取命令的參數(shù))*/
                    // Determine how much to adjust the price decimal percentageAdjust;
              if (e.CommandName.CompareTo("IncreasePrice") == 0)
                  percentageAdjust = 1.1M;

      /*從Double類型到Decimal的顯示轉(zhuǎn)換(整型可以隱式轉(zhuǎn)換為Decimal類型,不必在后面加M,詳情見下方的MSDN查閱)*/
              else
                  percentageAdjust = 0.9M;
                    // Adjust the price
                  ProductsBLL productInfo = new ProductsBLL();
                  productInfo.UpdateProduct(percentageAdjust, productID);
            }
      }
           為了判斷被點(diǎn)擊“Price +10%” or “Price -10%” button 的那一行的ProductID ,我們需要用到GridView的DataKeys集合.這個(gè)集合包含了GridView 的行的所有值.由于GridView在綁定到ObjectDataSource時(shí),DataKeyNames 被Visual Studio設(shè)置為ProductID ,DataKeys(rowIndex).Value 提供了指定rowIndex的ProductID .ButtonField 會(huì)將被點(diǎn)擊的button所在row 的rowIndex 自動(dòng)傳到e.CommandArgument 參數(shù)里.(e.CommandName為當(dāng)前點(diǎn)擊的Button的CommandName,此處Button為當(dāng)前行的一個(gè)ButtonField)因此,用Convert.ToInt32(SuppliersProducts.DataKeys(Convert.ToInt32(e.CommandArgument)).Value)來獲取被點(diǎn)擊“Price +10%” or “Price -10%” button的行的ProductID . 和“Discontinue All Products” button里一樣,如果你禁用了GridView的view state屬性,每次postback時(shí)GridView 都會(huì)重新綁定。 如果你不這么做,你需要手動(dòng)再次綁定。

          注意:GridView (和DetailsView)同樣可以將Buttons,LinkButtons或ImageButtons 加到TemplateFields里.和BoundField一樣,這些Button被點(diǎn)擊時(shí)會(huì)產(chǎn)生回發(fā),觸發(fā)GridView的RowCommand 事件.當(dāng)添加button到TemplateField里時(shí),button的CommandArgument不會(huì)像使用ButtonFields一樣,被自動(dòng)設(shè)置為row 的index .如果你需要在RowCommand的event handler里判斷點(diǎn)擊的button所在行的index ,你需要在TemplateField的頁面代碼里使用以下代碼來設(shè)置button的CommandArgument 屬性:
      <asp:Button runat="server" ... CommandArgument='<%# CType(Container, GridViewRow).RowIndex %>' /> 


      以下為查閱MSDN中的相關(guān)內(nèi)容:
          GridView.DataKeys 屬性獲取一個(gè) DataKey 對(duì)象集合,這些對(duì)象表示 GridView 控件中的每一行的數(shù)據(jù)鍵值。


      屬性值
          一個(gè) DataKeyArray,其中包含 GridView 控件中每一行的數(shù)據(jù)鍵。


      備注
          當(dāng)設(shè)置了 DataKeyNames 屬性時(shí),GridView 控件自動(dòng)為該控件中的每一行創(chuàng)建一個(gè) DataKey 對(duì)象。DataKey 對(duì)象包含在DataKeyNames 屬性中的指定的字段的值。DataKey 對(duì)象隨后被添加到控件的 DataKeys 集合中。使用 DataKeys 屬性檢索 GridView 控件中特定數(shù)據(jù)行的 DataKey 對(duì)象。

      注意 
          可使用 SelectedDataKey 屬性檢索當(dāng)前選中行的 DataKey 對(duì)象。還可以使用 SelectedValue 屬性直接檢索當(dāng)前選中行的數(shù)據(jù)鍵值。DataKeyArray 類表示DataKey 對(duì)象的集合。無法繼承此類。


      備注
          DataKeyArray 類用于存儲(chǔ)和管理 DataKey 對(duì)象的集合。DataKey 對(duì)象表示數(shù)據(jù)綁定控件中某個(gè)記錄的主鍵。通常情況下,顯示多個(gè)記錄的數(shù)據(jù)綁定控件(如 GridView 控件)使用 DataKeyArray 對(duì)象來存儲(chǔ)該控件中顯示的記錄的DataKey 對(duì)象。

          DataKeyArray 類支持多種訪問集合中的項(xiàng)的方法:使用 Item 索引器直接從集合中從零開始的特定索引位置檢索 DataKey 對(duì)象。使用 GetEnumerator 方法檢索可用于循環(huán)訪問集合的枚舉數(shù)。使用 CopyTo 方法將集合中的項(xiàng)復(fù)制到數(shù)組,然后可使用該數(shù)組訪問集合中的項(xiàng)。若要確定集合中的總項(xiàng)數(shù),請(qǐng)使用 Count 屬性。

       

          DataKey 類 表示數(shù)據(jù)綁定控件中某個(gè)記錄的一個(gè)或多個(gè)主鍵字段。 DataKey 類用于表示數(shù)據(jù)綁定控件中某個(gè)記錄的主鍵。記錄的主鍵可以由數(shù)據(jù)源中的一個(gè)或多個(gè)字段組成。盡管 DataKey 類不是集合,但它可以存儲(chǔ)多個(gè)鍵字段值。當(dāng)調(diào)用 DataKey 類的某個(gè)構(gòu)造函數(shù)時(shí),將填充鍵字段值??梢酝ㄟ^以下方法從 DataKey 對(duì)象中檢索鍵字段值:使用 DataKey.Item(Int32) 屬性檢索 DataKey 對(duì)象中特定索引位置的鍵字段值。使用 DataKey.Item(String) 屬性檢索特定字段的鍵字段值。使用 Value 屬性檢索 DataKey 對(duì)象中索引 0 位置的鍵字段值。當(dāng)主鍵只包含一個(gè)字段時(shí),此屬性常用作檢索記錄鍵值的快捷方式。使用 Values 屬性創(chuàng)建可用于循環(huán)訪問鍵字段值的 IOrderedDictionary 對(duì)象。通常,當(dāng)設(shè)置了數(shù)據(jù)綁定控件的 DataKeyNames 屬性時(shí),控件自動(dòng)生成 DataKey 對(duì)象。DataKey 對(duì)象包含DataKeyNames 屬性中指定的一個(gè)或多個(gè)鍵字段的值。一次顯示一個(gè)記錄的數(shù)據(jù)綁定控件(如 DetailsView 或 FormView)通常在它的 DataKey 屬性中存儲(chǔ)所顯示的當(dāng)前記錄的 DataKey 對(duì)象。一次顯示多個(gè)記錄的數(shù)據(jù)綁定控件(如GridView)通常在 DataKeyArray 集合中存儲(chǔ)它的每個(gè)記錄的 DataKey 對(duì)象。然后,DataKeyArray 集合將存儲(chǔ)在控件的 DataKeys 屬性中。

      關(guān)于decimal類型(C# 參考) 
          decimal關(guān)鍵字表示 128 位數(shù)據(jù)類型。同浮點(diǎn)型相比,decimal類型具有更高的精度和更小的范圍,這使它適合于財(cái)務(wù)和貨幣計(jì)算。decimal 類型的大致范圍和精度如下表所示。

      類型             大致范圍                                               精度               .NET Framework 類型 
      decimal      ±1.0 × 10e?28至±7.9 × 10e28   28到29位有效位        System.Decimal

      標(biāo)識(shí)符
          如果希望實(shí)數(shù)被視為 decimal 類型,請(qǐng)使用后綴 m 或 M,例如:decimal myMoney = 300.5m;如果沒有后綴 m,數(shù)字將被視為 double 類型,從而導(dǎo)致編譯器錯(cuò)誤。

      轉(zhuǎn)換
          整型被隱式轉(zhuǎn)換為 decimal,其計(jì)算結(jié)果為 decimal。因此,可以用整數(shù)初始化十進(jìn)制變量而不使用后綴,如下所示:
                decimal myMoney = 300;

          在浮點(diǎn)型和 decimal 類型之間不存在隱式轉(zhuǎn)換;因此,必須使用強(qiáng)制轉(zhuǎn)換在這兩種類型之間進(jìn)行轉(zhuǎn)換。例如:decimal myMoney = 99.9m; double x = (double)myMoney; myMoney = (decimal)x;
      還可以在同一表達(dá)式中混合使用 decimal 和數(shù)值整型。但是,不進(jìn)行強(qiáng)制轉(zhuǎn)換就混合使用 decimal 和浮點(diǎn)型將導(dǎo)致編譯錯(cuò)誤。



        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多