隐藏网格的问题

时间:2012-08-23 09:20:52

标签: c# asp.net radgrid

我有一个场景如下。 有3个复选框.3文本框与each.Grid相关联在文本框中输入搜索值时填充。在此网格上选择一行时,我需要隐藏网格,并将选定的值(一列)填充到另一个文本框中我正在获取“对象引用未设置为对象的实例。”,当我实现隐藏网格的逻辑时。选中了检查框,在搜索文本框中输入值,填充网格,选择行,第二个文本框填充网格值,网格被隐藏 - >在此过程重复2-3次后,再次选中复选框会出现错误。 请找到带有上述控件的标记。

<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>

在网格上选择一行时,我将名称列值填充到文本框并隐藏网格,如下所示。

 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;             

    }

上面提到的复选框就像单选按钮和javascript一样,如下所示

  <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 });              
            }
        }

网格在textchanged事件和'onkeyup =“KeyUp();'上填充文本框的功能,与其关联如下。 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>
代码背后的代码:

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.Rebind();
       }
   }

在复选框的checkchanged eveent上,我正在显示网格。

 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如下:

 <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" />                  
               </UpdatedControls>
           </telerik:AjaxSetting>
           <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
               <UpdatedControls>
                   <telerik:AjaxUpdatedControl ControlID="gvPatientList" LoadingPanelID="RadAjaxLoadingPanel1" />
               </UpdatedControls>
           </telerik:AjaxSetting>

我希望隐藏/取消隐藏网格给我错误。请在此建议您的想法。 谢谢, Soumya

1 个答案:

答案 0 :(得分:0)

请尝试使用以下代码段。你得到错误,因为你试图ajaxify显示错误的网格。

protected void Page_PreRender(object sender, EventArgs e)
    {
        if (gvPatientList.Visible)
        {
            RadAjaxManager1.AjaxSettings.AddAjaxSetting(gvPatientList, gvPatientList, RadAjaxLoadingPanel1);
            RadAjaxManager1.AjaxSettings.AddAjaxSetting(gvPatientList, RCFName);
        }

    }