現在我已經在MSDN上做了一些關於如何從 頁面獲取控件的研究。 http://msdn.microsoft.com/en-us/library/yt340bh4(v=vs.100).aspxC#如何從靜態方法中找到頁面的Gridview
我已經完成了關於如何從靜態上下文獲取頁面引用的研究。 Access current instance of Page from a static class
但是,當我嘗試循環瀏覽頁面控件列表時,它出現null和gridview。
現在我需要該gridview,因爲我將它數據綁定到一個Datatable,我之前已經用數據庫調用進行填充。靜態方法,我做這裏面是一個WebMethod,所以它看起來是這樣的:
[WebMethod(EnableSession = true)]
[ScriptMethod]
public static string viewApps(){}
這裏是我的靜態方法
GridView applications = new GridView();
Page masspage = HttpContext.Current.CurrentHandler as Page;
foreach(Control childcontrol in masspage.Controls)
{
if(childcontrol is GridView){
applications = childcontrol as GridView;
}
}
applications.DataSource = dt;
applications.DataBind();
我想這將是簡單的我的C#代碼,但我猜不是太多。 如果有一種方法可以基於id獲得對我的GridView的引用,這也是有用的。
這是我的asp.net代碼。它是從母版頁繼承的頁面。
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Always">
<ContentTemplate>
<asp:GridView ID="ApplicationsGridView" runat="server"
AutoGenerateColumns="True" >
<%--Styling and column info omitted --%>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
你不能從webmethod得到它,只能在頁面的實際生命週期。請注意,在將頁面發送到客戶端後,所有對象都放置在服務器上。 –
那麼,我需要將我的aspx代碼文件中的Gridview數據綁定到數據表中,然後如何從靜態上下文中執行此操作。 –
如果你使用'UpdatePanel',爲什麼不異步回發? –