you have to write code in code-behind file only

Dani AI

Generated

A few practical points that connect the replies above and will get this working reliably.

The casting error that reported (a DataControlLinkButton being returned instead of a TextBox) is a symptom of relying on index-based access into a cell's Controls collection. CommandField/Buttons and validators can change the control order, so Controls[0] is brittle. 's Controls-chain shows the nesting but not the fragility: prefer explicit IDs and FindControl or declarative data sources instead of numeric indexes.

If you keep server-side handling, use TemplateField with an EditItemTemplate that contains a TextBox with a known ID, then locate it with FindControl in RowUpdating. Example pattern:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    var row = GridView1.Rows[e.RowIndex];
    var txtName = row.FindControl("txtName") as TextBox;
    if (txtName != null) e.NewValues["Name"] = txtName.Text;
}

If the goal really is "no source-code," use a declarative DataSource (SqlDataSource or ObjectDataSource) and set the GridView's DataSourceID plus a CommandField (ShowEditButton="True"). Configure the UpdateCommand (or let ObjectDataSource handle update methods) and set DataKeyNames for the primary key; GridView and SqlDataSource then perform edit/update without code-behind. See the ASP.NET GridView editing walkthrough for details: .

Quick troubleshooting: inspect row.Controls in the debugger to see actual control types, confirm DataKeyNames are set, set GridView.EditIndex back to -1 and rebind after a successful update, or switch BoundField -> TemplateField for predictable control IDs. If posting your GridView markup (as requested), include the DataSource/fields so responses can give exact fixes.

Recommended Answers

All 4 Replies

This code may help you:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
      GridViewRow r = this.GridView1.Controls[0].Controls[e.RowIndex] as GridViewRow;
      TextBox t=  r.Controls[cell_index].Controls[control_index] as Text Box;
     }

This Syntax for Grid View:

GridView.Controls(0) ------ Table

GridView.Controls(0).Controls(index) ------ GridViewRow

GridView.Controls(0).Controls(index).Controls(index) ------ TableCell. For BoundField, we can use

getting null value after writting this code.

My code is:-

TextBox tname =(TextBox ) GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
        ds.Tables[0].Rows[e.RowIndex ][1] = tname.Text;
        TextBox tgender = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0] ;
        ds.Tables[0].Rows[e.RowIndex][2] = tgender.Text;

when i run the application then it throwing an exception: Unable to cast object of type
'System.Web.UI.WebControls.DataControlLinkButton' to type 'System.Web.UI.WebControls.TextBox'.

pharindra,

Can you please supply your code for the the GridView? That would help a lot in trying to find any problems that may be in the code.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.