This is a migrated thread and some comments may be shown as answers.

Issue with hiding grid

1 Answer 44 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Soumya
Top achievements
Rank 1
Soumya asked on 23 Aug 2012, 09:04 AM
I have a scenario as below.
There are 3 check boxes.3 textbox associated with each.Grid gets populated on entering a search value in the textbox.On selecting a row on this grid,I need to hide the grid and selected value(one column) is populated into another textbox.I am getting "Object reference not set to an instance of an object.",when I implementing the logic for hiding the grid.Checkbox checked,value entered in search textbox,grid is populated ,a row selected,second textbox populated with grid value,grid gets hidden--> after this process repeated for 2-3 times,again checking on a check box gives the error.
Please find the markup with above controls.

<table width="500px">
           <tr>
               <td>
                   <fieldset id="fssearch" runat="server">
                       <legend>Search </legend>
                       <table>
                           <tr>
                               <td>
                                   <asp:CheckBox ID="CBFile" runat="server" Text="File No" OnCheckedChanged="CBFile_CheckedChanged"
                                       AutoPostBack="true" />
                               </td>
                               <td>
                                   <asp:CheckBox ID="CBname" runat="server" Text="Patient Name" OnCheckedChanged="CBname_CheckedChanged"
                                       AutoPostBack="true" />
                               </td>
                               <td>
                                   <asp:CheckBox ID="CBMobile" runat="server" Text="Mobile No" OnCheckedChanged="CBMobile_CheckedChanged"
                                       AutoPostBack="true" />
                               </td>
                           </tr>                    
                       
                        
                           <tr>                           
                               <td>
                                <asp:TextBox ID="RTFileS" onkeyup="KeyUp();" runat="server" OnTextChanged="RTFileS_TextChanged" Visible="false"></asp:TextBox>                                 
                               </td>
                               <td colspan="2">
                                   <asp:TextBox ID="RTNameS" onkeyup="KeyUp();" runat="server" OnTextChanged="RTNameS_TextChanged" Visible="false"></asp:TextBox>                                 
                               </td>
                               <td>
                                   <asp:TextBox ID="RTMobileS" onkeyup="KeyUp();" runat="server" OnTextChanged="RTMobileS_TextChanged" Visible="false"></asp:TextBox>                               
                               </td>                          
                           </tr>
                           <tr>
                               <td colspan="4">                                                                 
                                   <telerik:RadGrid ID="gvPatientList" runat="server" AllowFilteringByColumn="True"
                                       AllowPaging="True" GridLines="None" OnSelectedIndexChanged="gvPatientList_SelectedIndexChanged" >
                                       <PagerStyle Mode="NextPrevAndNumeric" AlwaysVisible="true" />
                                       <GroupingSettings CaseSensitive="false" />
                                       <ItemStyle HorizontalAlign="Left" />
                                       <HeaderStyle HorizontalAlign="Left" />
                                       <AlternatingItemStyle HorizontalAlign="Left" />
                                       <ClientSettings EnablePostBackOnRowClick="true">
                                           <Selecting AllowRowSelect="true" />
                                       </ClientSettings>
                                       <MasterTableView AutoGenerateColumns="False" DataKeyNames="pt_regid">
                                           <CommandItemTemplate>
                                               <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                                           </CommandItemTemplate>
                                           <Columns>
                                               <telerik:GridBoundColumn HeaderText="Patient Name" UniqueName="pt_name" DataField="pt_name"
                                                   AllowFiltering="false">
                                               </telerik:GridBoundColumn>
                                               <telerik:GridBoundColumn HeaderText="File No" UniqueName="pt_fileno" DataField="pt_fileno"
                                                   AllowFiltering="false">
                                               </telerik:GridBoundColumn>
                                               <telerik:GridBoundColumn HeaderText="Mobile" UniqueName="pt_pmobileno" DataField="pt_pmobileno"
                                                   AllowFiltering="false">
                                               </telerik:GridBoundColumn>
                                           </Columns>
                                       </MasterTableView>
                                       <HeaderContextMenu EnableImageSprites="True" CssClass="GridContextMenu GridContextMenu_Default">
                                       </HeaderContextMenu>
                                   </telerik:RadGrid>                                   
                               </td>
                           </tr>
                       </table>
                       </fieldset>
               </td>
           </tr>
       </table>


On selecting a row on the grid,I am populating name column value to a textbox  and hiding the grid as below.
protected void gvPatientList_SelectedIndexChanged(object sender, EventArgs e)
    {
 
        GridDataItem RegId = gvPatientList.SelectedItems[0] as GridDataItem;
         string regid = RegId.GetDataKeyValue("pt_regid").ToString();
         
 
         
        foreach (GridDataItem dataItem in gvPatientList.Items)
        {
            if (dataItem.Selected)
            {
                RCFName.Text = dataItem["pt_name"].Text;              
            }
        }
        gvPatientList.Visible = false;             
         
    }


Above mentioned checkboxes act like radio buttons and javascript for the same is as below
<script language="CS" runat="server">
        private void makeRadioGroupFromCheckBoxes(IEnumerable<CheckBox> checkBoxes)
        {
            StringBuilder sb = new StringBuilder();
            foreach (CheckBox cb in checkBoxes)
            {
                foreach (CheckBox innercb in checkBoxes)
                {
                    if (innercb != cb)
                    {
                        sb.Append("document.getElementById('");
                        sb.Append(innercb.ClientID);
                        sb.Append("').checked = false;");
                    }
                }
                cb.Attributes["onclick"] = "if(this.checked){" + sb.ToString() + "}else{this.checked = true;}";
 
                sb = new StringBuilder();
            }
        }      
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.makeRadioGroupFromCheckBoxes(new CheckBox[] { CBFile, CBname, CBMobile });              
            }
        }
Grid gets populated on textchanged event and 'onkeyup="KeyUp();' function of the textbox,associated with it as below.
aspx:
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            var timer = null;
            function KeyUp() {
                if (timer != null) {
                    clearTimeout(timer);
                }
                timer = setTimeout(LoadTable, 500);
            }
            function LoadTable() {
                $find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("FilterGrid");
            }
         
        </script>
    </telerik:RadCodeBlock>
code behind:
protected void RadAjaxManager1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
       {
           if (e.Argument.IndexOf("FilterGrid") != -1)
           {
               gvPatientList.Rebind();
           }
       }
 
       protected void RTFileS_TextChanged(object sender, EventArgs e)
       {
           if (!string.IsNullOrEmpty(RTFileS.Text))
           {
                
               _scheduleService = new ScheduleService();
               clsSchedule clsschedule = new clsSchedule();
               string search = " where OldRegnNo like '" + RTFileS.Text + "%" + "'";
               gvPatientList.DataSource = _scheduleService.GetAllPatients(search);
               //gvPatientList.MasterTableView.Rebind();
               gvPatientList.Rebind();
           }
       }
On checkedchanged eveent of check boxes,I am making the grid visible.
protected void CBFile_CheckedChanged(object sender, EventArgs e)
   {
       if (CBFile.Checked)
       {
           RTFileS.Visible = true;
           gvPatientList.Visible = true;
           gvPatientList.MasterTableView.Visible = true;
           gvPatientList.Rebind();
          
       }
       else
       {
           RTFileS.Visible = false;
       }
   }

Ajaxmanager is as below:
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest">
       <AjaxSettings>
           <telerik:AjaxSetting AjaxControlID="gvPatientList">
               <UpdatedControls>
                   <telerik:AjaxUpdatedControl ControlID="gvPatientList" LoadingPanelID="RadAjaxLoadingPanel1" />
                   <telerik:AjaxUpdatedControl ControlID="RCFName" />
                   <%--<telerik:AjaxUpdatedControl ControlID="CBFile" />
                   <telerik:AjaxUpdatedControl ControlID="CBname" />
                   <telerik:AjaxUpdatedControl ControlID="CBMobile" />--%>
               </UpdatedControls>
           </telerik:AjaxSetting>
           <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
               <UpdatedControls>
                   <telerik:AjaxUpdatedControl ControlID="gvPatientList" LoadingPanelID="RadAjaxLoadingPanel1" />
               </UpdatedControls>
           </telerik:AjaxSetting>

I hope hiding/unhiding the grid gives me the error.Please suggest your ideas on this.
Thanks,
Soumya

1 Answer, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 28 Aug 2012, 07:59 AM
Hello,

 I checked the code in the CBFile_CheckedChanged event and the most likely cause of the error seems to be this line:

gvPatientList.MasterTableView.Visible = true;

When the grid has been hidden in advance it will not databind and render any data so the MasterTableView may not be initialized at all and throw the specified error.
Normally you do not need to separately show/hide the MasterTableView - it will inherit this setting from the grid, so you can remove the above line and see if this resolves the problem.Greetings,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Soumya
Top achievements
Rank 1
Answers by
Marin
Telerik team
Share this question
or