Where do I find the commandname handler for asp:linkbutton - asp.net

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.

Related

ASP.NET Webforms OnClientClick without enter submission

I have the following command button:
<asp:Button Text="Insert Value" UseSubmitBehavior="false" runat="server" OnClientClick="return Confirmation.value('insert');" CommandName="Insert" />
I am using UseSubmitBehavior="false" to prevent ASP.Net page enter key causing post back
I do not want to listen to the enter keyCode via javascript because enter is used to submit non-webform elements not related to the form
Apparently, when using a Command and UseSubmitBehavior="false" then OnClientClick doesnt work. If I turn on submit behavior it works as expected but then hitting enter on the page automatically tries to click the button.
I prefer not to listen for the click event in Jquery or in javascript, and prefer a webform solution. Possible a better way of prevent enter from submitting the form or a way for OnClientClick to work properly with no submit behavior
You may need to use Panel and keep all your form inside Panel Tag and set the DefaultButton value of Panel to Button. Like Below:
<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
// other form elements
<asp:Button Text="Insert Value" ID="Button1" runat="server" OnClientClick="return Confirmation.value('insert');" />
</asp:Panel>

Postback using button in GridView

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.

ASP.NET how best to add a delete confirmation on a gridview with EntityDataSource

I have a .NET4 web application using the Entity Framework
In one of my pages I have a gridview bound to an entity data source. Within the Gridview definition I have
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
and the EntityDataSource is defined
<asp:EntityDataSource ID="eds_timesheets" runat="server" ConnectionString="name=TIME_ENTRY_DB"
DefaultContainerName="TIME_ENTRY_DB" EnableDelete="True" EnableFlattening="False"
EnableUpdate="True" EntitySetName="TIMESHEETs" Include="USER, ACTIVITY, PROJECT"
EntityTypeFilter="TIMESHEET">
</asp:EntityDataSource>
Everything works as expected, however I now want to put a confirmation of delete in place in case of accidental pressing.
I have tried placing code on the row command of the gridview wwhich would register a javascript alert window however it appears that at this point the EntityDataSource has already carried out its delete.
There is no OnClientClick for a gridview command field to place a small javascript snippet.
Has anyone encoutnered and subsequently solved this issue? Is it easier to have a link button and handle the delete of the Entity data source myself?
Use a TemplateField, and then put a Button in it with the CommandName="Delete". Then you can use the OnClientClick property to call your javascript confirmation.
Something like the following (can use Button, ImageButton, or LinkButton):
<asp:TemplateField>
<ItemTemplate>
<asp:Button id="DeleteButton" runat="server" text="Delete"
CommandName="Delete" OnClientClick="return confirm('Are you sure?');" ></asp:Button>
</ItemTemplate>
</asp:TemplateField>
Of course, you would not want to show the delete button in the CommandField.

Custom validation not working with delete buttons?

In a ListView, I have a CustomValidator set up to validate a field whenever a button with CommandName="Delete" is clicked.
<ItemTemplate>
<asp:TextBox ID="NameTextBox" Text=<%# Eval("Name") %> runat="server" />
<asp:Button ID="DeleteButton" Text="Delete" CommandName="Delete" ValidationGroup="Delete" runat="server" />
<asp:CustomValidator ValidationGroup="Delete" SetFocusOnError="true" Display="Dynamic" OnServerValidate="CustomValidator_ServerValidate" runat="server">You can't delete this.</asp:CustomValidator>
</ItemTemplate>
However, the error message is never displayed and the processing continues. What's strange is that the custom validation method is called, finds the field, and properly sets up e.IsValid to false. It does not matter whether I check Page.IsValid or not, because the error message is not displayed anyway.
It works if I remove the CommandName="Delete" from the button.
With Google I found the following solution, which seems to indicate someone has had a similar issue:
http://devio.wordpress.com/2007/12/11/formview-delete-button-and-customvalidators/
But I want to make sure that this solution is the way to go. I mean, is custom validation really not supposed to work with a delete button in a databound control, seriously?
I've alrealy heard about a problem like that, he solved it by doing it entirely differently. Like, instead of the customValidator, he puts a Label set EnableViewState="False" and Visible="False" and he check on the delete event the conditions and put the response back into the label. Maybe it can't works for you too?
But, if you realy ask "Why?????", I know he didn't find the exact reason ...

How to show insert or cancel buttons instead of links with Detailsview?

How to show insert or cancel buttons instead of links with Detailsview?
You can use the CommandField inside your <fields> tag
<Fields>
<asp:CommandField ButtonType="Button" />
</Fields>
Came across this question while looking for other information on the DetailsView. An alternative approach to that above is to change the commandfield into a template field (from the Edit Fields dialog box for the details view, select the command field entry in the list of selected fields, then click on the "Convert this field into a template field" link). Then, back in your source view, you will see something like this:
<asp:TemplateField HeaderText="Actions">
<InsertItemTemplate>
<asp:ImageButton ID="btnInsertAccept" runat="server" CausesValidation="True" CommandName="Insert" AlternateText="Insert" ImageUrl="images/16x16.check.gif" ToolTip="Click to insert this new division." ValidationGroup="ValidationInsert" />
<asp:ImageButton ID="btnInsertCancel" runat="server" CausesValidation="False" CommandName="Cancel" AlternateText="Cancel" ImageUrl="images/cancel.png" ToolTip="Click to cancel inserting this new division." />
</InsertItemTemplate>
</asp:TemplateField>
Now, there will also be ItemTemplate and EditItemTemplate entries, but you get the idea. Ive changed the default asp:LinkButton controls to ImageButtons, but you could also use normal buttons, whatever fits your UI design. The important part is the CommandName entry on each control - be it insert, Update, Delete, Cancel - it is that command name that causes the appropriate operation to take place.
Hope that give you additional help.

Resources