Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
      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();

          BoundField bf = new BoundField();
          bf.DataField = "Monday";
          txtmonday.Text = GridView1.Rows[getindex].Cells[3].Text;
          GridView1.Rows[getindex].Cells[3].Controls.Add(txtmonday);

  }





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
Comments
[no name] 15-Jul-13 23:04pm    
convert gridview value to String
Manikandan Sekar 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 19-Jul-13 12:26pm    
If any answer had helped you please "Accept Answer" which marked question as Solved and can help others

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
C#
if (!IsPostBack)
            {
                TextBox t = new TextBox();
                t.ID = "txt1";
                pnl1.Controls.Add(t);

            }


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

            }


This is just concept not exact solution to your problem .. I guess you get the logic.
 
Share this answer
 
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 -


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




It will work in gridview for textchange event.


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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900