2011-01-26 68 views
0

我在page.aspx中有一個Gridview。這個gridview我想作爲參數傳遞給class1.cs.的構造函數。任何人都可以打電話給我,這怎麼辦?我如何訪問一個控件到一個類中?

+0

哪個GridView的?或者這不是asp.net mvc? – alexn 2011-01-26 10:38:29

回答

1

所以,你有一個網頁,一個GridView:

<asp:GridView runat="server" ID="gv1" AutoGenerateColumns="true"> 
</asp:GridView> 

<div> 
    This is where the count of rows of your GridView will be displayed: 
    <p> 
     <strong><asp:Label runat="server" ID="lCount" /></strong> 
    </p> 
</div> 

而且,在代碼隱藏你填充它是這樣的:

this.gv1.DataSource = FullName.GetDemoCollection(); //Just returns a List<string>; 
gv1.DataBind(); 

和你有另一個類GridViewRowCounter做一些事GridView,例如計算行:

public class GridViewRowCounter 
{ 
    private System.Web.UI.WebControls.GridView _gv; 

    public GridViewRowCounter(){} 

    public GridViewRowCounter(System.Web.UI.WebControls.GridView _GV){ 
     this._gv = _GV; 

    } 

    public int GetRowCount(){ 
     return _gv.Rows.Count; 
    } 

} 

所以,你的GridView傳遞給Gridviewcounter類,你可以這樣做:

public partial class PassingControls : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     //Bind the GridView 
     this.gv1.DataSource = FullName.GetDemoCollection();// 
     gv1.DataBind(); 

     //Pass Gridview reference to the GridVeiwRowCounter constructor. 
     GridViewRowCounter gvcounter = new GridViewRowCounter(this.gv1); 
     //Get the external class to return the rowcount from your GridView. 
     this.lCount.Text = gvcounter.GetRowCount().ToString(); 
    } 
} 

HTH。

希望這是你問;-)

+0

感謝花時間回答我,但是,對不起! ü誤解了我的問題。:-)其實我想傳遞的GridView作爲參數的一類事情是這樣的MyClass類 { 公共MYCLASS(GridView的GV) {// 一些代碼 } }上面的代碼需要在一個班上。 Gridview我想從.aspx頁面傳遞。我想一些命名空間必須添加到類中,以在構造函數中將gridview作爲數據類型。這個怎麼做? – Swathi 2011-01-28 04:47:30

相關問題