Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP NET. Rad Grid. Lose changed values on postback

I am using a RadGrid with multiple columns which can be edited (BatchEdit like excel).

<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" AutoGenerateColumns="False"GridLines="Both"OnNeedDataSource="RadGrid1_NeedDataSource" AllowAutomaticInserts="True" AllowAutomaticUpdates="True"AllowAutomaticDeletes="True">
            <ItemStyle Wrap="false" />
            <MasterTableView TableLayout="Fixed" NoMasterRecordsText="" ShowFooter="true" EditMode="Batch">
                ...
            </MasterTableView> </telerik:RadGrid>

Datasource of RadGrid is ObjectDataSource

<asp:ObjectDataSource ID="TestSource" runat="server" TypeName="TestClass" SelectMethod="GetAllItems">
      <UpdateParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="LastName" Type="String" />
                <asp:Parameter Name="BirthData" Type="DateTime" />
      </UpdateParameters> </asp:ObjectDataSource>

On OnNeedDataSource event I'm setting Id of ObjectDataSource to RadGrid.DataSourceId.

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
        RadGrid1.DataSourceID = "TestSource";
}

In current Page I have also button "Send email" send rad gird inserted values.

<asp:Button ID="SendEmail" OnClick="SendEmail_Click" Text="Send" runat="server" />

Problem is that on SendEmail_Click DataSource is null. But I want to get newly changed DataSource.

protected void SendEmail_Click(object sender, EventArgs e)
{
        RadGrid1.Rebind();
        // RadGrid1.Datasource is null
}

How can I solve this problem? Many thanks.

like image 528
Mirzodaler Ataev Avatar asked Oct 21 '16 09:10

Mirzodaler Ataev


1 Answers

DataSource in GridView is not stored in any persistent way across Postback so you have to save it somewhere or you have to request it again from your database. Because of that your datasource is null.

This is suggested code in their Forum for how to take the source of your grid.

    DataTable dtRecords = new DataTable();
    foreach (GridColumn col in grdShippedOrders.Columns)
    {
        DataColumn colString = new DataColumn(col.UniqueName);
        dtRecords.Columns.Add(colString);

    }
    foreach (GridDataItem row in grdShippedOrders.Items) // loops through each rows in RadGrid
    {
        DataRow dr = dtRecords.NewRow();
        foreach (GridColumn col in grdShippedOrders.Columns) //loops through each column in RadGrid
               dr[col.UniqueName] = row[col.UniqueName].Text;
        dtRecords.Rows.Add(dr);
    }
    return dtRecords; 
like image 66
mybirthname Avatar answered Sep 21 '22 04:09

mybirthname