Click here to Skip to main content
15,913,211 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What i want to do is to assign a textbox dynamically created from a table cell into a gridview row button clicked.This is what i have done so far.Any help will be appreciated.


C#
protected override object SaveViewState()
{
    var viewState = new object[2];
    //Saving the dropdownlist value to the View State
    viewState[0] = txtmonday.Text;
    return viewState;
}

protected override void LoadViewState(object savedState)
{
    //Getting the dropdown list value from view state.
    if (savedState is object[] && ((object[])savedState).Length == 2)
    {
        var viewState = (object[])savedState;
        //var count = int.Parse(viewState[0].ToString());
        CreateDynamicControl();
        base.LoadViewState(viewState[1]);//cheged from 1 to 0
    }
    else
    {
        base.LoadViewState(savedState);
    }
}


public void CreateDynamicControl()
{
    var txtmonday1 = new TextBox();
    var textboxCell = new TableCell();
    var tableRow = new TableRow();
    tableRow.Cells.Add(textboxCell);
    textboxCell.Controls.Add(txtmonday1);
    tbl.Rows.Add(tableRow);
   // txtmonday1 = (TextBox)txtmonday1;
   // int count = GridView1
   // GridView1.Controls.Add(txtmonday1);



}


protected void btnupdate_Click(object sender, EventArgs e)
{




    foreach (TableRow row in tbl.Rows)
    {
        var textbox = row.Cells[0].Controls[0] as TextBox;
    }





}



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 id = GridView1.Rows[getindex].Cells[4].Text;



        TextBox txtmonday = new TextBox();
        txtmonday.ID = "Textbox1";
        string get = GridView1.HeaderRow.Cells[3].Text.ToString();


        txtmonday.Text = GridView1.Rows[getindex].Cells[3].Text;
        Page.Form.Controls.Add(txtmonday);
        GridView1.Rows[getindex].Cells[3].Controls.Add(txtmonday);

}
Posted
Updated 15-Jul-13 3:19am
v2
Comments
Dholakiya Ankit 15-Jul-13 8:22am    
so where's the problem?
Amit Karyekar 15-Jul-13 8:39am    
the problem is the textbox is in the different location and i want that in a particular cell of gridview based on edit button click in the row.So how can i get that in the [row][cell]

1 solution

If you want the txtbox in a particular cell means put the textbox in the edititemtemplate in the table which is inside the gridview so when editing is done the txtbox gets displayed instead of the itemtemplate associated control
 
Share this answer
 

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