Dynamic datagrid EditCommand problem

G

Guest

Hi,

I created a Dynamic Datagrid and i added an EditCommandColumn to it.
Works fine, but my Editcommand eventhandler seems to have a problem with
PostBack
This is my code
private DataGrid GridDataLangs(DataTable vDataTable){
DataGrid myGrid = new DataGrid();
myGrid.AutoGenerateColumns = false;

//Add Columns
BoundColumn colDate = new BoundColumn();
colDate.HeaderText = "Date";
colDate.DataField = ("DateTimeFK");



EditCommandColumn colEditCommand = new EditCommandColumn();
colEditCommand.ButtonType = ButtonColumnType.LinkButton;
colEditCommand.UpdateText = "Update";
colEditCommand.EditText = "Edit";
colEditCommand.CancelText = "cancel";

myGrid.Columns.Add(colDate);
myGrid.Columns.Add(colEditCommand);

myGrid.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.Edit_Data);
myGrid.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.Cancel_Data);
myGrid.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.Save_Data);

myGrid.DataSource = vDataTable;
myGrid.DataBind();
return myGrid ;
}

private void Edit_Data(object
sender,System.Web.UI.WebControls.DataGridCommandEventArgs e){
if(Page.IsPostBack){
((DataGrid)sender).EditItemIndex = e.Item.ItemIndex;
this.lblInfo.Text = e.Item.ItemIndex.ToString();
}
}

Anyone knows what is wrong here?
 
T

Teemu Keiski

Hi,

DataGrid needs to be added to the Controls collection before binding it (or
generally setting any properties), therefore binding and after returning it
from method and adding to Controls collection won't work.

Second because it is a dynamical control, it needs to be recreated and added
to the controls collection in Page_Load at the latest, so that postback
events will be raised.

See this article
Dynamic Web Controls, Postbacks, and View State
http://aspnet.4guysfromrolla.com/articles/092904-1.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top