Wrong event handler firing in DataGrid

A

arlie_maija

Hey -

I'm writing a control that contains a DataGrid, and I'm unable to get
the update event to fire. When I click the update link, the edit event
fires.

heres the details...

my control overrides CreateChildControls and dynamically creates the
DataGrid, creates an EditCommandColumn which it adds to the
DataGrid.Columns, and then adds several BoundColumns. I then create a
dummy dataset and bind it to the DataGrid. I then create and set an
EditCommandEventHandler for the EditCommand, CancelCommand, and
UpdateCommand. finally I add the datagrid to the Control array.
...
in Render I tell the DataGrid to render.

When I run this, It looks right, and the edit and cancel buttons work
fine, but the update link fires the Edit event.

heres my code... any help on whats going on here would be really
appreciated.

private DataGrid dataGrid1;
private Label lblMsg;
private string messageText = "";
private DataSet myDataSet;

private string MessageText
{
get
{
return messageText;
}
set
{
messageText = value;
}
}


protected override void Render(HtmlTextWriter output)
{
// Start Main Table
output.AddAttribute("width", "100%");
output.RenderBeginTag(HtmlTextWriterTag.Table);

// Write the Message Text
output.RenderBeginTag(HtmlTextWriterTag.Thead);
output.RenderBeginTag(HtmlTextWriterTag.Tr);
output.AddAttribute("class", "message");
output.AddAttribute("colSpan", "2");
output.RenderBeginTag(HtmlTextWriterTag.Th);
output.Write(MessageText);
output.RenderEndTag();
output.RenderEndTag();
output.RenderEndTag();

// Start Main table body
output.RenderBeginTag(HtmlTextWriterTag.Tbody);
output.RenderBeginTag(HtmlTextWriterTag.Tr);

// Write the DataGrid column
output.AddAttribute("align", "center");
output.AddAttribute("valign", "top");
output.RenderBeginTag(HtmlTextWriterTag.Td);
dataGrid1.RenderControl(output);
output.RenderEndTag();

output.RenderEndTag(); // row
output.RenderEndTag(); // main table body
output.RenderEndTag(); // main table
}
public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}

protected override void CreateChildControls()
{
Controls.Clear();

lblMsg = new Label();
Controls.Add(lblMsg);

// DataGrid
dataGrid1 = new DataGrid();
dataGrid1.ID = "dataGrid";
dataGrid1.EnableViewState = true;
dataGrid1.AutoGenerateColumns = false;
FormatDataGrid();
CreateEditCol();
CreateBoundCols();
MakeParentTable();
BindToDataGrid();
dataGrid1.EditCommand +=new
DataGridCommandEventHandler(dataGrid1_EditCommand);
dataGrid1.CancelCommand +=new
DataGridCommandEventHandler(dataGrid1_CancelCommand);
dataGrid1.UpdateCommand += new
DataGridCommandEventHandler(dataGrid1_UpdateCommand);
Controls.Add(dataGrid1);
}


private void FormatDataGrid()
{
dataGrid1.CssClass = "DataGridTable";
dataGrid1.HeaderStyle.CssClass = "DataGridHeading";
dataGrid1.ItemStyle.CssClass = "DataGridRow";
dataGrid1.AlternatingItemStyle.CssClass = "DataGridAltRow";
dataGrid1.EditItemStyle.CssClass = "DataGridEditRow";
}

private void CreateEditCol()
{
EditCommandColumn editCol = new EditCommandColumn();

editCol.ButtonType = ButtonColumnType.LinkButton;
editCol.CancelText = "Cancel";
editCol.EditText = "Edit";
editCol.UpdateText = "Update";

dataGrid1.Columns.Add(editCol);
}
private void CreateBoundCols()
{
//create the bound cols in the grid
.... create bound cols for the datatable and add them
to the dataGrid...
dataGrid1.Columns.Add(myBoundColumn);
}

private void MakeParentTable()
{
// Create a new DataTable.
System.Data.DataTable myDataTable = new DataTable("ParentTable");
// Declare variables for DataColumn and DataRow objects.
DataColumn myDataColumn;
DataRow myDataRow;
... add a few columns, and then populate a few rows...
}

private void BindToDataGrid()
{
// Turn off AutoGenerate of Columns
// Set DataSource
dataGrid1.DataSource = myDataSet; //,"ParentTable"

// Bind
dataGrid1.DataBind();
}


private void dataGrid1_EditCommand(object sender,
DataGridCommandEventArgs e)
{
MessageText = "Edit link was clicked.";
dataGrid1.EditItemIndex = e.Item.ItemIndex;
dataGrid1.DataBind();
}
private void dataGrid1_CancelCommand(object sender,
DataGridCommandEventArgs e)
{
MessageText = "Cancel was clicked";
dataGrid1.EditItemIndex = -1;
dataGrid1.DataBind();
}
private void dataGrid1_UpdateCommand(object sender,
DataGridCommandEventArgs e)
{
MessageText = "Update link was clicked";

dataGrid1.EditItemIndex = -1;
dataGrid1.DataBind();
}
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top