I have an ImageButton in a GridView.
<asp:GridView ID="ItemGridView" runat="server" DataKeyNames="Id"
OnRowDataBound="ItemGridView_RowDataBound" AllowPaging="True"
AllowSorting="True" EnableSortingAndPagingCallbacks="True"
AutoGenerateEditButton="False" AutoGenerateDeleteButton="false"
DataSourceID="ItemDataSource" EnableViewState="true" >
....
<asp:TemplateField ShowHeader="False" ItemStyle-Width="40px">
<ItemTemplate>
<asp:ImageButton ID="btnDelete" SkinID="btnDelete"
runat="server" CausesValidation="false"
OnClick="btnDeleteAccountItem_Click"
OnClientClick="javascript:return confirm('Are you sure?');" />
</ItemTemplate>
</asp:TemplateField>
and a corresponding handler for the delete button event
protected void btnDeleteAccountItem_Click(object sender, EventArgs e) {
ImageButton btnDel = sender as ImageButton;
GridViewRow row = (GridViewRow)btnDel.NamingContainer;
....
}
I am using this very same construct in many places and it works fine. I have one gridview now, though, where it does not, and I am hoping to get some ideas for how to track down the problem.
When I click the button, the client-side event fires and the alert box is displayed, then the post-back fires and I can hit a break point in the Page_Load method. So the client-side wiring of the button events seems to work. However, the event is not handled and the method btnDeleteAccountItem_Click does not get called.
This is a complex page and I cannot post all the code. What can I do to narrow down potential causes?
Your event is defined incorrectly ImageButton.Click:
protected void btnDeleteAccountItem_Click(object sender, ImageClickEventArgs e) {
ImageButton btnDel = sender as ImageButton;
GridViewRow row = (GridViewRow)btnDel.NamingContainer;
....
}
I'm not sure if this will solve it, but once I placed a asp:Button control in my markup and generated the 'onClick' signature for it too.
I then changed my mind and decided to make it an Image button... ... I simply rewrote the tag myself.
After making those changes, I realised that the onClick signature wasn't working anymore... after some research I found an answer... I was using 'EventArgs' instead of 'ImageClickEventArgs'...
(object sender, ImageClickEventArgs e)
Once I changed the event arg object, it started working as normal.
rather than creating a button click event, you could use the datagrid row command event
You can then use e.commandName and e.commandArgument to find out which button was pressed and what its argument is:
Private Sub gv1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv1.RowCommand
If e.CommandName = "Whatever" Then
// do something
End If
Hope it helps
make sure the CausesValidation is set to True.
You can try (you are creating wrong click event of ImageButton) :
<asp:TemplateField ShowHeader="False" ItemStyle-Width="40px">
<ItemTemplate>
<asp:ImageButton ID="btnDelete" SkinID="btnDelete" runat="server"
CausesValidation="false" OnClick="btnDeleteAccountItem_Click"
OnClientClick="javascript:return confirm('Are you sure?');" />
</ItemTemplate>
</asp:TemplateField>
and for image button there are change in click event like as follow :
protected void btnDelete_Click(object sender, ImageClickEventArgs e)
{
ImageButton btnDel = sender as ImageButton;
GridViewRow row = (GridViewRow)btnDel.NamingContainer;
}
Related
I have this code
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Command1">
<img src="../images/image1.png" alt="" />
</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
and this code
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Command1")
{
// Never drops here
}
}
The event is being triggered.
But when I debug, LinkButton1's CommandName is not visible in source code.
So, the if statement doesn't work.
Any ideas ?
Edit :
I realised that I have an other error my page that belongs this situation.
Then I used GridView rather than DataList and used the GridView's RowCommand Event and fixed this.
The reason is because your attempting to obtain the CommandName from the DataList not the LinkButton. Your code would work if you did the following:
protected void btnSample_Click(object sender, EventArgs e)
{
// Instantiate:
var command = ((LinkButton)sender).CommandName;
// Do additional logic.
}
I believe your intent is to actually do something on the Click Event. If that isn't your case, you would need to FindControl on an event within your DataList, then instantiate the LinkButton to obtain the CommandName. Hopefully this points you in the proper direction.
I would do a sample within the DataList, but without exact implementation and additional information I wouldn't be able to. It would be similar to the above, just allocating the Control within the current control.
I have a user control that allows the user to add/edit a worker. When the user clicks the 'Add Worker' button the user control appears in a DevExpress Popup. All of the following button are in an update panel to prevent Postbacks.
When I edit a user (pencil) everything works fine. To edit a user I enter a last name, click search (magnifying glass) and then click edit (pencil). It's only when I load the page and click add the save/cancel buttons do not work.
I add the control in asp.net
<dx:PopupControlContentControl ID="PopupControlContentControl2" runat="server" SupportsDisabledAttribute="True">
<uc:WorkerAddEdit ID="wae" runat="server" OnOnWAECancelEvent="wae_OnWAECancelEvent" OnOnWAESaveEvent="wae_OnWAESaveEvent" />
</dx:PopupControlContentControl>
Here is the C# code behind the edit (the one that works correctly. The pencil)
protected void btnEditWorker_Click(object sender, EventArgs e)
{
SetupSessions();
wae.WorkerEdit = loadedWorker;
pucAddEditWorker.HeaderText = "Edit Worker";
pucAddEditWorker.ShowOnPageLoad = true;
}
Here is the C# code behind for the add (the round + that doesn't work)
protected void btnAddWorker_Click(object sender, EventArgs e)
{
wae.WorkerEdit = null;
pucAddEditWorker.HeaderText = "Add Worker";
pucAddEditWorker.ShowOnPageLoad = true;
}
Here is the asp.net section of the save and cancel button. This shows both onClick calls
<td><dx:ASPxButton ID="btnSave" runat="server" Text="Save" Theme="MetropolisBlue"
Width="50px" Height="20px" style="float:right;" onclick="btnSave_Click" /></td>
<td><dx:ASPxButton ID="btnCancel" runat="server" Text="Cancel"
Theme="MetropolisBlue" Width="50px" Height="20px" style="float:right;"
onclick="btnCancel_Click" /></td>
Here is the events in the code behind
protected void btnCancel_Click(object sender, EventArgs e)
{
//Do Work Here
}
protected void btnSave_Click(object sender, EventArgs e)
{
// Do Work Here
}
If I put a break point on either the save or cancel click event nothing ever happens. I've been googling for a while now with no luck.
Thanks in advance.
In Design mode of the form, have you tried clicking the control once, and check it on Properties (Events) window? Maybe the Click event does not have any method selected.
Select the btnSave_Click method in the Click label.
I figured out what the problem was. I am using a textbox from DevExpress. Further up in my code I had the textbox as follows:
<dx:ASPxTextBox ID="txtPhoneNumber" runat="server" Width="100px" Theme="MetropolisBlue" >
<MaskSettings Mask="(999) 000-0000" IncludeLiterals="None" />
<ValidationSettings Display="None">
</ValidationSettings>
</dx:ASPxTextBox>
Because I had the 0's in the mask it was trying to validate whatever was in the text box. Because I turned off the validation settings (ValidationSettings Display="None") I never saw the error but it was still validating. I made a change to this:
<dx:ASPxTextBox ID="txtPhoneNumber" runat="server" Width="100px" Theme="MetropolisBlue">
<MaskSettings Mask="(999) 999-9999" IncludeLiterals="None" />
<ValidationSettings Display="None">
</ValidationSettings>
</dx:ASPxTextBox>
and everything worked fine. I just started using DevExpress and it shows! Thanks to everyone for the help!
In my ASP.NET page's Page_Load I'm trying to determine whether a certain button has been clicked and is attempting a postback:
if (Page.IsPostBack)
{
if (Request.Params.Get("__EVENTARGUMENT") == "doStuff")
doSomething();
}
doStuff is a JavaScript within the markup, but I don't want this alone to trigger the doSomething() method call, I also need to be able to ensure that the button the user has clicked is correct.
How can I identify and reference the button control from the code behind once the user clicks? I searched for and discovered this but when I try to implement it, the control returned is always null.
Or use the CommandName and command events.
<telerik:RadButton ID="RadButton1" runat="server" CommandName="first" CommandArgument="one" OnCommand="CommandHandler" />
<telerik:RadButton ID="RadButton2" runat="server" CommandName="second" CommandArgument="two" OnCommand="CommandHandler" />
<asp:Label ID="Label1" Text="" runat="server" />
<asp:Label ID="Label2" Text="" runat="server" />
protected void CommandHandler(object sender, CommandEventArgs e)
{
Label1.Text = e.CommandArgument.ToString();
Label2.Text = e.CommandName;
}
You haven't really explained what you're trying to do, but it sounds like you would have a much easier time if you added an OnClick event handler to the button instead of trying to figure it out in Page_Load. The event handler would only be executed when that button is clicked.
I have an ImageButton within a GridView in .aspx on clicking this ImageButton i have to call a function.
This is how i tried and the function was not being called.
Code inside.aspx page:
<GridView ......>
<asp:HyperLink ID="HyperLink2" runat="server"
NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"VehID","mngVeh.aspx?delid={0}") %>'>
<asp:ImageButton runat="server" ID="DeleteUrlImageButton"
width='24' height='24'
ImageUrl="~/images/delete.jpeg"
OnClick="DeleteUrlImageButton_Click"
OnClientClick="return confirm('Are you sure you want to delete?');" />
<!--<img src="images/delete.jpeg" alt='edit' border="0" width='24' height='24'/> -->
</asp:HyperLink>
</GridView>
code in .aspx.cs page:
public void DeleteUrlImageButton_Click(object sender, EventArgs e)
{
//code to perform the necessary action.
}
Because you are wrapping your ImageButton inside of a Hyperlink, the browser is probably going to the hyperlink's URL instead of posting back to hit the OnClick function. You should have the DeleteUrlImageButton_Click function call Server.Transfer or Response.Redirect to the appropriate URL and get rid of the Hyperlink.
Sure it won't be fired because it is nested in a Hyperlink. So the imagebutton serves as the text for the hperlink and hyperlink does not cause postback. ImageButton can cause the desired action only if it stands out of the Hyperlink. Try this:
<asp:GridView ....
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton runat="server" ID="DeleteUrlImageButton"
width='24' height='24'
ImageUrl="~/images/delete.jpeg"
OnClick="DeleteUrlImageButton_Click"
OnClientClick="return confirm('Are you sure you want to delete?');"
PostBackUrl='<%# DataBinder.Eval(Container.DataItem,"VehID","mngVeh.aspx?delid={0}")
%>'/>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
ImageButton can do the job no need for Hyperlink just use the postbackurl and it will redirect you to the page. You can omit the HyperLink.
Button controls (like LinkButton,ImageButton and Button) are designed to cause postback by default.
Edit: Make sure event name and arguments are correct. This is the event I used to test it. y the way don't forget to place the ImageButton in a TemplateField, refer the code above
protected void DeleteUrlImageButton_Click(object sender, ImageClickEventArgs e)
{
TextBox5.Text = "Fired ";
//Response.Redirect( ((ImageButton)sender).PostBackUrl);//uncomment this if the button does not automatically redirects. This line should be the last one
}
Is there a way to set control (in template field of gridview) as target control for modal popup extender? I tried like here:
Full postback triggered by LinkButton inside GridView inside UpdatePanel
But I get an exception as value parameter can't be null. Please help in detail.
Grid:
<asp:GridView ID="grdTemp" runat="server" AllowPaging="True" AllowSorting="True"
DataSourceID="SqlDataSource1" AutoGenerateColumns="False" DataKeyNames="TempNo" OnRowCommand="grdDULead_RowCommand" OnRowDataBound="grdDULead_RowDataBound">
<FooterStyle BackColor="White" CssClass="GridFooter" />
<Columns>
<asp:TemplateField><ItemTemplate><asp:LinkButton ID="grDULeadlnkSelect" runat="server" ForeColor="Red" OnClick="grDULeadlnkSelect_Click" CausesValidation="False" CommandName="selectrow">select</asp:LinkButton></ItemTemplate>
</asp:GridView>
Extender:
<cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="grDULeadlnkSelect" PopupControlID="pnlCDAMTClar" BackgroundCssClass="modalBackground">
</cc1:ModalPopupExtender>
Code-behind:
At the start I got the error could not find control "grDULeadlnkSelect". Then I tried to register button like so but I get the error value cannot be null.
protected void grdTemp_RowDataBound(object sender, GridViewRowEventArgs e)
{ // getting err at below line.
LinkButton lb = e.Row.FindControl("grDULeadlnkSelect") as LinkButton;
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lb);
}
What you can do is just set a dummy control ID as TargetControlID for the ModalPopUpExtender and make that dummy one invisible.
Now on RowCommand event of that GridView write:
ModalPopupExtender1.Show();
Hope this will solve your problem.