0

This one's really stumping me. And it looks simple. I have a telerik: RadGrid on an aspx page and I'm using a web service to get a table and bind it. I bind it once on page_load and when a node from a tree is clicked i want to bind it again displaying new info. However, when trying to bind from this function the grid comes up null. For right, now I'm using the same service call to bind.

Here's my server code:

  protected void Page_Load(object sender, EventArgs e)
    {



            using (ServiceReference1.Service1SoapClient myService = new ServiceReference1.Service1SoapClient())
            {


                CurrentDemog = Convert.ToInt32(Session["demog"].ToString());

                DataTable lookupTbl = myService.getTable();
                LookupGrid.DataSource = lookupTbl;
                LookupGrid.DataBind();
            }


    }



   protected void LookupsTree_NodeClick(object sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
    {

        RadTreeNode currNode = e.Node;
        if (currNode.Nodes.Count == 0) {
            if (currNode.ParentNode.Text != "Treatments" && !String.IsNullOrEmpty(currNode.Text))
            {
                selectedNode = currNode.Text;

                 using (ServiceReference1.Service1SoapClient myService = new ServiceReference1.Service1SoapClient())
                 {
                     Util.ClearGrid(ref LookupGrid);
                     DataTable lookupTbl = myService.getTable();

                     LookupGrid.DataSource = lookupTbl;
                     LookupGrid.DataBind();
                 }


            }

        }

    }``

Here's my client code:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

 <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="LookupGrid">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="LookupGrid"></telerik:AjaxUpdatedControl>
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>

     <telerik:RadGrid ID="LookupGrid" runat="server" BackColor="Gray" 
         BorderColor="#404040" BorderStyle="Solid" CellSpacing="0" GridLines="None" 
         Skin="MetroTouch">

     </telerik:RadGrid>

4

1 回答 1

1

您需要在 Page_Load 方法中有一个 Page.IsPostback 。否则,每次您执行任何导致页面重新加载的操作时都会加载数据。

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            using (ServiceReference1.Service1SoapClient myService = new ServiceReference1.Service1SoapClient())
            {
                CurrentDemog = Convert.ToInt32(Session["demog"].ToString());

                DataTable lookupTbl = myService.getTable();
                LookupGrid.DataSource = lookupTbl;
                LookupGrid.DataBind();
            }
        }
    }

这里发生的是您加载网格视图并单击按钮。当您这样做时,在事件处理程序之前调用 Page_Load 方法。这很可能会导致事件处理程序中的 if 语句为 false,从而导致无法加载数据。

这是有关此主题的好读物:ASP.NET 页面生命周期概述

于 2013-09-06T12:21:24.857 回答