我有一个场景如下。有 3 个复选框。每个关联的 3 个文本框。在文本框中输入搜索值时会填充网格。在此网格上选择一行时,我需要隐藏网格并将所选值(一列)填充到另一个文本框中。我得到“对象引用未设置为对象的实例。”,当我实现隐藏网格的逻辑时。选中复选框,在搜索文本框中输入值,填充网格,选择一行,第二个文本框填充网格值,网格被隐藏->在此过程重复 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();
}
}
在复选框的选中更改事件上,我使网格可见。
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>
我希望隐藏/取消隐藏网格会给我带来错误。请就此提出您的想法。谢谢, 苏米亚