GridView模板列中使用AutoPostBack的CheckBox/DropDownList

來源:互聯網
上載者:User

有時候希望在 GridView 模板中使用AutoPostBack的 CheckBox (autopostback=true) ,但是 CheckBox 沒有 CommandName 屬性,因此也就無法在 GridView.RowCommand  事件中處理,並且如何擷取當前 GridView 行資訊呢?

我們可以選擇像處理頁面上普通 CheckBox 的 CheckedChanged 事件的“最原始”的方式。

要點:
1. 注意到 .net 中控制項事件委託統一原型 (object sender,  EventArgs e)
第一個參數 sender,表示觸發此事件控制項

2. ASP.NET  伺服器控制項基類 Control 中定義了屬性 NamingContainer ,表示對伺服器控制項的命名容器的引用,此引用建立唯一的命名空間。在模板列中的控制項就是表示包含此控制項的模板列。
註:也可以使用 BindingContainer 屬性擷取,但 MSDN 不建議使用此屬性,
         此屬性主要用於控制項開發以及 ASP.NET 引擎內部使用

       
下面以 DataGrid/GridView 模板列中使用 CheckBox/DropDownList 為例:

 .aspx
<asp:TemplateField>
                    <ItemTemplate>                       
                    <asp:CheckBox ID="chkItem" runat="server" OnCheckedChanged="chkItem_CheckedChanged" AutoPostBack="true" />
                    <asp:DropDownList ID="drpItem" runat="server" OnSelectedIndexChanged="drpItem_SelectedIndexChanged" AutoPostBack="true">
                        <asp:ListItem></asp:ListItem>
                        <asp:ListItem Value="red">red</asp:ListItem>
                        <asp:ListItem Value="green">green</asp:ListItem>
                        <asp:ListItem Value="orange">orange</asp:ListItem>
                    </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
 .aspx.cs
protected void chkItem_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = sender as CheckBox; // 觸發事件的 CheckBox       
        GridViewRow row = chk.NamingContainer as GridViewRow; // GridView 當前行   
        // 也可以使用 BindingContainer 屬性擷取,但 MSDN 不建議使用此屬性,
        // 此屬性主要用於控制項開發以及 ASP.NET 引擎內部使用
        // GridViewRow row = chk.BindingContainer as GridViewRow;
        row.BackColor = chk.Checked ? System.Drawing.Color.BlueViolet : System.Drawing.Color.White;       
        Response.Write(String.Format("選中第 {0} 行", row.RowIndex + 1));
        // more codes
        // Label lbl = row.FindControl("MyLabelID") as Label;
        // string str = row.Cells[0].Text;
        //
    }

    protected void drpItem_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList  drp = sender as DropDownList; // 觸發事件的 DropDownList
        GridViewRow row = drp.NamingContainer as GridViewRow; // GridView 當前行       
        row.Style.Add(HtmlTextWriterStyle.BackgroundColor, drp.SelectedValue);
        Response.Write(String.Format("選中第 {0} 行", row.RowIndex + 1));
        // more codes
        // Label lbl = row.FindControl("MyLabelID") as Label;
        // string str = row.Cells[0].Text;
        //
    }
protected void chkItem2_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = sender as CheckBox; // 觸發事件的 CheckBox       
        DataGridItem row = chk.NamingContainer as DataGridItem;       
        row.BackColor = chk.Checked ? System.Drawing.Color.BlueViolet : System.Drawing.Color.White;       
        Response.Write(String.Format("選中第 {0} 行", row.ItemIndex + 1));
        // more codes
        // Label lbl = row.FindControl("MyLabelID") as Label;
        // string str = row.Cells[0].Text;
        //
    }

    protected void drpItem2_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList drp = sender as DropDownList; // 觸發事件的 DropDownList
        DataGridItem row = drp.NamingContainer as DataGridItem;
        row.Style.Add(HtmlTextWriterStyle.BackgroundColor, drp.SelectedValue);
        Response.Write(String.Format("選中第 {0} 行", row.ItemIndex + 1));
        // more codes
        // Label lbl = row.FindControl("MyLabelID") as Label;
        // string str = row.Cells[0].Text;
        //
    }
說明
1。事實上,對於 Button LinkButton ImageButton 當然也可以採取此中方式,只是我們習慣了 ItemCommand 事件中處理
2。對於 DataList/Repeater  只要轉換外對應的模板項類型,DataListItem/RepeaterItem

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.