By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,847 Members | 2,261 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,847 IT Pros & Developers. It's quick & easy.

Wrong event handler firing in DataGrid

P: n/a
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_CancelComman d);
dataGrid1.UpdateCommand += new
DataGridCommandEventHandler(dataGrid1_UpdateComman d);
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();
}

Dec 8 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.