Postback using button in GridView - asp.net

I have the following in an ASP.NET GridView:
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ibtnDown" runat="server" ToolTip="Down" CommandName="Down"
ImageUrl="~/images/arrow.png" Width="16px" Height="16px"
CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>
</asp:TemplateField>
It's simply an image button that when clicked raises the RowCommand event for the GridView. It is very convenient in that it allows me to use the CommandName and CommandArgument in order to determine the button that was clicked and for which row it was clicked.
However, I find that the .NET ImageButton is limited in that it can't display CSS sprites in order to change the image on mouseover. At least I couldn't get it to work, but of course that doesn't preclude some manner in which it's possible.
Anyway, I'd like to use a standard HTML button tag because I know how to get it to work with my sprites.
<button name="btnDown" id="btnDown" type="submit" class="downArrow"></button>
But what I don't know how to do is get the button to cause a postback and raise the RowCommand event and still allow me to somehow access the CommandArgument and CommandName parameters.

Right I would make the button a server control first, then use its various properties as an alternative to commandName and CommandArgument. Additionally I would use OnServerClick event in place of rowCommand. I would use at last the sender object parse it to an hmt button and extract the values out of its properties. I think I'vent got lost.
<button runat="server" name="Down" id="btnDown" title="<%# Container.DataItemIndex %>" type="submit" class="downArrow" OnServerClick="ClickMe"></button

Have you considered using a button field to perform the action you wish to do?
Take a look at the following link
ButtonFields within a GridView

Just use (original post here):
<asp:LinkButton ID="LinkButton1" runat="server">
<div class="sprite-box_mac_osx_disc_button" />
</asp:LinkButton>
Of course use your own css with all width, height, background and background-position. And add Command and CommandArgument to LinkButton.

Related

Where do I find the commandname handler for asp:linkbutton

Trying to debug a aspx script. Don't know any asp.net.
I've come across this piece of code:
<asp:LinkButton
ID="EditButton"
runat="server"
CausesValidation="False"
CommandName="Edit"
Text="Edit"
CssClass="LightBlue">
</asp:LinkButton>
From this site:
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.command%28v=vs.110%29.aspx
I see that the method to look for is LinkButton_Command. I have done a search in all the files and not found this method.
This link is doing something, so it's not ignored. Could this method have a different name and if so how do I find what it is?
I have changed the Text attribute and that change is there when I run it, so I have the right piece of code.
I've also changed the CommandName attribute and the link stops working, so something somewhere is handling it.
There are two more linkbuttons in the code immediately after it:
<asp:LinkButton
ID="DeleteButton"
runat="server"
CausesValidation="False"
CommandName="Delete"
Text="Delete"
CssClass="LightBlue"
OnClick="DeleteButton_Click">
</asp:LinkButton>
<asp:LinkButton
ID="NewButton"
runat="server"
CausesValidation="False"
CommandName="New"
Text="New"
CssClass="LightBlue">
</asp:LinkButton>
I notice that the deletebutton has an onclick attribute, and I can find that method within the same script, but nothing obvious near that handler for the other two.
EDIT:
Another way of getting the answer that I want might be to ask the question:
What are all the different ways to add a click handler to a asp:linkbutton?
EDIT:
Don't know if it helps, but the link button is inside the following structure:
<ajax:UpdatePanel...>
<ContentTemplate>
<asp:FormView
id="FormView1"
runat="server"
OnItemDeleted="FormView1_ItemDeleted"
DataKeyNames="Id"
OnDataBound="FormView1_DataBound"
OnItemUpdated="FormView1_ItemUpdated"
OnItemUpdating="FormView1_ItemUpdating"
OnItemInserted="FormView1_ItemInserted"
OnItemInserting="FormView1_ItemInserting"
DefaultMode="Insert"
DataSourceID="odsLogEntryForm"
>
<ItemTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="EditButton"...
The difference with the example from the site you posted is that they used the OnCommand="LinkButton_Command" to subscribe the handler to the event. In your example this is not the case. This can mean 2 things:
- The event is not handled
- The event is subscribed to in code, probably in the code behind file of the aspx/ascx file (.aspx.cs). Look for the following code:
EditButton.Command += <name of the function to handle the command event>;
EditButton is the name of your LinkButton.
Command is the name of the event to handle.
<name of the function to handle the command event> is a function which the programmer created. He/She could have named it anything.
After seeing your edit:
The event is handled by the encapsulating FormView. A FormView will handle command events of a inner buttons. Since the CommandName of the LinkButtons is set to "Delete" and "New" is will be handled by the corresponding events in the FormView.
see: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview_events(v=vs.110).aspx for the available events of a FormVIew in this case the events with the name "Item" apply.

Form button navigation

Is there a good reason why I can't do this
<asp:Button runat="server" ID="Btn1" OnClick='<%# Response.Redirect("wibble.aspx?ID=" + Eval("ID")) %>'/>
I get a compilation error:
The best overloaded method match for 'System.Convert.ToString(object, System.IFormatProvider)' has some invalid arguments
I appreciate I can create a code behind routine to handle the redirect but I would like to be able to create these type of buttons on the fly.
I can use a <asp:HyperLink> and it works fine using the NavigateUrl property, but why can't I do this with a button?
You can use the OnClientClick attribute instead. This will allow you to add Javascript code for the onclick HTML attribute.
<asp:Button runat="server" ID="Btn1" OnClientClick="window.location ='wibble.aspx?ID=<%# Eval("ID")).ToString() %>"/>
You can not do this as this will be rendered as input[type=submit] as html in browser, and for html its not functionality to redirect when you click on button.
If you have such request of having hyperlink in button , ASP.net gives linkbutton control , which will solve your need.
<asp:linkbutton PostBackUrl="~/something.aspx?id=1" runat="server">LinkButton</asp:linkbutton>

How to remove postback on linkbutton inside gridview?

I'd like to ask on how to prevent post back when a LinkButton inside the GridView is clicked?
My current implementation is that, I have a GridView with customer details and the button link which is the ID link button, once this link button is clicked the customer details will be shown on their respective fields (i.e. textbox, etc), but I want it to look like more interactive and faster when clicking the said button by removing Post back. How can I achieve this? Thanks.
So just to confirm my answer after our comments, replace this...
<asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="linkView" CssClass="View" Text ='<%# Eval("ID")%>' runat="server"></asp:LinkButton> </ItemTemplate> </asp:TemplateField>
...with this...
<asp:TemplateField> <ItemTemplate> <%# Eval("ID")%> </ItemTemplate> </asp:TemplateField>
...where loadViaAjax is a Javascript function which populates your customer fields via AJAX or some other means. Ensure this function returns false to prevent the browser responding to the anchor click.
Please mark this as the answer if it works for you.

Displaying Asp.net AJAX Calendar Extender Two Ways

Is there a way to set up the calendar extender so that the calendar displays when the text box recieves focus AND when the when the element with the "PopupButtonID" is clicked? With my current settings it seems to be one or the other.
It's a bit of an ugly way to do it, but you can do this if you're prepared to use two extenders.
<asp:TextBox runat="server" ID="DateTextBox" />
<asp:ImageButton runat="server" ID="CalendarImageButton" ImageUrl="~/date_16x16.gif" />
<ajaxtoolkit:CalendarExtender runat="server" id="Extender1" TargetControlID="DateTextBox"/>
<ajaxtoolkit:CalendarExtender runat="server" ID="Extender2" TargetControlID="DateTextBox" PopupButtonID="CalendarImageButton" />
This way the calendar will appear whether you focus on the textbox or click the imagebutton.
Not that I'm aware of; it's one or the other. The only way I can think of is set it to use the popup control, then add a JS event handler for the textbox focus, and manually find the calendar extender, and there may be a show() method so you could manually invoke the trigger potentially. Not 100% sure. To find out do:
function textboxFocus() {
var c = $find("<%= calextenderid.ClientID %>");
//can use firebug to see if c.open method exists, or check for something else
}
Again, never done it, so not 100% sure.
HTH.
Use the CalendarExtender's show() method, referencing it by BehaviorID:
<asp:TextBox runat="server" ID="DateTextBox" />
<asp:ImageButton runat="server" ImageUrl="~/date_16x16.gif" OnClientClick="$find('Extender1').show();return false;" />
<ajaxtoolkit:CalendarExtenderrunat="server" id="Extender1" BehaviorID="Extender1" TargetControlID="DateTextBox"/>

UpdatePanel with GridView with LinkButton with Image Causes Full Postback

So this might be a fairly specific issue but I figured I'd post it since I spent hours struggling with it before I was able to determine the cause.
<asp:GridView ID="gvAttachments" DataKeyNames="UploadedID" AutoGenerateColumns="false" OnSelectedIndexChanged="gvAttachments_SelectedIndexChanged" runat="server">
<EmptyDataTemplate>There are no attachments associated to this email template.</EmptyDataTemplate>
<Columns>
<asp:TemplateField ItemStyle-Width="100%">
<ItemTemplate>
<asp:LinkButton CommandName="Select" runat="server"><img src="/images/icons/trashcan.png" style="border: none;" /></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In the ItemTemplate of the TemplateField of the GridView I have a LinkButton with an image inside of it. Normally I do this when I have an image with some text next to it but this time, for whatever reason, I just have the image. This causes the UpdatePanel to always do a full postback.
Instead of changing the markup, you can goto web.config and specify ClientIDMode="Auto" in the pages tag.
Reason why UpdatePanel behaving like this is because the ClientIDMode is getting generated will be too long for UpdatePanel to register. So the ClientID got truncated in middle and such control will be treated like unregistered Control.
For more information read the following:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx
Change the LinkButton to be an ImageButton and the problem is solved.
<asp:ImageButton ImageUrl="/images/icons/trashcan.png" Style="border: none;" CommandName="Select" runat="server" />
Above solutions also work, But There is one more thing to check. Check form tag for your page. If id attribute is missing, you will get same issue.
If you form tag is as given below (without id), you will get issue:
<form runat="server">
<!-- your page markup -->
</form>
Please add id, as given below:
<form id="form1" runat="server">
<!-- your page markup -->
</form>
You will not need to update ClientIDMode in web.config or page or control.
You will not need to change your linkbutton in markup.
You will not need to register control for asynch postback from code behind.

Resources