0

There's a gridview in my program whose row's data is to be loaded in textboxes if edit is clicked.

Here is the code where I'm filling the data in textboxes after edit is clicked

protected void GV_Parameters_RowEditing(object sender, GridViewEditEventArgs e)
{
        GV_Parameters.EditIndex = e.NewEditIndex;        
        int index = e.NewEditIndex;

        IsEditing = true;

        Label lbl_frmdate = (Label)GV_Parameters.Rows[index].FindControl("lbl_frmdate") as Label;
        Label lbl_todate = (Label)GV_Parameters.Rows[index].FindControl("lbl_todate") as Label;

        EditFromDate = lbl_frmdate.Text.ToUpper();
        lbl_frm_edit.Text = EditFromDate;       
        EditToDate = lbl_todate.Text.ToUpper();
        lbl_to_edit.Text = EditToDate;

        Label lbl = (Label)GV_Parameters.Rows[index].Cells[3].FindControl("lbl_Is_holiday");
        string is_holiday = lbl.Text;       


        Label lbl_std_intime = (Label)GV_Parameters.Rows[index].FindControl("lbl_std_intime") as Label;
        Label lbl_std_outtime = (Label)GV_Parameters.Rows[index].FindControl("lbl_std_outtime") as Label;

        string[] arr_std_intime;
        string[] arr_std_outtime;

        if (is_holiday != "1")//working day
        {
            r_work_holiday.SelectedIndex = 0;
            IsHoliday = false;

            if (lbl_std_intime.Text != "")
            {
                arr_std_intime = lbl_std_intime.Text.Split(':');

                txt_std_TimeInHours.Text = arr_std_intime[0].ToString();

                txt_std_TimeInMins.Text = arr_std_intime[1].ToString();
            }

            if (lbl_std_outtime.Text != "")
            {
                arr_std_outtime = lbl_std_outtime.Text.Split(':');                
                txt_std_TimeOutHours.Text = arr_std_outtime[0].ToString();
                               txt_std_TimeOutMins.Text = arr_std_outtime[1].ToString();
            }
        }
        else
        {
            IsHoliday = true;
            //r_workingday.Checked = false;
            //r_holiday.Checked = true;
            r_work_holiday.SelectedIndex = 1;
            Label lbl_remarks = (Label)GV_Parameters.Rows[index].FindControl("lbl_remarks") as Label;
            txt_holiday_desc.Text = lbl_remarks.Text;
        }    

        collapse_state = "expand";
}

There is a radiobutton list who shows the edited row is holiday or working day, if user changes the selection in radioButtonList, PostBack occurs and this is the time when all of the texboxes turn blank.

protected void r_work_holiday_SelectedIndexChanged(object sender, EventArgs e)
{
        if(r_work_holiday.SelectedIndex==0)
        {
            IsHoliday = false;
        }
        else
        {
            IsHoliday = true;
        }

        collapse_state = "expand";
}

There's no any method in page load who is clearing the textboxes.

protected void Page_Load(object sender, EventArgs e)
{
        if (IsEditing)
        {
            collapse_state = "expand";
        }

        if (!Page.IsPostBack)           
        {
            BindYears();          
        }
}

Please help

Update ::

lbl_frm_edit and lbl_to_edit are not getting reset after postback. The variable EditFromDate and EditToDate are being set by Viewstate

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alina Anjum
  • 1,178
  • 6
  • 30
  • 53

1 Answers1

0

It's been sometime I worked in ASP controls but if I remember correctly, the content in textboxes are generally cleared on every post back by ASP .NET. The state of the textboxes are not saved.

Here is an answer I found while researching this issue.

You could store the value of the textboxes in ViewState and assign them back on the PageLoad event. Something like

txt_std_TimeOutHours.Text = arr_std_outtime[0].ToString();
txt_std_TimeOutMins.Text = arr_std_outtime[1].ToString();
ViewState["TimeOutHours"] = arr_std_outtime[0].ToString();
ViewState["TimeOutMins"] = arr_std_outtime[1].ToString();

And on the PageLoad event you can do this to restore the values.

if(Page.IsPostBack)
{
   txt_std_TimeOutHours.Text = ViewState["TimeOutHours"].ToString();
   txt_std_TimeOutMins.Text = ViewState["TimeOutMins"].ToString();
}

Hope this helps!