datagrid update/cancel command not working

M

madhu.datla

Hi,

I created a page with datagrid where i can edit, update, cancel the
data. but rather strangely the update and cancel commands are not
working. Only Edit button works.

Did any one face the same problem before. Neither
dgMasterData_CancelCommand() nor dgMasterData_UpdateCommand() is
getting the control.

I debugged and found that when I click update command the control goes
to dgMasterData_EditCommand(), Also e.commandName has value "Edit"
instead of "Update".

(I already tried the ultimate solutions: Closing VS.Net and reloading
the project,deleting the event handlers, and readding etc) ;)

any pointers? I have a decent amount of time trying to figure out what
was happening. but no luck..
 
D

DanG

I set up my screens like this:

<asp:TemplateColumn>
<ItemStyle Wrap="false"></ItemStyle>
<ItemTemplate>
<asp:imagebutton runat="server" CommandName="Edit"
BorderWidth="0" ImageURL="../images/edit.gif" alt="Edit"/>
<asp:imagebutton runat="server" CommandName="Delete"
id="btnDelete" BorderWidth="0" ImageURL="../images/delete.gif"
alt="Delete" />
</ItemTemplate>

<EditItemTemplate>
<asp:imagebutton runat="server" CommandName="Update"
BorderWidth="0" ImageURL="../images/update.gif" alt="Save"/>
<asp:imagebutton runat="server" CommandName="Cancel"
BorderWidth="0" ImageURL="../images/cancel.gif" alt="Cancel"/>
</EditItemTemplate>

<FooterTemplate>
<asp:imagebutton runat="server" CommandName="Insert"
BorderWidth="0" ImageURL="../images/add.gif" alt="Add"/>
</FooterTemplate>
</asp:TemplateColumn>

And this is the codebehind:

public void DataGrid_ItemCommand (Object sender,
DataGridCommandEventArgs e) {
if (((ImageButton)e.CommandSource).CommandName == "Insert")
{DataGrid_Insert(sender, e);}
if (((ImageButton)e.CommandSource).CommandName == "Delete")
{DataGrid_Delete(sender, e);}
}

public void DataGrid_Edit (Object sender, DataGridCommandEventArgs e) {
DataGrid.EditItemIndex = e.Item.ItemIndex;
DataGrid.ShowFooter = false;
}

public void DataGrid_Cancel (Object sender, DataGridCommandEventArgs e)
{
DataGrid.EditItemIndex = -1;
DataGrid.ShowFooter = true;
}


public void DataGrid_Update (Object sender, DataGridCommandEventArgs e)
{
DataGrid.EditItemIndex = -1;
DataGrid.ShowFooter = true;
}

public void DataGrid_Insert (Object sender, DataGridCommandEventArgs e)
{
DataGrid.EditItemIndex = -1;
}

public void DataGrid_Delete (Object sender, DataGridCommandEventArgs e)
{
DataGrid.EditItemIndex = -1;
}

//Add a "confirm" to the Delete button
public void DataGrid_ItemCreated(Object sender, DataGridItemEventArgs
e) {
ListItemType itemType = e.Item.ItemType;
if (itemType == ListItemType.Item || itemType ==
ListItemType.AlternatingItem) {
ImageButton ib = (ImageButton) e.Item.FindControl("btnDelete");
if (ib != null) {
ib.Attributes.Add("onclick", "return confirm('Delete this
record?');");
}
}
}

If you're running VB, be sure you haven't lost the "handles" clauses on
your events. I hate it when that happens!!

HTH
Dan
 
M

madhu.datla

Thanks Dan and Gaurav,

events are properly wired and here is the code : let me know if you
find something wrong here..

<ASP:DATAGRID ID="dgMasterData" RUNAT="server" SHOWFOOTER="True"
AUTOGENERATECOLUMNS="False"
ONUPDATECOMMAND="dgMasterData_UpdateCommand"
ONCANCELCOMMAND="dgMasterData_CancelCommand">
<EDITITEMSTYLE CSSCLASS="dgEditItem"></EDITITEMSTYLE>
<ALTERNATINGITEMSTYLE
CSSCLASS="dgAlternateItem"></ALTERNATINGITEMSTYLE>
<ITEMSTYLE CSSCLASS="dgItemStyle"></ITEMSTYLE>
<HEADERSTYLE CSSCLASS="dgHeader"></HEADERSTYLE>
<COLUMNS>
<ASP:TEMPLATECOLUMN VISIBLE="False">
<ITEMTEMPLATE>
<ASP:LABEL ID="lblMstrDataID" RUNAT="server" TEXT='<%#
DataBinder.Eval(Container.DataItem,"mstrDataID")%>'>
</ASP:LABEL>
</ITEMTEMPLATE>
</ASP:TEMPLATECOLUMN>
<ASP:TEMPLATECOLUMN HEADERTEXT="Description">
<ITEMTEMPLATE>
<%# DataBinder.Eval(Container.DataItem,"mstrDataValue")%>
</ITEMTEMPLATE>
<FOOTERTEMPLATE>
<ASP:TEXTBOX ID="tbNewMasterDataValue" RUNAT="server"
CSSCLASS="tboxClass"></ASP:TEXTBOX>
</FOOTERTEMPLATE>
<EDITITEMTEMPLATE>
<ASP:TEXTBOX ID="tbMasterDataValue" RUNAT="server"
CSSCLASS="tboxClass" TEXT='<%#
DataBinder.Eval(Container.DataItem,"mstrDataValue")%>'>
</ASP:TEXTBOX>
</EDITITEMTEMPLATE>
</ASP:TEMPLATECOLUMN>
<ASP:EDITCOMMANDCOLUMN BUTTONTYPE="PushButton" UPDATETEXT="Update"
CANCELTEXT="Cancel" EDITTEXT="Edit"></ASP:EDITCOMMANDCOLUMN>
<ASP:TEMPLATECOLUMN>
<FOOTERTEMPLATE>
<ASP:BUTTON ID="btnSaveNewMstrData" RUNAT="server" TEXT="Add New"
ONMOUSEOVER="btn_FocusOn(this)"
ONMOUSEOUT="btn_FocusOff(this)" CSSCLASS="mfbuttonCellText"
COMMANDNAME="Add New"></ASP:BUTTON>
</FOOTERTEMPLATE>
</ASP:TEMPLATECOLUMN>
</COLUMNS>
</ASP:DATAGRID>



private void InitializeComponent()
{
this.ddMasterData.SelectedIndexChanged += new
System.EventHandler(this.ddMasterData_SelectedIndexChanged);
this.dgMasterData.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMasterData_CancelCommand);
this.dgMasterData.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMasterData_EditCommand);
this.dgMasterData.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMasterData_UpdateCommand);
this.dgMasterData.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.dgMasterData_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);

}

private void dgMasterData_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
dgMasterData.EditItemIndex = e.Item.ItemIndex;
}
catch(Exception ex)
{
Response.Write(ex.Message.ToString());
}

}

public void dgMasterData_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgMasterData.SelectedIndex = -1;
}

public void dgMasterData_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
.........
}
 

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,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top