日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

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)擊一個Button(位于FormView內(nèi)部EdiTemplate)時,頁面會回發(fā),FormView的ItemCommand event被激發(fā).我們可以為這個事件創(chuàng)建一個event handler ,用來在Button 被點(diǎn)擊時執(zhí)行自定義代碼。注意:任何時候FormView里的任何Button, LinkButton, 或 ImageButton被點(diǎn)擊時,ItemCommand 事件都會被激發(fā).這意味著當(dāng)用戶在FormView里從一個頁面跳到另一個頁面時,ItemCommand 事件會被激發(fā).當(dāng)用戶點(diǎn)擊一個支持inserting, updating, 或 deleting的FormView里的New, Edit, 或 Delete 時,ItemCommand 事件會被激發(fā)。(要熟悉FormView的構(gòu)造)   既然無論點(diǎn)擊什么button時, ItemCommand 都會被激發(fā),那么在event handler里我們需要判斷是“Discontinue All Products” Button 被點(diǎn)擊了還是其它的button.為了達(dá)到這個目的,可以通過設(shè)置Button 的CommandName來識別. 當(dāng)Button 被點(diǎn)擊后,CommandName 的值被傳到ItemCommand 的event handler,我們通過這個值來判斷被點(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)擊時,頁面回發(fā),并激發(fā)FormView 和DetailsView 的ItemCommand 事件,GridView的RowCommand 事件.除了可以執(zhí)行本身內(nèi)置的功能外,還可以使用執(zhí)行自定義代碼的button.為了達(dá)到這個目的,需要為ItemCommand 或 RowCommand 創(chuàng)建一個event handler(事件處理程序). 在這個event handler 里我們首先檢查CommandName 的值來判斷哪個button被點(diǎn)擊了,然后執(zhí)行相應(yīng)的自定義代碼.

    注意:任何時候FormView里的任何Button,LinkButton, 或 ImageButton被點(diǎn)擊時,ItemCommand 事件都會被激發(fā).這意味著當(dāng)用戶在FormView里從一個頁面跳到另一個頁面時,ItemCommand 事件會被激發(fā).當(dāng)用戶點(diǎn)擊一個支持inserting, updating, 或 deleting的FormView里的New,Edit, 或 Delete 時,ItemCommand 事件會被激發(fā). 以下為自定義功能實現(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。此時e.CommandName為Button所設(shè)置的CommandName。DataKeys獲取一個 DataKey(數(shù)據(jù)綁定控件中某個記錄的一個或多個主鍵字段) 對象集合,這些對象表示 GridView 控件中的每一行的數(shù)據(jù)鍵值。類CommandEventArgs有兩個公共屬性,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集合.這個集合包含了GridView 的行的所有值.由于GridView在綁定到ObjectDataSource時,DataKeyNames 被Visual Studio設(shè)置為ProductID ,DataKeys(rowIndex).Value 提供了指定rowIndex的ProductID .ButtonField 會將被點(diǎn)擊的button所在row 的rowIndex 自動傳到e.CommandArgument 參數(shù)里.(e.CommandName為當(dāng)前點(diǎn)擊的Button的CommandName,此處Button為當(dāng)前行的一個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時GridView 都會重新綁定。 如果你不這么做,你需要手動再次綁定。

    注意:GridView (和DetailsView)同樣可以將Buttons,LinkButtons或ImageButtons 加到TemplateFields里.和BoundField一樣,這些Button被點(diǎn)擊時會產(chǎn)生回發(fā),觸發(fā)GridView的RowCommand 事件.當(dāng)添加button到TemplateField里時,button的CommandArgument不會像使用ButtonFields一樣,被自動設(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 屬性獲取一個 DataKey 對象集合,這些對象表示 GridView 控件中的每一行的數(shù)據(jù)鍵值。


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


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

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


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

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

 

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

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

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

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

轉(zhuǎn)換
    整型被隱式轉(zhuǎn)換為 decimal,其計算結(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)致編譯錯誤。



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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多