You don't show your GridView pager code, but keep in mind that when using a GV pager, then a full GV re-load occurs, and worse yet, a full page post-back occurs.
And you also don't mention what you plan to do after writing all that code to "persist" the text box, then all all that extra work, you most likely will want to save that text box back to the database right? (I mean, you going to want to do something with those text boxes after all that amount of work, then, right?
So, it turns out the amount of code to persist the text box is about the same to SAVE the text box values back into the database. Once we do that, then we achieve persisting of data for free efforts so to speak.
There are 2 approaches I recommend. Which choice depends on how large your data set is you are feeding to the GV and data pager. So, for up to say maybe 300 rows, the following code will work quite nice. I note the 300 row max, since this solution involves "persisting" the GV datasource into session. So, that's why I noted this works for about 300 rows, since anything more, then we are loading and persisting too much data into session, and that going to result in performance and memory issues down the road.
So, to persist the text box values, then we bind the textbox to the given database row and column/value that we want to use. If on paging of data, we always save the text box to the database row, then we can dump and eliminate all of the code you have trying to persist the text box, and trade that code for code that always sends/saves the text box data to the database when you page data.
So, "some" code to send the text box back to the table is STILL MUCH LESS code then all that code that attempts to persist and save the text box into viewstate or other appropraches here.
Worse yet? After you done all the above, then we can then send all updates to the persisted datatable back to the database WITH ONE simple operation.
So, here is a working proof of concept of the above:.
First, the markup with one description text box.
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="ID"
CssClass="table table-hover" Width="50%"
AllowPaging="true" PageSize="6" OnPageIndexChanging="GridView1_PageIndexChanging"
>
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" />
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:TextBox ID="txtDescription"
runat="server" TextMode="MultiLine"
Text= '<%# Eval("Description") %>'
Height="100px" Width="330px"
>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="GridPager" />
</asp:GridView>
<br />
<asp:Button ID="cmdSave" runat="server" Text="Save/Done"
CssClass="btn btn-dark"
OnClick="cmdSave_Click"/>
And now the code behind is this:
DataTable rstHotels = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
Session["rstHotels"] = rstHotels;
}
else
{
rstHotels = (DataTable)Session["rstHotels"];
}
}
void LoadData()
{
string strSQL =
"SELECT * FROM tblHotelsA ORDER BY HotelName";
rstHotels = General.MyRst(strSQL);
GridView1.DataSource = rstHotels;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridToTable();
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = rstHotels;
GridView1.DataBind();
}
void GridToTable()
{
// send grid rows back to table.
foreach (GridViewRow rRow in GridView1.Rows)
{
int RecordPtr = rRow.DataItemIndex; // note data index, not row index!!!
DataRow OneDataRow;
OneDataRow = rstHotels.Rows[RecordPtr];
OneDataRow["Description"] = ((TextBox)rRow.FindControl("txtDescription")).Text;
}
}
protected void cmdSave_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
string strSQL = "SELECT * FROM tblHotelsA";
using (SqlCommand cmd = new SqlCommand(strSQL, conn))
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder daU = new SqlCommandBuilder(da);
conn.Open();
da.Update(rstHotels);
}
}
// code here no doubt will navagate to a different page
// since we are done.
}
Note close, the above thus gives BOTH persisting of the text box when paging, but ALSO saves all the changes back to the database, and as you can see ,not a lot of code here at all.
The result is thus this:
Note close, I enter some text on the first page, go to 2nd page, and then return back - you see the text box maintains its value.
And note how easy the final save button is to send all the changes on any page of data back to the database in one operation.