GridView自定義命令列的實現

在GridView中一般不支持命令列 只支持系統自帶的Update Delete Edit Cancel命令列其實要自定義命令列也非常簡單的前臺

<asp:TemplateField>

       <ItemTemplate>

         <asp:LinkButton runat="server" ID="lbJoin" CommandName="joinSchool"    Text="入"  CommandArgument='<%#DataBinder.Eval(Container,"RowIndex") %>' OnCommand="lbJoin_Command" CausesValidation="false"></asp:LinkButton>

       </ItemTemplate>

</asp:TemplateField> 

前臺把需要的參數寫到CommandArgument中 在後臺去獲取

後臺:

protected void lbJoin_Command(object sender,CommandEventArgs e)

    {

        int rowIndex = Convert.ToInt32(e.CommandArgument);

        int sch_id = Convert.ToInt32(SchoolView.DataKeys[rowIndex].Value);

        if (Users.SetSchoolId(Convert.ToInt32(Session["UserID"]), sch_id))

        {

            Session["S_ID"] = sch_id;

            Response.Redirect("FindClass.aspx");

        }

        else

        {

            insertHint.InnerHtml="<font color='red'>加入學校失敗.請重試..</font>";

        }

    }

後臺通過e.CimmandArgument獲取參數 在此處爲命令列所在的RowIndex值 根據此值獲取主鍵參數 進行數據庫操作其實比較簡單 只要設置好了參數就OK了.....

發表於 2007-11-27 12:41 浪客流星劍
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章