在单击ImageButton创建动态行后,RadGrid中的用户输入数据丢失

本文关键字:用户 输入 数据 RadGrid ImageButton 单击 创建 动态 | 更新日期: 2023-09-27 18:04:03

我有一个包含网格视图的aspx页面。在回发网格期间,绑定到网格视图的数据将丢失。原因是什么呢?

我在下面附加了代码,当图像按钮被点击时,用户在控件上输入的数据就会消失。

aspx页面:

<telerik:RadGrid ID="GridOfficeInfo" runat="server" AllowMultiRowSelection="false"        OnItemCommand="GridOfficeInfo_ItemCommand" BorderWidth="0" AutoGenerateColumns="false" Width="100%" Visible="true" >
  <MasterTableView>
    <Columns>
      <telerik:GridTemplateColumn>
        <HeaderStyle BorderWidth="0" HorizontalAlign="Center" CssClass="OfficeInfoHeader" />
        <ItemStyle BorderWidth="0" HorizontalAlign="Center" CssClass="OfficeRow" />
        <HeaderTemplate>
          <table border="0" cellpadding="0" cellspacing="0" width="100%">
            <tr>
              <td align="left">
                <asp:Label runat="server" ID="Label1" Text="Office Info" CssClass="Label">
              </asp:Label>
            </td>
              <%--                                            <td align="right">
                <asp:LinkButton ID="lnkOfficeInfo" runat="server" Font-Underline="true" Text="Click here for map" OnClick="lnkOfficeInfo_Click" CssClass="Label">
              </asp:LinkButton>
            </td>
    --%>
          </tr>
        </table>
      </HeaderTemplate>
        <ItemTemplate>
          <table id="TableOfficeinfo" border="0" width="100%" runat="server">
            <col style="width:16%;" />
            <col style="width:18%;" />
            <col style="width:15%;" />
            <col style="width:18%;" />
            <col style="width:15%;" />
            <col style="width:18%;" />
            <tr>
              <td align="left" colspan="2" style="border-width:0;">
                <asp:Label ID="LblOffice" Text="Primary Practice Location" runat="server" CssClass="LabelItalics">
              </asp:Label>
            </td>
              <td align="left" style="border-width:0;">
                <asp:Label ID="LblBillAdd" Text="Billing Address" runat="server" CssClass="Label">
              </asp:Label>
            </td>
              <td align="left" style="border-width:0;">
                <asp:CheckBox runat="server" ID="ChkBillAdd" Checked="false" CssClass="ProviderCb" AutoPostBack="true" OnCheckedChanged="ChkBillAdd_CheckedChanged" />
              </td>
              <td align="right" colspan="2" style="border-width:0;">
                <asp:LinkButton ID="lnkOfficeMap" runat="server" Font-Underline="true" Text="Click here for map" OnClick="lnkOfficeInfo_Click" CssClass="Label">
              </asp:LinkButton>
            </td>
          </tr>
            <tr>
              <td align="left" style="border-width:0;">
                <asp:Label ID="LblStreet" Text="Street Address" runat="server" CssClass="Label">
              </asp:Label>
            </td>
              <td align="left" style="border-width:0;">
                <asp:TextBox runat="server" ID="TxtStreet" Text='<%#Eval("StreetAddress")%>' Enabled="false" CssClass="ProviderTextbox">
              </asp:TextBox>
            </td>
              <td align="left" style="border-width:0;">
                <asp:Label ID="LblSuite" Text="Suite" runat="server" CssClass="Label">
              </asp:Label>
            </td>
              <td align="left" style="border-width:0;">
                <asp:TextBox runat="server" ID="TxtSuite" Text='<%#Eval("Suite")%>' Enabled="false" CssClass="ProviderTextbox">
              </asp:TextBox>
            </td>
              <td align="left" style="border-width:0;">
                <asp:Label runat="server" ID="LblCity" Text="City" CssClass="Label">
              </asp:Label>
            </td>
              <td align="left" style="border-width:0;">
                <asp:TextBox runat="server" ID="TxtCity" Text='<%#Eval("City")%>' Enabled="false" CssClass="ProviderTextbox">
              </asp:TextBox>
            </td>
          </tr>
            <tr>
              <td align="left" style="border-width:0;">
                <asp:Label ID="LblState" Text="State" runat="server" CssClass="Label">
              </asp:Label>
            </td>
              <td align="left" style="border-width:0;">
                <asp:TextBox runat="server" ID="TxtState" Text='<%#Eval("State")%>' Enabled="false" CssClass="ProviderTextbox">
              </asp:TextBox>
                <telerik:RadComboBox runat="server" ID="cbOfficeInfo" CssClass="ProviderCombo" Visible="false" EnableTextSelection="true" SortCaseSensitive="false" Height="100">
                  <Items>
                    <telerik:RadComboBoxItem runat="server" Text="Choose State" />
                    <telerik:RadComboBoxItem runat="server" Text="AL" />
                    <telerik:RadComboBoxItem runat="server" Text="AK" />
                    <telerik:RadComboBoxItem runat="server" Text="AZ" />
                    <telerik:RadComboBoxItem runat="server" Text="AR" />
                    <telerik:RadComboBoxItem runat="server" Text="CA" />
                    <telerik:RadComboBoxItem runat="server" Text="CO" />
                    <telerik:RadComboBoxItem runat="server" Text="CT" />
                    <telerik:RadComboBoxItem runat="server" Text="DE" />
                    <telerik:RadComboBoxItem runat="server" Text="DC" />
                    <telerik:RadComboBoxItem runat="server" Text="FL" />
                    <telerik:RadComboBoxItem runat="server" Text="GA" />
                    <telerik:RadComboBoxItem runat="server" Text="HI" />
                    <telerik:RadComboBoxItem runat="server" Text="ID" />
                    <telerik:RadComboBoxItem runat="server" Text="IL" />
                    <telerik:RadComboBoxItem runat="server" Text="IN" />
                    <telerik:RadComboBoxItem runat="server" Text="IA" />
                    <telerik:RadComboBoxItem runat="server" Text="KS" />
                    <telerik:RadComboBoxItem runat="server" Text="KY" />
                    <telerik:RadComboBoxItem runat="server" Text="LA" />
                    <telerik:RadComboBoxItem runat="server" Text="ME" />
                    <telerik:RadComboBoxItem runat="server" Text="MD" />
                    <telerik:RadComboBoxItem runat="server" Text="MA" />
                    <telerik:RadComboBoxItem runat="server" Text="MI" />
                    <telerik:RadComboBoxItem runat="server" Text="MN" />
                    <telerik:RadComboBoxItem runat="server" Text="MS" />
                    <telerik:RadComboBoxItem runat="server" Text="MO" />
                    <telerik:RadComboBoxItem runat="server" Text="MT" />
                    <telerik:RadComboBoxItem runat="server" Text="NE" />
                    <telerik:RadComboBoxItem runat="server" Text="NV" />
                    <telerik:RadComboBoxItem runat="server" Text="NH" />
                    <telerik:RadComboBoxItem runat="server" Text="NJ" />
                    <telerik:RadComboBoxItem runat="server" Text="NM" />
                    <telerik:RadComboBoxItem runat="server" Text="NY" />
                    <telerik:RadComboBoxItem runat="server" Text="NC" />
                    <telerik:RadComboBoxItem runat="server" Text="ND" />
                    <telerik:RadComboBoxItem runat="server" Text="OH" />
                    <telerik:RadComboBoxItem runat="server" Text="OK" />
                    <telerik:RadComboBoxItem runat="server" Text="OR" />
                    <telerik:RadComboBoxItem runat="server" Text="PA" />
                    <telerik:RadComboBoxItem runat="server" Text="RI" />
                    <telerik:RadComboBoxItem runat="server" Text="SC" />
                    <telerik:RadComboBoxItem runat="server" Text="SD" />
                    <telerik:RadComboBoxItem runat="server" Text="TN" />
                    <telerik:RadComboBoxItem runat="server" Text="TX" />
                    <telerik:RadComboBoxItem runat="server" Text="UT" />
                    <telerik:RadComboBoxItem runat="server" Text="VT" />
                    <telerik:RadComboBoxItem runat="server" Text="VA" />
                    <telerik:RadComboBoxItem runat="server" Text="WA" />
                    <telerik:RadComboBoxItem runat="server" Text="WV" />
                    <telerik:RadComboBoxItem runat="server" Text="WI" />
                    <telerik:RadComboBoxItem runat="server" Text="WY" />
                  </Items>
                </telerik:RadComboBox>
              </td>
              <td align="left" style="border-width:0;">
                <asp:Label ID="LblZip" Text="Zip" runat="server" CssClass="Label">
              </asp:Label>
            </td>
              <td align="left" style="border-width:0;">
                <asp:TextBox runat="server" ID="TxtZip" Text='<%#Eval("Zip")%>' Enabled="false" CssClass="ProviderTextbox">
              </asp:TextBox>
            </td>
              <td align="left" style="border-width:0;">
                <asp:Label runat="server" ID="LblContact" Text="Office Contact" CssClass="Label">
              </asp:Label>
            </td>
              <td align="left" style="border-width:0;">
                <asp:TextBox runat="server" ID="TxtContact" Text='<%#Eval("OfficeContact")%>' Enabled="true" CssClass="ProviderTextbox">
              </asp:TextBox>
            </td>
          </tr>
            <tr>
              <td align="left" style="border-width:0;">
                <asp:Label ID="LblEMRUsed" Text="EMR Used" runat="server" CssClass="Label">
              </asp:Label>
            </td>
              <td align="left" style="border-width:0;">
                <telerik:RadComboBox runat="server" ID="CbEMRUsed" EnableTextSelection="true" CssClass="ProviderCombo" AutoPostBack="true" OnSelectedIndexChanged="CbEMRUsed_SelectedIndexChanged">
                  <Items>
                    <telerik:RadComboBoxItem runat="server" Text="Yes" />
                    <telerik:RadComboBoxItem runat="server" Text="No" />
                  </Items>
                </telerik:RadComboBox>
              </td>
              <td align="left" style="border-width:0;">
                <asp:Label ID="LblEMRName" Text="EMR Name" runat="server" CssClass="Label">
              </asp:Label>
            </td>
              <td align="left" style="border-width:0;">
                <telerik:RadComboBox runat="server" ID="CbEMRName" EnableTextSelection="true" CssClass="ProviderCombo" Height="200">
                  <Items>
                    <telerik:RadComboBoxItem runat="server" Text="Select" Selected="true" />
                  </Items>
                </telerik:RadComboBox>
              </td>
              <td align="left" style="border-width:0;">
                <asp:Label runat="server" ID="LblUse" Text="Meaningful Use" CssClass="Label">
              </asp:Label>
            </td>
              <td align="left" style="border-width:0;">
                <asp:CheckBox runat="server" ID="MeaningUse" Checked="false" CssClass="ProviderCb" AutoPostBack="true" OnCheckedChanged="MeaningUse_CheckedChanged"/>
              </td>
            </tr>
            <tr>
              <td align="left" style="border-width:0;">
                <asp:Label ID="LblOffTel1" Text="Office Tel # 1" runat="server" CssClass="Label">
              </asp:Label>
            </td>
              <td align="left" style="border-width:0;">
                <telerik:RadMaskedTextBox ID="TxtOffTel1" runat="server" Enabled="false" Mask="(###) ###-####" Width="88.5%" Text='<%#Eval("OfficeTel_1")%>' DisabledStyle-BackColor="#E5E5E5">
              </telerik:RadMaskedTextBox>
                <%--<asp:TextBox runat="server" ID="TxtOffTel1" Text='<%#Eval("OfficeTel_1")%>' Enabled="false" CssClass="ProviderTextbox">
              </asp:TextBox>--%>
            </td>
              <td align="left" style="border-width:0;">
                <asp:Label ID="LblOffTel2" Text="Office Tel # 2" runat="server" CssClass="Label">
              </asp:Label>
            </td>
              <td align="left" style="border-width:0;">
                <telerik:RadMaskedTextBox ID="TxtOffTel2" runat="server" Enabled="false" Mask="(###) ###-####" Width="88.5%" Text='<%#Eval("OfficeTel_2")%>' DisabledStyle-BackColor="#E5E5E5">
              </telerik:RadMaskedTextBox>
                <%--<asp:TextBox runat="server" ID="TxtOffTel2" Text='<%#Eval("OfficeTel_2")%>' Enabled="false" CssClass="ProviderTextbox">
              </asp:TextBox>--%>
            </td>
              <td align="left" style="border-width:0;">
                <asp:Label runat="server" ID="LblOffFax" Text="Office Fax" CssClass="Label">
              </asp:Label>
            </td>
              <td align="left" style="border-width:0;">
                <telerik:RadMaskedTextBox ID="TxtOffFax" runat="server" Enabled="false" Mask="(###) ###-####" Width="88.5%" Text='<%#Eval("OfficeFax")%>' DisabledStyle-BackColor="#E5E5E5">
              </telerik:RadMaskedTextBox>
                <%--<asp:TextBox runat="server" ID="TxtOffFax" Text='<%#Eval("OfficeFax")%>' Enabled="false" CssClass="ProviderTextbox">
              </asp:TextBox>--%>
            </td>
          </tr>
            <tr>
              <td align="left" style="border-width:0;">
                <asp:Label ID="LblIDTCount" Text="IDT Assigned Count" runat="server" CssClass="Label">
              </asp:Label>
            </td>
              <td align="left" style="border-width:0;">
                <asp:TextBox runat="server" ID="TxtIDTCount" Text='<%#Eval("IDTAssignedCount")%>' Enabled="false" CssClass="ProviderTextbox">
              </asp:TextBox>
            </td>
              <td align="left" style="border-width:0;">
                <asp:Label ID="LblLastDate" Text="Last Visit Date" runat="server" CssClass="Label">
              </asp:Label>
            </td>
              <td align="left" style="border-width:0;">
                <telerik:RadDatePicker runat="server" ID="DpLastDate" CssClass="ProviderDtpicker">
                  <DateInput DateFormat="MM/dd/yyyy">
                </DateInput>
              </telerik:RadDatePicker>
            </td>
              <td align="left" style="border-width:0;">
                <asp:Label runat="server" ID="LblWaitTime" Text="Avg. Wait Time" CssClass="Label">
              </asp:Label>
            </td>
              <td align="left" style="border-width:0;">
                <asp:TextBox runat="server" ID="TxtWaitTime" Text='<%#Eval("AvgWaitTime")%>' Enabled="false" CssClass="ProviderTextbox">
              </asp:TextBox>
            </td>
          </tr>
            <tr id="trAddOffice" runat="server">
              <td align="left" colspan="6" style="border-width:0;">
                <asp:ImageButton ID="AddOfficeRow" runat="server" UseSubmitBehavior="false" ImageUrl="~/Images/AddDiag1.png" OnClick="AddOfficeRow_Click" />
              </td>
            </tr>
          </table>
        </ItemTemplate>
      </telerik:GridTemplateColumn>
    </Columns>
  </MasterTableView>
  <ClientSettings >
</ClientSettings>
</telerik:RadGrid>   

aspx.cs页

protected void AddOfficeRow_Click(object sender, ImageClickEventArgs e)
{        
    //int Rno = GridOfficeInfo.Items.Count + 1;
    int Rno = GridOfficeInfo.Items.Count + 1;
    PopulateGridOfficeInfo(Rno);                
}
public void PopulateGridOfficeInfo(int RowNo)
{
    //bool[] bBillingAddress = new bool[RowNo - 1];
    //bool[] bMeaningfulUse = new bool[RowNo - 1];
    string strCbEMRUsed = string.Empty;
    string strCbEMRName = string.Empty;
    string strcbOfficeInfo = string.Empty;
    DataTable dt = new DataTable();
    dt.Columns.Add("StreetAddress");//Text='<%#Eval("StreetAddress")%>'
    dt.Columns.Add("Suite");//
    dt.Columns.Add("City");//
    dt.Columns.Add("State");//
    dt.Columns.Add("Zip");//
    dt.Columns.Add("OfficeContact");//
    dt.Columns.Add("OfficeTel_1");//
    dt.Columns.Add("OfficeTel_2");//
    dt.Columns.Add("OfficeFax");//
    dt.Columns.Add("IDTAssignedCount");//
    //dt.Columns.Add("LastDate", typeof(DateTime?));
    dt.Columns.Add("AvgWaitTime");//
    for (int i = 0; i < RowNo; i++)
    {
        //string lnkOfficeMap = string.Empty;
        string StreetAddress = string.Empty;
        string Suite = string.Empty;
        string City = string.Empty;
        string State = string.Empty;
        string Zip = string.Empty;
        string OfficeContact = string.Empty;
        string OfficeTel_1 = string.Empty;
        string OfficeTel_2 = string.Empty;
        string OfficeFax = string.Empty;
        string IDTAssignedCount = string.Empty;
        string AvgWaitTime = string.Empty;
        if (i < RowNo - RowNo)
        {
            //bBillingAddress[i] = ((CheckBox)GridOfficeInfo.Items[i].FindControl("ChkBillAdd")).Checked;
            //lnkOfficeMap = ((LinkButton)GridOfficeInfo.Items[i].FindControl("Click here for map")).Text;
            StreetAddress = ((TextBox)GridOfficeInfo.Items[i].FindControl("TxtStreet")).Text;
            Suite = ((TextBox)GridOfficeInfo.Items[i].FindControl("TxtSuite")).Text;
            City = ((TextBox)GridOfficeInfo.Items[i].FindControl("TxtCity")).Text;
            State = ((TextBox)GridOfficeInfo.Items[i].FindControl("TxtState")).Text;
            Zip = ((TextBox)GridOfficeInfo.Items[i].FindControl("TxtZip")).Text;
            OfficeContact = ((TextBox)GridOfficeInfo.Items[i].FindControl("TxtContact")).Text;
            //bMeaningfulUse[i] = ((CheckBox)GridOfficeInfo.Items[i].FindControl("MeaningUse")).Checked;
            OfficeTel_1 = ((RadMaskedTextBox)GridOfficeInfo.Items[i].FindControl("TxtOffTel1")).Text;
            //OfficeTel_1 = ((TextBox)GridOfficeInfo.Items[i].FindControl("TxtOffTel1")).Text;
            OfficeTel_2 = ((RadMaskedTextBox)GridOfficeInfo.Items[i].FindControl("TxtOffTel2")).Text;
            //OfficeTel_2 = ((TextBox)GridOfficeInfo.Items[i].FindControl("TxtOffTel2")).Text;
            OfficeFax = ((RadMaskedTextBox)GridOfficeInfo.Items[i].FindControl("TxtOffFax")).Text;
            //OfficeFax = ((TextBox)GridOfficeInfo.Items[i].FindControl("TxtOffFax")).Text;
            IDTAssignedCount = ((TextBox)GridOfficeInfo.Items[i].FindControl("TxtIDTCount")).Text;
            AvgWaitTime = ((TextBox)GridOfficeInfo.Items[i].FindControl("TxtWaitTime")).Text;
        }
        dt.Rows.Add(StreetAddress, Suite, City, State, Zip, OfficeContact, OfficeTel_1, OfficeTel_2, OfficeFax, IDTAssignedCount, AvgWaitTime);
    }
    GridOfficeInfo.DataSource = dt;
    GridOfficeInfo.DataBind();
}

在单击ImageButton创建动态行后,RadGrid中的用户输入数据丢失

使用NeedDataSource事件来提供一个数据源。我最好的猜测是,回发后它不再有数据源:https://demos.telerik.com/aspnet-ajax/grid/examples/data-binding/simple-vs-advanced/defaultcs.aspx。您将看到手动提供数据源时需要多久提供一次。如果禁用viewstate,也会出现类似的问题。