Hi
I have below Gridview. There is value in Session Date but it is showing ""
foreach (GridViewRow gr in grdPlanning.Rows)
{
if (gr.RowType == DataControlRowType.DataRow)
{
if (gr.Cells[0].Enabled == true)
{
sessionDate = (gr.FindControl("txtSessionDate") as TextBox).Text.Trim();
}
}
}
Thanks
Mohamed Azarudeen ZPosted May 20, 2023, 12:03 PM
Based on the provided code, it seems that you are trying to retrieve the value of the `TextBox` control with the ID "txtSessionDate" inside the `GridView` named "grdPlanning". However, you mentioned that the value is showing as an empty string even though there is a value in the Session Date.
One possible reason for this issue is that you are attempting to access the value during a postback, where the GridView is rebound and the TextBox values are reset. The `GridView` control in ASP.NET Web Forms does not automatically retain the values of dynamic controls like TextBoxes after a postback.
To overcome this issue, you can store the TextBox values in a persistent storage mechanism, such as Session state or ViewState, during the `RowDataBound` event. Then, during the postback, you can retrieve the values from the storage mechanism.
Here's an example of how you can modify your code to store and retrieve the TextBox values using Session state:
By storing the TextBox values in Session state during the `RowDataBound` event and retrieving them during the postback, you can ensure that the values are not lost and can be accessed correctly. Remember to clear the Session values when they are no longer needed to avoid unnecessary memory usage.
You can adjust the code accordingly if you prefer to use a different storage mechanism, such as ViewState or a custom solution.