Linkbutton OnCommand not firing after upgrading to .Net 4 - c#

I have a webforms project that was written in ASP.Net 3.5 and works fine. I decided to upgrade it to 4.0 for various reasons. The upgrade went fine. However, on one particular page, I have certain linkbuttons that fire an event in code behind. The event never fires on click. Other controls on the page, like an link button with client side code and Telerik radgrid work fine.
This Works
<asp:LinkButton ID="InsertLinkButton" runat="server">
<asp:Image ID="ImageButton1" runat="server" />Add New Employee</asp:LinkButton>
//Code behind adding javascript event
InsertLinkButton.Attributes["onclick"] = String.Format("return ShowInsertForm();");
This Doesn't
<asp:LinkButton ID="ALinkButton" runat="server" CommandName="A" OnCommand="LinkButton_Command" ">A</asp:LinkButton>
The page is a content page with a master page. Any ideas?

I'm not sure if this is just a typo on your post but your markup isn't correct on the LinkButton.
It is currently:
<asp:LinkButton ID="ALinkButton" runat="server"
CommandName="A" OnCommand="LinkButton_Command" ">A</asp:LinkButton>
It should be:
<asp:LinkButton ID="ALinkButton" runat="server"
CommandName="A" OnCommand="LinkButton_Command">A</asp:LinkButton>

The panel containing the controls was referenced as an updated control in a Telerik RadAjaxManager buried in the mark up. If I removed the RadAjaxManager, it worked. Apparently the linkbutton command event didn't fire the ajax callback. Since this worked in 3.5, I'm wondering if some metadata didn't get converted properly during the 3.5>4 upgrade?
Anyway, I updated the references of what was supposed to fire an ajax call from the RadAjaxManager and it now works. Thanks for the help!

Related

How to prevent page reload on asp.net LinkButton OnClick function

I'm using the <asp:LinkButton />’s OnClick function on the server side to fetch data. I'm using OnClientClick to open a popup and populate data. But the page refreshes when I click the button. Please see the attached code and help me to fix the issue.
<asp:LinkButton ID="lnkEdit_Bill" runat="server" CssClass="lnkAddressButton" OnClientClick="javascript:ShowDialog_Both_New('Invoice','edit');" OnClick="lnkEdit_Bill_Click_new" >Edit</asp:LinkButton>
Google event.preventDefault(), I believe i've used that previously to prevent a postback. Also remove the OnClick, if that's not what you want, and just use OnClientClick.
Bro, if you don't want the postback, just fire the javascript. and you definitely don't need the server control. which you can present your code as below:
Edit
Let's says if you still insist want to user ASP.NET server control of LinkButton, then you can do something like this:
<asp:LinkButton ID="lnkEdit_Bill" runat="server" CssClass="lnkAddressButton"
OnClientClick="ShowDialog_Both_New('Invoice','edit'); return false;" >
Edit
</asp:LinkButton>

Click on LinkButton in Gridview: why is the Grid reloaded most of the times but not always?

I´m trying to understand the annoying refreshment of the GridView/Repeater that occurs most of the times I click on a LinkButton within that element. If this is caused by the Postback, then why doesn't it happen all the times?
And if it is not caused always, do I might even get rid of it?
I usually do this to get detailed information on a cell clicked in a Grid or repeater:
<asp:LinkButton ID="ButtonSelect" runat="server" CommandName ="Select" CommandArgument = '<%# Eval("date") %>' Text='<%# Bind("TAG") %>' OnClick="GetDetails"/>
The data processed in the "GetDetails" method will then be displayed in some other element. There wouldn't be any need to refresh the Grid.
Is this the normal behavior for any LinkButton click in a GridView?
Martin
It's the normal action of a server button within a gridview. It will cause a postback. A postback will force the Page LifeCycle. The entire page will be recreated and Databinding may or may not occur depending on your cache options and programming.
"Fixing" that really depends what you are trying to accomplish. If it's simply to stop the "screen flicker" due to the postback, consider using AJAX calls or <asp:UpdatePanel> server controls.
If you are trying to work with JS on the clientside and just wish to defer the postback until later, convert the button to a template field and replace the <asp:LinkButton> with a basic html control <a href="javascript:void();" ...>, <button type='button'>, <input type='button' ..., etc...

Asp .Net Web Forms removing postback from asp:linkbutton with inner HTML tags

I have created my first fully functional listview with custom action buttons, that worked fine when I used asp:buttons, but I need the buttons to be icons, so I tried inserting HTML tags inside, since it can't be done out of the box, I used as:linkbuttons controls to achieve the desired look.
What it does is that the buttons trigger several actions that populate controls inside update panels, and I have my own Javascript that shows a popup on click, but on the second action it currently sends a postback and page is reloaded, I googled other questions and tried everything they had, what I tried:
Adding a return false on client click
<asp:LinkButton runat="server" OnClientClick="showDialog('#popup'); return false;" CommandName="Detalles" CssClass="toolbar-button" ID="EditButton">
Editar
<span class="mif-pencil"></span>
</asp:LinkButton>
Using an HTML button with runat server, but Command doesn't trigger
<button runat="server" onclick="showDialog('#popup');" CommandName="Detalles" class="toolbar-button" ID="EditButton">
Editar
<span class="mif-pencil"></span>
</button>
And on jQuery, but doesn't do any difference
$('.toolbar-button').removeAttr('href');
What exactly I'm missing, my code works fine with Asp:buttons, but they can't have additional HTML :(, I'm a newbie on ASP, any hints are appreciated.
Working button that doesn't allow inside HTML
<asp:Button runat="server" Text="Detalles" CssClass="toolbar-button fg-darkCyan button-popup" ID="btn_detalles" CommandName="Detalles">
</asp:Button>
NOTE Buttons are inside a ListView control, and assigned for each item.

Checkbox not entering OnCheckChanged first time

<ajaxtoolkit:AccordionPane ID="accordianPaneAroundTheCheckbox" runat="server">
<Content>
<asp:UpdatePanel ID="updatePanelAroundTheCheckbox" runat="server" >
<ContentTemplate>
<div>
<asp:CheckBox ID="chkFoo" AutoPostBack="true" runat="server" OnCheckedChanged="DoBar"/>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</Content>
</ajaxtoolkit:AccordionPane>
We have something like the above. Extra stuff has been left off as it is a large page.
I place a breakpoint at the begining of Page_Load and DoBar. The first time I click the checkbox, the breakpoint on Page_Load is hit, but DoBar is not. The second time I click the checkbox, both breakpoints are hit.
Why could this be happening? I'm not dynamically generating the checkbox. It's ClientID is the same every time (no dynamically generated or ID'd containers). I've tried resubscribing to the event in the Page_Load, but it didn't hit the first time, and just hit it subsequent times twice.
Update I have tried removing the UpdatePanel completely. Not only does the whole page postback, which I don't want, but the event is still not entered. What can block/swallow an event call like that? Is there some exception deep in the bowels of a master page or framework call or something somewhere that I can't see?
Is that the correct source code you've pasted? I'm asking because there's no event called OnCheckChanged, there is however an OnCheckedChanged event. Using your code it works just fine when the markup for the checkbox looks like this:
<asp:CheckBox ID="chkFoo" runat="server" AutoPostBack="true" OnCheckedChanged="DoBar"/>
The checkbox isn't being set by anything on the server side. The form is populated by an ajax call to a PageMethod that pulls a databag from the server and populates the controls. The problem is that the server thought it was unchecked, but javascript checked it. When I unchecked it, the postback happened, and the viewstate still thought its previous state was unchecked, and the current state is now unchecked. Thus, the CheckedChanged event doesn't fire.
A thousand grisly deaths to Viewstate. I'm close enough to fix the issue now. Thanks for trying/looking.

ScriptManager.RegisterClientScript in a UserControl within a FormView inside an Async Panel

I'm having an annoying problem registering a javascript event from inside a user control within a formview in an Async panel. I go to my formview, and press a button to switch into insert mode. This doesn't do a full page postback. Within insert mode, my user control's page_load event should then register a javascript event using ScriptManager.RegisterStartupScript:
ScriptManager.RegisterStartupScript(base.Page, this.GetType(), ("dialogJavascript" + this.ID), "alert(\"Registered\");", true);
However when I look at my HTML source, the event isn't there. Hence the alert box is never shown. This is the setup of my actual aspx file:
<igmisc:WebAsyncRefreshPanel ID="WebAsyncRefreshPanel1" runat="server">
<asp:FormView ID="FormView1" runat="server" DataSourceID="odsCurrentIncident">
<EditItemTemplate>
<uc1:SearchSEDUsers ID="SearchSEDUsers1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
Hello
<asp:Button ID="Button1" runat="server" CommandName="Edit" Text="Button" />
</ItemTemplate>
</asp:FormView>
</igmisc:WebAsyncRefreshPanel>
Does anyone have any idea what I might be missing here?
Have you tried using RegisterClientSideScript? You can always check the key for the script with IsClientSideScriptRegistered to ensure you don't register it multiple times.
I'm assuming the async panel is doing a partial page past back which doesn't trigger the mechansim to regenerate the startup scripts. Perhaps someone with a better understanding of the ASP.Net Page Life Cycle and the CLR can fill in those blanks.
For me that works fine. resizeChartMid() is a function name.
ScriptManager.RegisterStartupScript(this, typeof(string), "getchart48", "resizeChartMid();", true);
Try this, i got the same issue
ScriptManager.RegisterClientScriptBlock(MyBase.Page, Me.[GetType](),
("dialogJavascript" + this.ID), "alert(\"Registered\");", True)
This worked for me!

Categories