如何在GridView中獲得隱藏欄位值

來源:互聯網
上載者:User

昨天在用GridView想實現如DataGrid 的功能,即在對GridView的資料來源邦定時,其中要實現有幾列隱藏,但在GridView的RowCommand函數中,和在DataGrid的處理一樣,通過e來過的隱藏列的值。

但在實踐中發現,在GridView的RowCommand函數中,他不能像DataGrid一樣通過e.item獲得那一列的值,更別說獲得獲得隱藏列的值。因為在GridView的RowCommand函數中沒有e.item這樣的功能,後來在網上尋找相關資訊才知道,你需要通過e.CommandArgument來獲得你需要的參數,再通過該參數來獲得相應的值。而CommandArgument是在GridView的RowCreated函數中設定的,如在GridView中有一個“詳情”的LinkButton,在RowCreated中,你需要設定該LinkButton的CommanArgurment,設定例子參看如下代碼:

 //在LinkButton的CommandArgument中設定其參數值,再次設定為當前GridView的RowIndex
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    ...{
        if (e.Row.RowType == DataControlRowType.DataRow)
        ...{
            LinkButton btn = (LinkButton)e.Row.Cells[e.Row.Cells.Count - 1].Controls[0];
            btn.CommandArgument = e.Row.RowIndex.ToString();//this.GridView1.Rows[e.Row.RowIndex].Cells[0].ToString();
        }
    }

然後在RowCommand函數中獲得CommandArgument的值,然後根據其獲得的值再獲得具體相應的值。
然而,若你的GridView中存在一些隱藏列,你就不能通過GridViewItem.cells[index]來獲得其值,我也不知道GridView中為什麼要這樣做,但通過如下方法你可以獲得其值:

在GridView的DataKeyName屬性中,將你需要隱藏的列的值設定其中,然後再在RowCommand中獲得其值,餐卡代碼如下:

 


//在GirdView中設定的參數
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  AllowPaging="True" 
            OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="20" OnRowCommand="GridView1_RowCommand" 
            OnRowCreated="GridView1_RowCreated"
             DataKeyNames="UserID,JobID,DptID">
                <Columns>
                    <asp:BoundField DataField="DptName" HeaderText="部門" />
                    <asp:BoundField DataField="Banzhu" HeaderText="班組" />
                    <asp:BoundField DataField="JobName" HeaderText="職位/崗位" />
                    <asp:BoundField DataField="TrueName" HeaderText="姓名" />
                    <asp:BoundField DataField="XiaoLingTong" HeaderText="小靈通" />
                    <asp:BoundField DataField="BanGongPhone" HeaderText="辦公電話" />
                    <asp:BoundField DataField="ExtensionPhone" HeaderText="分機" />
                    <asp:BoundField DataField="TelePhone" HeaderText="手機" />
                    <asp:BoundField DataField="HousePhone" HeaderText="住家電話" />
                    <asp:BoundField DataField="Remark" HeaderText="備忘" />
                    <asp:ButtonField CommandName="Selects" Text="詳情" />
                    <asp:ButtonField CommandName="Deletes" Text="刪除" />                   
                </Columns>
            </asp:GridView>


//在該函數中,獲得DataKeyName的屬性值,其中Index就是在Row_Created中設定的參數
 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  ...{
        if (e.CommandName == "Selects")
        ...{            
            int index = int.Parse(e.CommandArgument.ToString());
            GridViewItem item = this.GridView1.Rows[index];
            //要獲得其他置,通過item.Cells[cells_index].Text來獲得,再次不過多說明,

            //Datakey獲得在DataKeyName設定的值
            DataKey ky = this.GridView1.DataKeys[index];
            this.Label1.Text += "UserID:" + ky["UserID"]+"<br>";
            this.Label1.Text += "DptID:" + ky["DptID"] + "<br>";
            this.Label1.Text += "JobID:" + ky["JobID"] + "<br>";
        }
}

而且這樣的方法,不會出現分頁的效果失效。

這些只是我的一些處理辦法,大家若有什麼好的辦法,大家請多指教,謝謝了,^_^

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.