How to retain value between postbacks asp.net

SoccerBoss
Category:
Other
Price: $10 USD

Question description

I am dynamically creating textboxes and adding it to the gridview.When i edit the textbox and update it the value is lost.Please help me out This is what i have done so far.


 protected void btnedit_Click(object sender, EventArgs e)
    {
       // txtmonday.ID = "txtmonday1";
        int index = GridView1.EditIndex;
        Button btn = (Button)sender;
       
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        int getindex = gvr.RowIndex;
        //string headertext = this.GridView1.Columns[1].HeaderText;
        string id = GridView1.Rows[getindex].Cells[4].Text;
        //DataTable dtfrweekend = new DataTable();
        //connfordata.Open();
        //SqlCommand cmdfrgetweekend = new SqlCommand("select s001_Weekending as Weekend from dbo.s001_Timesheets where s001_TimesheetsID ='" +id+ "' ", connfordata);
        //SqlDataAdapter dafrweekend = new SqlDataAdapter(cmdfrgetweekend);
        //dafrweekend.Fill(dtfrweekend);
        //connfordata.Close();
            TextBox txtmonday = new TextBox();
            string get = GridView1.HeaderRow.Cells[3].Text.ToString();
            BoundField bf = new BoundField();
            bf.DataField = "Monday";
            txtmonday.Text = GridView1.Rows[getindex].Cells[3].Text;
            Page.Form.Controls.Add(txtmonday);
            GridView1.Rows[getindex].Cells[3].Controls.Add(txtmonday);      
    }
When i click on update it postbacks and value is lost.below is snippet of update.Please help now anyone 


   protected void btnupdate_Click(object sender, EventArgs e)
    {
            int index = GridView1.EditIndex=-1;
            TextBox txtmonday = new TextBox();
            //string get = txtmonday.Text;
            //TextBox tbfrreplace = new TextBox();
            Button btn = (Button)sender;
            GridViewRow gvr = (GridViewRow)btn.NamingContainer;
            int getindex = gvr.RowIndex;
           
           
            BoundField bf2 = new BoundField();
            bf2.DataField = "Monday";
            string monday =   txtmonday.Text;
           
        //tbfrreplace.Text = GridView1.Rows[getindex].Cells[3].Text;
         //TextBox tbfrreplace1 = GridView1.FindControl("txtmonday") as TextBox;
        // GridView1.Rows[getindex].Cells[3].Controls.Add(tbfrreplace1);
    }

Tutor Answer

(Top Tutor) Daniel C.
(997)
School: Duke University
PREMIUM TUTOR
Studypool has helped 1,244,100 students
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1830 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors