欧美日韩精品在线,国内精品久久久久久久久,一级毛片恃级毛片直播,清纯唯美亚洲综合欧美色

添加GridView/DataGrid單擊一行服務器事件_.Net教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:解析2個ASP.NET小技巧
1. ASP.NET AJAX 中,如何用 JavaScript 調用服務器端的方法? 這里不是指調用簡單的PageMethod,因為靜態(tài)方法是不能操作當前頁面的控件的,所以靜態(tài)的PageMethod作用就跟普通的WebService一樣,比較局限。 那么,調用一般的服務器端方法,其實就是發(fā)起一個

實現(xiàn)功能:
asp.net的GridView/DataGrid控件本身均支持行選擇事件(通過設置Button/LinkButton.CommandName="Selected",并在 SelectedIndexChanged 事件中處理)。
然而,有時候我們希望用戶點擊網(wǎng)頁上GridView/DataGrid 一行中任意位置都可以實現(xiàn)觸發(fā)一個事件,并在服務端對此行進行相應處理,現(xiàn)在我們就實現(xiàn)此功能。
實現(xiàn)方式:
這里我們采取的方法有點 "hack" : 
通過客戶端 javascript 引發(fā)行中隱藏的按鈕(Button/LinkButton 均可以)的 click 事件。
主要代碼:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
            <Columns>                              
                <asp:TemplateField HeaderText="ProductName" >
                    <ItemTemplate>
                        <%# Eval("ProductName") %>
                        <asp:Button ID="btnHiddenPostButton" CommandName="HiddenPostButtonCommand" runat="server" Text="HiddenPostButton" style="display:none" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
            </Columns>
        </asp:GridView>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Button btnHiddenPostButton = e.Row.FindControl("btnHiddenPostButton") as Button;
        if (btnHiddenPostButton != null) {
            e.Row.Attributes["onclick"] = String.Format("javascript:document.getElementById(’{0}’).click()", btnHiddenPostButton.ClientID);
            // 額外樣式定義
            e.Row.Attributes["onmouseover"] = "javascript:this.style.background=’red’";
            e.Row.Attributes["onmouseout"] = "javascript:this.style.background=’’";
            e.Row.Attributes["style"] = "cursor:pointer";
            e.Row.Attributes["title"] = "單擊選擇當前行";
        }
        // 若希望將隱藏按鈕單獨放于一列,則設置此列隱藏,占位符 <cellIndex> 表示此列索引
        //e.Row.Cells[<cellIndex>].Attributes["style"] = "display:none";
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int rowIndex = -1;
        GridViewRow row = null;
        switch (e.CommandName) {            
            case "HiddenPostButtonCommand": // 模板列                
                Control cmdControl = e.CommandSource as Control; // 表示觸發(fā)事件的 IButtonControl,保持統(tǒng)一性并便于后續(xù)操作,我們這里直接轉化為控件基類 Control
                row = cmdControl.NamingContainer as GridViewRow; // 當前行
                // 如何訪問單元格值
                // string txt = row.Cells[0].Text;
                // 如何獲取模板列中的 Label
                // string lbl = row.FindControl("MyLabelID") as Label;
                // 執(zhí)行更多的自定義操作
                // ....
                //  .....
                Response.Write(String.Format("GridView Version 當前第 {0} 行:", row.RowIndex + 1));
                break;
            // case "Command2":
            // more cases
            //  .....
        }
    }
 




分享:解析asp.net編程中6條實用語句
1.Panel橫向滾動,縱向自動擴展 <asp:panelstyle=quot;overflow-x:scroll;overflow-y:auto;quot;></asp:panel> 2.回車轉換成Tab (1) <scriptlanguage=quot;javascriptquot;for=quot;documentquot;event=quot;onkeydownquot;> if(event.keyCode==13amp;

來源:模板無憂//所屬分類:.Net教程/更新時間:2010-05-29
相關.Net教程