Click here to Skip to main content
11,798,866 members (72,784 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# ASP.NET
I am trying to edit my detailed gridview based on master gridview click.I am adding a textbox dynamically and editing it.when i click on the update button it gives me the old value whereas i require the new.This is what i have done so far.Please suggest.

protected void btnedit_Click(object sender, EventArgs e)

      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();
      SqlCommand cmdfrgetweekend = new SqlCommand("select s001_Weekending as Weekend from dbo.s001_Timesheets where s001_TimesheetsID ='" +id+ "' ", connfordata);
      SqlDataAdapter dafrweekend = new SqlDataAdapter(cmdfrgetweekend);
          BoundField bf = new BoundField();
          bf.DataField = "Monday";
          txtmonday.Text = GridView1.Rows[getindex].Cells[3].Text;

protected void btnupdate_Click(object sender, EventArgs e)
    //string get = txtmonday.Text;
    //String sValue = Request.Form["txtmonday"];

    Button btn = (Button)sender;
    GridViewRow gvr = (GridViewRow)btn.NamingContainer;
    int getindex = gvr.RowIndex;
    TextBox tb = GridView1.FindControl("txtmonday") as TextBox;


Is it possible to get the changed value from dynamically created textbox in another function?
Posted 13-Jul-13 12:53pm
senthil kumar - 2185 at 15-Jul-13 23:04pm
convert gridview value to String
Manikandan Sekar at 16-Jul-13 9:27am
yes it is possible but you need to give the row index that is the row number correctly during the operation
sumit_kapadia at 19-Jul-13 12:26pm
If any answer had helped you please "Accept Answer" which marked question as Solved and can help others
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Dynamic Controls not avaialbe after postback reason is simple. As web is disconnect in nature it create one object of your page then create required HTML using that and send back to browser that renders it. So ViewState comes into picture to take care after post back and mange control on page despite multiple postbacks. Means dynamic controls not added to ViewState? Yes.

However any thing on HTML page that has type of input has ability to sent data to server. Means your text box is input type and does post data So how to get that value . Look below

if I add some control to page on page load like
if (!IsPostBack)
                TextBox t = new TextBox();
                t.ID = "txt1";

then I can get values after postback as
if (Request["txt1"] != null)
                value = Request["txt1"];

This is just concept not exact solution to your problem .. I guess you get the logic.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Hello ,

put your textBox in Item template and write hard coded "AutoPostBack="True" ontextchanged="txtBox_TextChanged" " on that textBox on aspx page after that

in code behind create event of that text box Like -

protected void txtBox_TextChanged(object sender, EventArgs e)
    {  }

It will work in gridview for textchange event.

<asp:TemplateField HeaderText ="Sch.End Date" >
                    <ItemTemplate >
                        <asp:TextBox ID="txtBox" runat="server" ontextchanged="txtBox_TextChanged"></asp:TextBox>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 505
1 OriginalGriff 503
2 Richard MacCutchan 321
3 Maciej Los 245
4 phil.o 244
0 OriginalGriff 2,270
1 Maciej Los 1,810
2 KrunalRohit 1,496
3 CPallini 1,425
4 Richard MacCutchan 1,109

Advertise | Privacy | Mobile
Web04 | 2.8.151002.1 | Last Updated 17 Jul 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100