471,595 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,595 software developers and data experts.

Wrong event handler firing in DataGrid

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
0 1434

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by SteveC | last post: by
5 posts views Thread by csgraham74 | last post: by
2 posts views Thread by Deepesh | last post: by
1 post views Thread by Mark Gilkes | last post: by
19 posts views Thread by Daniela Roman | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.