LinkButton Won't Trigger Command Event - c#

I know there are a lot of related posts or articles about this, but then it seems that they're not helping my case. I've even compared with a working sample at this site, http://www.ezineasp.net/post/ASP-Net-LinkButton-Command-Event.aspx, I don't think there's much difference. I thought my code should be working but apparently it just won't. I'm so sorry if this looks like a duplicate, but it's my last resort to post here.
Here's my HTML:
<asp:ListView runat="server" ID="AppsList">
<LayoutTemplate>
<div>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
</div>
</LayoutTemplate>
<ItemTemplate>
<div class="applist">
<div class="app">
<asp:ImageButton ID="imgbtnApp" runat="server" ImageUrl='<%#Eval("Icon") %>' height="100" width="100" CommandName="Select" CommandArgument='<%# Eval("ID") %>' OnCommand="AppsList_ItemCommand" />
</div>
<div class="appname">
<asp:LinkButton ID="linkbtnAppName" runat="server" CommandName="Select" ForeColor="#333333" CommandArgument='<%# Eval("ID") %>' OnCommand="AppsList_ItemCommand" CssClass="linkbtnAppName"><%# Eval("AppName") %></asp:LinkButton>
</div>
</div>
</ItemTemplate>
<EmptyDataTemplate>
Sorry - Nothing found.
</EmptyDataTemplate>
</asp:ListView>
Code:
protected void AppsList_ItemCommand(object sender, CommandEventArgs e)
{
if (e.CommandName == "Select")
{
txtTest.Text = e.CommandArgument.ToString();
}
}
What I'm trying to achieve here is to capture the ID of the item in the ListView into the textbox when I click on either the Image Button or Link Button. Both will perform the same thing. I already got the Image button to work. When I click the Image, the ID, e.g 1 will appear in the textbox. But when I want to do the same thing with the Link Button, nothing will happen. The event is not being triggered in any way.
I've seen some posts talking about repeaters or AJAX to do the same thing, but I was just wondering why can't this code work. I would appreciate any pointer.

For my own experience, I have wrongly set ViewStateMode="Disabled" in page directive.
Because of this when post back occur upon clicking LinkButton inside the List View, existing data are vanished. So, will not reach to LinkButton's OnCommand event and never fire.
Once ViewStateMode attribute is removed, everything working well.

I noticed that your eventArgs should ListViewCommandEventArgs instead of CommandEventArgs and you should also bind the itemCommand event to your aspx page as shown below.
ASPX:
<asp:ListView runat="server" ID="AppsList" OnItemCommand="AppsList_OnItemCommand">
Code Behind:
protected void AppsList_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
if (String.Equals(e.CommandName, "Select"))
{
}
}
UPDATE:
Also remove the OnCommand attribute from image button and Link button OnCommand="AppsList_ItemCommand" , and each CommandName should be different than other.
<div class="applist">
<div class="app">
<asp:ImageButton ID="imgbtnApp" runat="server" ImageUrl='<%#Eval("Icon") %>' height="100" width="100" CommandName="Select" CommandArgument='<%# Eval("ID") %>' />
</div>
<div class="appname">
<asp:LinkButton ID="linkbtnAppName" runat="server" CommandName="Select" ForeColor="#333333" CommandArgument='<%# Eval("ID") %>' CssClass="linkbtnAppName"><%# Eval("AppName") %></asp:LinkButton>
</div>
</div>
</ItemTemplate>

Well, in case someone is wondering why this is happening, this is what I've found out after spending a great deal of time ripping off my codes to see what's wrong with it. I doubt that someone will bump into this issue, but to heck with it, sharing is caring.
Apparently the search button I've placed in my master page is the reason behind this. I've assigned its ID as "submit", which is the thing that is stopping my linkbutton from working, which I have no idea why that is happening. The search function is merely a search engine allowing users to look for the keywords in the webpage. The linkbutton's OnCommmand event just won't trigger when the ID to the search button is assigned as "submit".
When I change it to "submit1", everything works as normal. I'm still a newbie in this asp.net thing, can anyone tell me why is this even affecting the linkbutton? Anyway, the OnCommand event is now working properly.

I had a similar issue with LinkButtons. I don't remember how I figured this out, but I must of got fustrated and started clicking the LinkButton a few times fairly quickly and I noticed that the Command event did fire once in awhile. Soon I figured it's if I clicked on the LinkButton before the page fully loads that the command would fire correctly. If the page fully loaded then it seems something was hijacking that command on the LinkButton.
Without success, from one of the other suggestions I searched for any button that might be using the name/id "submit". There wasn't one on my page.
With success, I tried using ASP.Net Buttons instead of LinkButtons. That seemed to fix my issue. I'm not entirely sure what caused this situation but Buttons seem to work in my case so I'll stick with that.

Related

RadAjaxLoadingPanel does not works with DNN

I have a dnn site, which has a label and an imagebutton, clicking on which replaces the label with textbox and user can enter their text, once submitted the label will be updated with this text. Now clicking on the imagebutton causes the page to postback, i don't want a postback for this, hence i have placed telerik RadAjaxLoadingPanel control, so the cool loading div gets displayed while processing is going on, but for some reason it's not working, It always throws below error:
Please, see whether wrapping the code block, generating the exception, within RadCodeBlock resolves the error.
Below is the markup of my page: (I tried the wrapping the code with RadScriptBlock and RadCodeBlock, in both case it throws same error as above)
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default">
</telerik:RadAjaxLoadingPanel>
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" LoadingPanelID="RadAjaxLoadingPanel1">
<telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
<a class="subscribetoday" href="#">
<strong>Subscribe Today!</strong> <asp:Label ID="lblsubscribemsg" runat="server" Text="12 issues for $14.95"></asp:Label>
<asp:ImageButton ID="imgEditSubscribe" runat="server"
OnClick="imgEditSubscribe_Click" ToolTip='Edit' ImageUrl="~/images/edit.gif" AlternateText='Edit' Visible="false" />
<div id="editsubscribe" runat="server" visible="false">
<asp:TextBox ID="txtSubscribe" runat="server"></asp:TextBox> <asp:ImageButton ID="imgSave" runat="server"
OnClick="imgSave_Click" OnClientClick="return validateSubscribeNote();" ToolTip='Save' ImageUrl="~/images/save.gif" AlternateText='Save' /> <asp:ImageButton ID="imgCancel" runat="server"
OnClick="imgCancel_Click" ToolTip='Cancel' ImageUrl="~/images/cancel.gif" AlternateText='Cancel' />
</div>
<img src="img/prosound-subscribe.png" alt="Subscribe Today!">
</a>
</telerik:RadScriptBlock>
</telerik:RadAjaxPanel>
Can anyone tell me where i am going wrong with this.
The problem is with other server code blocks on the page (<%=%> for example, generally - <% ... %>), not with this concrete piece of code you are trying to AJAX-enable. You can read more here: http://docs.telerik.com/devtools/aspnet-ajax/controls/ajax/radcodeblock-and-radscriptblock
So, you should find the place where those code blocks are used and wrap THEM in a RadCodeBlock controls. It is often scripts that reference controls, e.g.:
<telerik:RadCodeBlock runat="server" ID="RadCodeBlock1">
<script>
function getReference() {
return $find("<%=someControl.ClientID%>");
}
</script>
</telerik:RadCodeBlock>
With DNN, however, I cannot say where these may originate.
Thus, your other option is to use an <asp:UpdatePanel> control to get AJAX requests instead of full postbacks. The native AJAX toolset also offers the <asp:UpdateProgress> control that you can use instead of RadAjaxPanel.

Event in repeater not fired until page postback by another control in asp.net c#!

Why My Event as link button event or item command not fired until i post back page with another control in page.
for example in this code:
<div id="pagination">
<span class="all" runat="server" id="CurrentPage">Total Pages</span>
<asp:Repeater ID="RPTPaging" runat="server" Visible="false" OnItemDataBound="RPTPaging_ItemDataBound" OnItemCommand="RPTPaging_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="BtnPage" CssClass="inactive" CommandName="Page" CommandArgument="<%# Container.DataItem %>" runat="server" Text="<%# Container.DataItem %>"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</div>
related to this question
Why Repeater ItemCommand Doesn't fire, even if i don't rebind by post back?
i found when i click on link button in this repeater nothing happened,but after i click on empty input button on page, after post back a page changed corectlly.
Dear Expert After 2 day finally i figure out why my page doesn't post back.
after i tried to post back my button manually with java script and i failed. i tried to trace the script and i found this Error with firebug:
TypeError: theForm.submit is not a function
theForm.submit();
when i google it i found i have a button in my MasterPage with id="Submit" and as
"submit is not a function" means that you named your submit button or some other element submit. Rename the button to btnSubmit and your call will magically work.
When you name the button submit, you override the submit() function on the form.
So This Problem Resolved. Thanks to all.

.net Radgrid with button in column only fires itemcommand on 2nd click

I'm having an issue where the button within a radgrid is not firing until second time.
I have a usercontrol with a radgrid on it and button within one of its column.
The usercontrol is placed on a page.
When clicking the button on radgrid nothing happens 1st time, however it works 2nd time.
This is some of the radgrid column data
<telerik:GridTemplateColumn DataField="Quantity" HeaderText="Quantity" UniqueName="QuantityCol">
<ItemTemplate>
<asp:TextBox ID="Quantity" runat="server" Columns="4" Text='<%# DataBinder.Eval(Container.DataItem, "Quantity") %>' width="40px" />
<asp:LinkButton id='btnUpdateRow' runat="server" CausesValidation="false" Text='<span>Update</span>' CommandName="ButtonUpdateRow" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'/>
</ItemTemplate>
</telerik:GridTemplateColumn>
The radgrid uses NeedDataSource to obtain its data.
Page Load does not do anything.
During debug within itemcreated/itemdatabound, the linkbutton clientid it shows
TestBasket_RadGrid1_ctl01_ctl04_btnUpdateRow
However, when rendered to the browser it shows as
<a id="TestBasket_RadGrid1_ctl01_ctl09_btnUpdateRow" href="javascript:__doPostBack('TestBasket$RadGrid1$ctl01$ctl09$btnUpdateRow','')">
Clicking the button - itemcommand is NOT fired.
On returning from postback the brower shows
<a id="TestBasket_RadGrid1_ctl01_ctl04_btnUpdateRow" href="javascript:__doPostBack('TestBasket$RadGrid1$ctl01$ctl04$btnUpdateRow','')">
Clicking the button - itemcommand is fired.
Anyone explain why clientid gets changed.
I have tried placing a placeholder and creating the control with an id in itemcreated - still same issue.
Thanks in advance for any help.
Looks like the issue happened because of page inheritance.
I made a simple aspx page not using any of our architecture and it seemed to work fine.
I then bought the code back in and it stopped working.
I’ve managed to fix it by changing the page inheritance of the pages that use Telerik controls.

Repeater's Item command event is not firing on linkbutton click

I am having problem with my repeater's OnItemCommand event.
When I click the Link Button, its not firing.
Am I missing any environment variable
ASPX code
<table>
<!-- repResearchers begin, 0=display name, 1=url -->
<asp:Repeater ID="repExtResearchers" Runat="server" OnItemCommand="deleteResearcher">
<ItemTemplate>
<tr>
<td>
<a href="<%# ((System.String[])Container.DataItem)[1] %>">
<%# ((System.String[])Container.DataItem)[0] %></a>
</td>
<td>
<asp:LinkButton ID="lbDelete" runat="server" CommandName="del"
CommandArgument = "<%# ((System.String[])Container.DataItem)[1]%>"
OnClientClick="if (!confirm('Are you sure do you want to delelte it?')) return false;">Delete</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
CS
protected void deleteResearcher(object sender, RepeaterCommandEventArgs e)
{
string a;
lblError.Text = e.CommandArgument.ToString();
lblError.Visible = true;
}
Make sure you dont rebind the repeater at every postback.
If (Page.IsPostBack)
return;
repExtResearchers.DataSource = ...
repExtResearchers.DataBind();
Hope that helps.
I'm sure - as this is an EXTREMELY old question - that this has been answered already, but for people who may be running into what I was running into...
If you're using any of the Ajax Controls, they all require a validation group. I had a really long page that I was trying to shorten by doing this, so I wasn't noticing that the ajax controls from the Ajax Control Toolkit were throwing errors and not validating. I set the LinkButton's validation group to something that was nowhere anywhere and it started firing.
Hopefully, that helps someone out.
It won't fix your ptoblem but change
OnClientClick="if (!confirm('Are you sure do you want to delelte it?')) return false;"
to
OnClientClick="return confirm('Are you sure do you want to delelte it?')"
Your code is using a double negative to confirm a positive.
I had this issue using OnCommand in a LinkButton and I had an empty href="". When I removed the extra attribute, it posted back.

Unable to Create Event Handler for a LinkButton inside a ListView ItemTemplate

This is a weird issue. I have a List view with 2 Link buttons. "Edit" and "Delete"
Iam able to attach an event handler for the first linkbutton(Update). Code in the event handler executed fine. But If I try to attach a event handler for the second link button(Delete) , I get an error.
My Item Template Looks like this.
<ItemTemplate>
<tr>
<td>
<asp:Label ID="MessageLabel" runat="server" Text='<%# Eval("Item") %>' />
</td>
<td>
<asp:Label ID="URLLabel" runat="server" Text='<%# Eval("URL") %>' />
</td>
<td>
<asp:LinkButton ID="EditLinkButton" runat="server" OnClick="EditLinkButtonClicked"
CommandArgument='<%# Eval("ItemID") %>'> Edit</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="DeleteLinkButton" runat="server" OnClick="DeleteLinkButtonClicked"
CommandArgument='<%# Eval("ItemID") %>'>Delete</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
The Event handler declared in the codebehind file are
public void EditLinkButtonClicked(object sender, EventArgs e)
{
-----
}
public void DeleteLinkButtonClicked(object sender, EventArgs e)
{
-----
}
[Exactly same]
First Item works absolutely fine. But If I attach the second handler, I get the following error
Am I missing some thing ?
Note - There is no error if I try to attach the 1st event handler to the second link button.[ie EditLinkButtonClicked to DeleteLinkButtonClicked ]
Issue occurs only when I try to attach DeleteLinkButtonClicked to DeleteLinkButton
Any help? Thanks in Advance
Try to disable EnableEventValidation for the Page directive and check it.
<%# Page EnableEventValidation="false" %>
If it help then you should to know that:
This feature reduces the risk of
unauthorized or malicious postback
requests and callbacks. It is strongly
recommended that you do not disable
event validation.
regarding to http://msdn.microsoft.com/en-us/library/system.web.ui.page.enableeventvalidation.aspx
and try to avoid this in the way that was suggested here.
It'd be three things:
You need to clean, rebuild solution, and reset IIS or close ASP.NET development server (maybe some cache is preventing you to execute the most recent version of the code-behind class).
Typo. Double-check that...!
Event handler hasn't the right signature.
Anyway, why don't you use the "Command" event?
You can do OnCommand="Item_Command" and the event handler will have CommandEventArgs which provides you the CommandName and CommandArgument, so you can switch the command name and invoke the logic for editing or deleting respectively.
Read more here:
http://msdn.microsoft.com/es-es/library/system.web.ui.webcontrols.commandeventargs_members.aspx
The same problem occurs in my code. I resolved by using CausesValidation="false"
<asp:LinkButton ID="YourID" runat="server" **CausesValidation="false"** OnClick="Category_btn_Click1" />

Categories

Resources