onclick event not firing

G

Guest

I've got a datalist that includes in each databound row a button to delete that specific record. But for some reason, clicking the button does not trigger the event handler. here's the datalist

<form runat="server"><asp:Label id="lblErrorText" runat="server" cssclass="FormValidationErrorText" enableviewstate="False"></asp:Label><asp:Label id="lblAgreementNbr" runat="server" enableviewstate="False"></asp:Label><br /><br /><br /><asp:datalist id="dlEquipment" runat="server" DataKeyField="EquipmentID" OnItemDataBound="FormatDataListRow"><HeaderTemplate><table width="800" border="1" ><tbody><tr><td><asp:Label id="lblEquipmentType" runat="server" cssclass="InputLabelText">Equipmen
Type: </asp:Label></td><td><asp:Label id="lblBrand" runat="server" cssclass="InputLabelText">Brand: </asp:Label></td><td><asp:Label id="lblModelNbr" runat="server" cssclass="InputLabelText">Model Number: </asp:Label></td><td><asp:Label id="lblSerialNbr" runat="server" cssclass="InputLabelText">Serial Number: </asp:Label></td></tr></HeaderTemplate><ItemTemplate><tr><td><input id="hdnEquipmentID" type="hidden" name="hdnEquipmentID" runat="server" value='<%#Databinder.Eval(Container.DataItem, "EquipmentID")%>' /><CustomControl:EquipmentDropDown id="ddlEquipmentType" runat="server" SelectedItemValue='<%#Databinder.Eval(Container.DataItem, "EquipmentTypeID")%>'></CustomControl:EquipmentDropDown></td><td><CustomControl:BrandDropDown id="ddlBrand" runat="server" Filter="showPrivateList" SelectedItemValue='<%#Databinder.Eval(Container.DataItem, "MfgCode")%>'></CustomControl:BrandDropDown></td><td width="18%"><asp:TextBox id="txtModelNbr" Text='<%#Databinder.Eval(Container.DataItem, "ModelNbr")%>' runat="server"></asp:TextBox><asp:RequiredFieldValidator id="vldModelNbr" runat="server" CssClass="FormValidationErrorText" ErrorMessage="Model Number" ControlToValidate="txtModelNbr" Enabled="False" EnableViewState="True" Display="None"></asp:RequiredFieldValidator></td><td width="18%"><asp:TextBox id="txtSerialNbr" Text='<%#Databinder.Eval(Container.DataItem, "SerialNbr")%>' runat="server"></asp:TextBox><asp:RequiredFieldValidator id="vldSerialNbr" runat="server" CssClass="FormValidationErrorText" ErrorMessage="Serial Number" ControlToValidate="txtSerialNbr" Enabled="False" EnableViewState="True" Display="None"></asp:RequiredFieldValidator></td><td

***************** here's the button *****************
<asp:Button id="btnDelete" CommandName='<%#Databinder.Eval(Container.DataItem, "EquipmentID")%>' runat="server" Text="Delete item"
onClick="btnDelete_Click"></asp:Button></td></tr></ItemTemplate><FooterTemplate><tr><td colspan="5" align="center"><input type="submit" value="submit" />&nbsp;<input type="reset" value="cancel" /></td></tr></tbody></table></FooterTemplate></asp:datalist></form

------------------- Here's the event handle

Sub btnDelete_Click(Sender as ObJect, E as EventArgs
tr
cmd.CommandText = "DELETE FROM equipment WHERE EquipmentID = " & Cint(Sender.CommandName
cmd.ExecuteNonQuery(
lblAgreementNbr.Text = "The selected equipment has been deleted. <a href='AgreementListing.aspx'>Click here</a> to return to the agreement listing.
catch exc as Exceptio
lblAgreementNbr.Visible = Fals
lblErrorText.Text = "An error occurred while trying to delete the equipment.
End Tr
End Su
 
T

twostepted

Anyone,

I'm having the same problem. Here is what I've done:

Event Signature:
protected void btnDelete_Click(object sender, System.EventArgs e)

In InitializeComponent:
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);

In .aspx file:
<asp:Button id="btnDelete" CssClass="clsButton" runat="server"
Text="Delete" ToolTip="Permanently delete this message"
OnClick="btnDelete_Click" />

I've tried taking the OnClick="btnDelete_Click" out of the <asp:Button>
tag. I've also tried commenting out the this.btnDelete.Click += new
System.EventHandler(this.btnDelete_Click). Neither has any effect.
I'm never making it into the btnDelete_Click event. Does anyone have
any ideas why?

Thanks,

--twostepted
 
N

No One

twostepted said:
Anyone,

I'm having the same problem. Here is what I've done:

Event Signature:
protected void btnDelete_Click(object sender, System.EventArgs e)

In InitializeComponent:
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);

In .aspx file:
<asp:Button id="btnDelete" CssClass="clsButton" runat="server"
Text="Delete" ToolTip="Permanently delete this message"
OnClick="btnDelete_Click" />

I've tried taking the OnClick="btnDelete_Click" out of the <asp:Button>
tag. I've also tried commenting out the this.btnDelete.Click += new
System.EventHandler(this.btnDelete_Click). Neither has any effect.
I'm never making it into the btnDelete_Click event. Does anyone have
any ideas why?

Thanks,

--twostepted

Does your <asp:Button /> HTML code have Runat="Server"?
 
G

Guest

The event is not raised because the button is in a DataList and the DataList
renamed the children controls ... and so the event isn't catch in the page.

If you want to handle the event you must (C#) :

1/ Rewrite your button as :
<asp:button id="btnDelete" commandName="Delete"
commandArgument='<%#Databinder.Eval(Container.DataItem, "EquipmentID")%>'
runat="server" text="Delete item" >

2/ handle the ItemCommand event from the DataList
this.DataList1.ItemCommand += new EventHandler (DataList1_ItemCommand)

3/ Evaluate the command name from the handler function parameter
private void DataList1_ItemCommand (object sender, DataListCommandEventArgs e)
{
if ( e.CommandName == "Delete")
{
// write your code here
BusinessLayer.Items.Delete (e.CommandArgument);
}
}

Note that commands named : "Update", "Cancel" and "Delete" could be directly
handled by specifics DataList's events : UpdateCommand, CancelCommand and
DeleteCommand.
The ItemCommand is the common event for all command events.
 

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

Top