C# - LinkButton In UserControl Posts Back But Does Not Fire OnClick?
Aug 18, 2010
I've been looking at google for the past few hours, trying to find an issue to a apparently simple problem. I have a UserControl named RolloverLink that basically contains a asp:LinkButton and a <img /> . I tried setting the OnClick handler like this:
[code]....
I put a breakpoint in the propagate method, but it doesnt stop there. The page does post back (it flashes), but the event doesn't get called.
As you can see, nothing special; just colors and beautiful things.
I click on the blue Edit button and it fires the OnClick postback just fine.... in Chrome! But if I do the same in IE8, it just does nothing; doesn't even detect a click.
I removed the tag and kept just the word "Edit", and it works just fine in IE8 as a simple underlined link; the postback fires.
So, Why can't IE8 accept anything within LinkButton?
Here's an odd one that I've never seen before. Only on IIS, not in Cassini but in all web browsers I have a button that posts back and the page_load event fires, but the actual button click event doesn't fire. there is no re-writing of the page or anything else happening.
How do I setup a default setting so that if I do not set an OnClick (i.e the asp.net OnClick attribute) explicitly for an asp:LinkButton tag, it will not render an onclick(html attribute for javascript) attribute client side? By default, asp.net adds an onclick='doPostBack....' for the LinkButton.
Case for use:
There is a LinkButton tag on the page. For this page, if the user has one friend, I only want to run client side code if the button is clicked and would not for any reason want to make a post back. If the user has more than one friend I would want a click to trigger a postback.
Using any asp.net Ajaxtoolkit
Dynamically switching the control type (i.e. if friends == 1 use a asp:Hyperlink)
-I want to avoid this because it is not scalable. There might be many cases where I want an asp:Link tag to do a postback or to not do a postback depending on the user context or user attributes Using OnClientClick (I am using jQuery would like to avoid this)
Does a response.Redirect first postback to the page that's calling the redirect before actually redirecting? I don't think so but my debugging is showing me that it is.
I can see it doing so and that causes me a problem. The problem is this:
1) My page has an asp.net button on it 2) When the button is clicked, the page posts back of course. Then the following is called in the event handler method for that button:
Response.Redirect("Checkout?frpp=1"), true);
3) But I notice that when the redirect is called, it first goes back and hits the page load for this page, the page that's doing the redirecting. Well that is causing me problems because it's calling code that I don't want it to be calling again such as this:
if (!IsPostBackl) ShowOrderItems();
so it's like I'm doing a 2nd postback:
first postback happens when you click the button, hits the event handler, runs my handler method which calls the Redirect. Redirect is called but it's doing another postback to the same page here..why?
I am using Javascript to alter the innerHTML attribute of a <td> and I need to get that info back in the form submittal. The <td> corrosponds to an <asp:TableCell> on the server-side, where the Text attribute is set to an initial value.
The user cannot enter the value in this particular field. Instead, its value is set by me (via client-side script) based on actions that the user performs. But this field is useless to me if I can't see its value on the server-side as well.
I'd like to avoid using a read-only textbox, because those are difficult to resize dynamically. Can an <asp:Label> be used as form data? Is there any way to achive this without letting the user manually enter the data? Or is there a simpler way to store a string as a variable somewhere and send it back as form-data?
The intent is that when the LinkButton is clicked, it disables itself and returns false to prevent the postback (this control is used as a trigger for an animation).
What I'm trying to accomplish is to set my dynamically created linkbutton with a onClick command so when click it will run a method in the code behind. This is my code:
I have two problems with a databound AJAX Accordion. The Accordion looks like this:
[Code]....
The code behind has this subroutine:
[Code]....
[Code]....
The first problem I'm having is that this subroutine is not fired (I have a breakpoint in it) when the LinkButton inside the accordion is clicked. The Page_Load event fires and there is no error message, javascript errors, etc, but nothing else happens.
The second problem is that I'm not sure how to go backwards with the databind. I want to be able to make use of the UpdateMethod in the ObjectDataSource because I also have textboxes inside the accordion. I will have a Save subroutine that can be called but I'm not sure what code to put inside it.
I have a LinkButton inside a GridView (via an TemplateField). No matter what I try, the LinkButton will not invoke its event handler. I have tried both traditional event handler ("OnClick")A OnRowCommand event handler at the GridView level.In both cases, I've debugged and it doesn't even catch the event handler.
MdsMenu:MenuItem is my custom WebControl. If I insert a Button control out of MdsMenu:MenuItem tag it works well but if I inserted it within MdsMenu:MenuItem tag it doesn't fire the concerned method. What's wrong with my code?
Edit: Menu.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Security.Permissions; using System.Collections; using System.ComponentModel.Design; using System.Drawing.Design; using System.Web.UI.HtmlControls; using System.IO; namespace MenuServerControl { [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)] [DefaultProperty("MenuItems")] [ParseChildren(true, "MenuItems")] [ToolboxData("<{0}:Menu runat="server"> </{0}:Menu>")] public class Menu : WebControl { #region Fields private List<MenuItem> _MenuItems; string _Direction { get { string direction = ""; switch (this.Direction) { case Directions.LeftToRight: direction = "direction:ltr;"; break; case Directions.RightToLeft: direction = "direction:rtl;"; break; default: direction = "direction:rtl;"; break; } return direction; } } string _AnimationDelay { get { if (AnimationDelay == null) AnimationDelay = 1000; return AnimationDelay.ToString().ToLower(); } } string _AnimationType { get { switch (this.AnimationType) { case AnimationType.Opacity_Height: return "animation:{opacity:'show',height:'show'}"; case AnimationType.Opacity_Width: return "animation:{opacity:'show',width:'show'}"; case AnimationType.Opacity: return "animation:{opacity:'show'}"; case AnimationType.Height: return "animation:{height:'show'}"; case AnimationType.Width: return "animation:{width:'show'}"; case AnimationType.Height_Toggle: return "animation: {height: 'toggle'}"; case AnimationType.Width_Toggle: return "animation: {width: 'toggle'}"; default: return "animation:{opacity:'show',height:'show'}"; } } } string _AnimationSpeed { get { switch (this.AnimationSpeed) { case AnimationSpeed.Fast: return "speed:'fast'"; case AnimationSpeed.Normal: return "speed:'normal'"; case AnimationSpeed.Slow: return "speed:'slow'"; default: return "speed:'fast'"; } } } string FloatStyle { get { if (Direction == Directions.RightToLeft) return "float:right;"; else return ""; } } string _Main_ul_CssClass = ""; #endregion #region Properties public string Main_ul_CssClass { get { if (string.IsNullOrEmpty(_Main_ul_CssClass)) return ""; _Main_ul_CssClass = "class=" + _Main_ul_CssClass; return _Main_ul_CssClass; } set { _Main_ul_CssClass = value.Trim(); } } public Directions Direction { get; set; } public int? AnimationDelay { get; set; } public AnimationType AnimationType { get; set; } public AnimationSpeed AnimationSpeed { get; set; } public bool DropShadow { get; set; } public bool AutoArrows { get; set; } [Category("Behavior")] [Description("The menu collection")] [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] [PersistenceMode(PersistenceMode.InnerDefaultProperty)] public List<MenuItem> MenuItems { get { if (_MenuItems == null) _MenuItems = new List<MenuItem>(); return _MenuItems; } } public VerOrHor VerticalOrHorizontal { get; set; } #endregion #region Methods public Menu() { DropShadow = true; AutoArrows = true; } public override void RenderBeginTag(HtmlTextWriter writer) { writer.Write("<div id="" + this.ID + "" style="" + _Direction + " " + FloatStyle + "">"); } public override void RenderEndTag(HtmlTextWriter writer) { writer.Write("</div>"); } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); #region Adding Script & link Tags HtmlGenericControl jquery = new HtmlGenericControl("script"); jquery.Attributes.Add("type", "text/javascript"); jquery.Attributes.Add("src", Page.ClientScript.GetWebResourceUrl(typeof(MenuServerControl.Menu), "MenuServerControl.JavaScriptFiles.jquery_1_4_3.js")); jquery.EnableViewState = false; Page.Header.Controls.Add(jquery); HtmlGenericControl hoverIntent = new HtmlGenericControl("script"); hoverIntent.Attributes.Add("type", "text/javascript"); hoverIntent.Attributes.Add("src", Page.ClientScript.GetWebResourceUrl(typeof(Menu), "MenuServerControl.JavaScriptFiles.hoverIntent.js")); hoverIntent.EnableViewState = false; Page.Header.Controls.Add(hoverIntent); HtmlGenericControl jquery_bgiframe_min = new HtmlGenericControl("script"); jquery_bgiframe_min.Attributes.Add("type", "text/javascript"); jquery_bgiframe_min.Attributes.Add("src", Page.ClientScript.GetWebResourceUrl(typeof(Menu), "MenuServerControl.JavaScriptFiles.jquery_bgiframe_min.js")); jquery_bgiframe_min.EnableViewState = false; Page.Header.Controls.Add(jquery_bgiframe_min); HtmlGenericControl superfish = new HtmlGenericControl("script"); superfish.Attributes.Add("type", "text/javascript"); superfish.Attributes.Add("src", Page.ClientScript.GetWebResourceUrl(typeof(Menu), "MenuServerControl.JavaScriptFiles.superfish.js")); superfish.EnableViewState = false; Page.Header.Controls.Add(superfish); HtmlGenericControl supersubs = new HtmlGenericControl("script"); supersubs.Attributes.Add("type", "text/javascript"); supersubs.Attributes.Add("src", Page.ClientScript.GetWebResourceUrl(typeof(Menu), "MenuServerControl.JavaScriptFiles.supersubs.js")); supersubs.EnableViewState = false; Page.Header.Controls.Add(supersubs); if (Direction == Directions.LeftToRight) { HtmlGenericControl csslink = new HtmlGenericControl("link"); csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl (typeof(Menu), "MenuServerControl.CSS.superfish.css")); csslink.ID = "NavigationMenu"; csslink.Attributes.Add("type", "text/css"); csslink.Attributes.Add("rel", "stylesheet"); csslink.EnableViewState = false; Page.Header.Controls.Add(csslink); } else { HtmlGenericControl csslink = new HtmlGenericControl("link"); csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl (typeof(Menu), "MenuServerControl.CSS.RightToLeft superfish.css")); csslink.ID = "NavigationMenu"; csslink.Attributes.Add("type", "text/css"); csslink.Attributes.Add("rel", "stylesheet"); csslink.EnableViewState = false; Page.Header.Controls.Add(csslink); } if (this.VerticalOrHorizontal == VerOrHor.Vertical && this.Direction == Directions.RightToLeft) { HtmlGenericControl csslink01 = new HtmlGenericControl("link"); csslink01.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl (typeof(Menu), "MenuServerControl.CSS.RightToLeft superfish-vertical.css")); csslink01.Attributes.Add("type", "text/css"); csslink01.Attributes.Add("rel", "stylesheet"); csslink01.EnableViewState = false; Page.Header.Controls.Add(csslink01); } else if (this.VerticalOrHorizontal == VerOrHor.Vertical) { HtmlGenericControl csslink01 = new HtmlGenericControl("link"); csslink01.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl (typeof(Menu), "MenuServerControl.CSS.superfish-vertical.css")); csslink01.Attributes.Add("type", "text/css"); csslink01.Attributes.Add("rel", "stylesheet"); csslink01.EnableViewState = false; Page.Header.Controls.Add(csslink01); } #endregion } protected override void RenderContents(HtmlTextWriter output) { output.Write(CreateMenuHtmlTags()); } StringBuilder CreateMenuHtmlTags() { if (this._MenuItems == null) throw new Exception("تگ های مربوط به منو را کامل کنید"); StringBuilder Html = new StringBuilder(""); #region Add <Script> if (String.IsNullOrEmpty(Main_ul_CssClass)) Html.Append("<script>$(document).ready(function() { $("ul.sf-menu").superfish({pathLevels: 1,"); else Html.Append("<script>$(document).ready(function() { $("ul." + Main_ul_CssClass + "").superfish({ pathLevels: 1,"); Html.Append("delay:" + _AnimationDelay + ","); Html.Append(_AnimationType + ","); Html.Append(_AnimationSpeed + ","); Html.Append("dropShadows: " + DropShadow.ToString().ToLower() + ","); Html.Append(@"autoArrows: " + AutoArrows.ToString().ToLower() + "});});</script>"); #endregion if (string.IsNullOrEmpty(Main_ul_CssClass) && VerticalOrHorizontal == VerOrHor.Vertical) Html.Append("<ul class="sf-menu sf-vertical sf-js-enabled sf-shadow" id='sample-menu-1'>"); else if (String.IsNullOrEmpty(Main_ul_CssClass)) Html.Append("<ul class="sf-menu sf-js-enabled sf-shadow" id='sample-menu-1'>"); else Html.Append("<ul class="" + Main_ul_CssClass + "" id='sample-menu-1'>"); foreach (MenuItem item in _MenuItems) { if (item == null) continue; if (item.SubMenuItems != null && item.SubMenuItems.Count > 0) { Html.Append("<li" + item.li_CssClass + ">"); Html.Append("<a href="" + item.Href + "">" + item.Text.Trim() + "</a>"); ParseSubMenuItems(ref Html, item); Html.Append("</li>"); } else if (item.SubMenuItems != null) Html.Append("<li" + item.li_CssClass + "><a href="" + item.Href + "">" + item.Text.Trim() + "</a></li>"); } Html.Append("</ul>"); return Html; } void ParseSubMenuItems(ref StringBuilder Html, MenuItem menuItems) { if (menuItems == null) return; Html.Append("<ul " + menuItems.ul_CssClass + " style="display: none; visibility: hidden;">"); foreach (var item in menuItems.SubMenuItems) { if (item == null) continue; MenuItem Sub_MenuItem = item as MenuItem; WebControl webControl = item as WebControl; if (Sub_MenuItem != null) { if (Sub_MenuItem.SubMenuItems != null && Sub_MenuItem.SubMenuItems.Count > 0) { Html.Append("<li" + Sub_MenuItem.li_CssClass + ">"); Html.Append("<a href="" + Sub_MenuItem.Href + "">" + Sub_MenuItem.Text.Trim() + "</a>"); ParseSubMenuItems(ref Html, Sub_MenuItem); Html.Append("</li>"); } else if (Sub_MenuItem.SubMenuItems != null) Html.Append("<li" + Sub_MenuItem.li_CssClass + "><a href="" + Sub_MenuItem.Href + "">" + Sub_MenuItem.Text.Trim() + "</a></li>"); } else if (webControl != null) { Html.Append("<li>"); webControl.EnableViewState = true; this.Controls.Add(webControl); webControl.EnableViewState = true; StringBuilder sb = new StringBuilder(); sb.Append("<div>"); using (StringWriter sw = new StringWriter(sb)) { using (HtmlTextWriter textWriter = new HtmlTextWriter(sw)) { webControl.RenderControl(textWriter); } } sb.Append("</div>"); Html.Append(sb.ToString()); Html.Append("</li>"); } } Html.Append("</ul>"); } #endregion } } MenuItem.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections; namespace MenuServerControl { [DefaultProperty("SubMenuItems")] [ParseChildren(true, "SubMenuItems")] [TypeConverter(typeof(ExpandableObjectConverter))] public class MenuItem : INamingContainer { #region Fields ArrayList _SubMenuItems; string text = ""; string href = "#"; string _ul_CssClass = ""; string _li_CssClass = ""; #endregion #region Properties [Description("متن منو آیتم")] [DefaultValue("")] [NotifyParentProperty(true)] public string Text { get { return text; } set { text = value; } } [DefaultValue("#")] [Description("<a /> href attribute")] [NotifyParentProperty(true)] public string Href { get { return href; } set { href = value; } } [DefaultValue("")] [Description("<ul /> css class")] [NotifyParentProperty(true)] public string ul_CssClass { get { if (string.IsNullOrEmpty(_ul_CssClass)) return ""; _ul_CssClass = " class="" + _ul_CssClass + """; return _ul_CssClass; } set { this._ul_CssClass = value.Trim(); } } [DefaultValue("")] [Description("<li /> css class")] [NotifyParentProperty(true)] public string li_CssClass { get { if (string.IsNullOrEmpty(_li_CssClass)) return ""; _li_CssClass = " class="" + _li_CssClass + """; return _li_CssClass; } set { this._li_CssClass = value.Trim(); } } [Category("Behavior")] [Description("The MenuItems collection")] [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] [PersistenceMode(PersistenceMode.InnerDefaultProperty)] public ArrayList SubMenuItems { get { if (_SubMenuItems == null) _SubMenuItems = new ArrayList(); return _SubMenuItems; } } #endregion } }
So, I have a MasterPage, in which I have an ImageButton and an UpdatePanel. Inside the UpdatePanel I have a LinkButton.
[Code]....
As you can see, both components are asociated to the same OnClick event, which should open said PDF document in a new window using javascript (Code Behind of the MasterPage):
[Code]....
I build the path using the toolTip of the component that has been clicked,which has the name of the file that should load in the
new window.
When I run the application, and click the ImageButton, the new window opens and the file loads correctly. However, when I click the LinkButton, nothing happens (it should do exactly the same as the ImageButton).
If I set a breakpoint, and debug, when I click the LinkButton, it goes to the LnkRelease1 event (as it should), everything looks fine, but it doesnt open the new window, It doesn't execute the javascript window.open.
I am using the following code to load user control on to ASPX Page, which under a Master Page:
[Code]....
The user control name is passed as a parameter on the URL.
Inside the user, there is a LinkButton. The link button has a simple server side code for OnClick event.
Everything is working fine on the production machine, but, on the Development machine, I can see incorrect behaviour. I have started making some development changes on the Development Machine since several months back, and it will be very difficult to find out what caused the problem.
When I click the LinkButton inside the user control, the user control is removed from the parent page, and the OnClick event of the LinkButton is not firing.
I did debug, and found out that after I click on the LinkButton inside the user control, then the Page Load events for the master page and the ASPX page are working fine, and the IsPostBack is set to true, but the OnClick event of the LinkButton is not firing.
I have a usercontrol that has a gridview. I have a few boundcolumns and then a buttonfield. The buttonfield needs to have a confirm message box and then proceed to the RowCommand event. In the RowCommand, I'm trapping for a CommandName called "DeleteFile".
The weird thing is everything works correctly with deleting a row if the onclick code is not registered. I'm using attributes.add for this.
I know there is another option for using a templatefield and then a button rather than a buttonfield. But, I've noticed that this approach causes a postback that effects another usercontrol on my page. It thinks I've submitted it which can either cause it to error out or potentially sumbit more information which I want to avoid.
Basically I am using a ModalPopUpExtender and it works fine, except that the OK button has an OnClick event that only fires when it is clicked for the 2nd time.. obviously i can progress with populating my OnClick event method for later on - its just annoying that a user has to click my button then ok in the pop up twice before they can get their intended result!
Very new to ASP.NET (using C# behind) and am having trouble figuring out the following scenario. I have a DataList that is populated (code below) and wanted each cell to be clickable. When the cell is clicked I wanted to know the unique ID for that cell. In my code behind, my OnClick method looks like this:
[Code]....
However I never get there. It seems as if the linkbutton always refers to some javascript callback function and just reloads my form. Am I using the wrong control for what I want to do? From this click I want to look up more information from the Key (will roll up the datalist or set visible to false, then supply the new data on the same page)DataList:
I have a listview with a linkbutton in it, whenever the user clicks on the link button, I want to retreive the value under:
[Code]....
In my code behind I have
[Code]....
if I change txtstartdate.text = "Testing..", it would work fine with no problem, so everything is working except just finding the value of the username
I have a linkbutton whose onclick method does not fire after a jquery drag and drop, the onlclientclick does work. Y would the server side code not work only after a jquery drag and drop?