I have an asp:button with an onclick property that posts back to the server. I would like to do some validation on the contents of a textbox before I fire the postback. I want to do the validation in javascript using some regex.I cannot use an asp:XXXXvalidator; I just can't due to what my web app does.Is there a way to call a javascript function from an asp:button that then calls the postback? I know I can use OnClientClick to call js from the asp:button, but once I call the JS how do I signal that I want to postback the button?
I have a form that occassionally submits more than once per user. I am assuming the problem is that the user gets impatient and clicks submit multiple times. I have a bunch of asp validation controls on the page. There is multiple validation groups, the submit button belongs to the "main" group. How do I validate the "main" group, then if the page is valid, disable the button and submit it? I tried adding this.disabled=true; but that just disables it and doesnt submit or anything.
I have used renderbegintag to create tags dynamically,and i have created html button tag by this ,but i am not getting how to redirect page by passing querystring of id on the click of that button.My code is:
writer.AddAttribute(HtmlTextWriterAttribute.Class, "Login-btn"); writer.AddAttribute(HtmlTextWriterAttribute.Id, "imgDetails"); writer.AddAttribute(HtmlTextWriterAttribute.Onclick,"Response.Redirect('ProductDetails.aspx?ProductId='+i)"); writer.RenderBeginTag(HtmlTextWriterTag.Button); writer.Write("Details"); writer.RenderEndTag();//button close
Obviously I am a total noob and this is simple to some of you, but I can not figure out why the rest of the sub works, but the button1.Text="Uploading, Please Wait..." seems to be completely ignored.
The button is supposed to change text when clicked but no method I have tried works with my page.
I'm currently using Validation controls to validate a form. I tried adding a separate onclientclick javascript method to do some extra validation but it now appears to only run this and not continue on to run through the Validation controls. The extra javascript method is returning true, so I'm not sure if the onclientclick is clobbering the scripts from the Validation controls. Can anyone explain what I've done wrong?
I have an aspx page with two buttons, one of the buttons has an OnClick attribute to a function that should be run when clicked. When the other button is clicked there is an if statement checking if the page is a postback, if it is then I run some statements that need to be run when that button is clicked. That postback button works fine. However, the other button, when it's clicked the function it's supposed to call never executes, and the statements inside if (Page.IsPostBack) get executed instead. What can I do to fix this? Is there a way to make the button that calls a function not do a Post back?
Void button has a confirm box after receiving true, expecting running another button's click event however, Void2_Button_Click not run, where is wrong?
protected void Void2_Button_Click(object sender, EventArgs e) { // do something } Void_Button.Attributes.Add("onclick", "var agree=confirm('Confirm to void?'); if (agree){document.getElementById('Void2_Button').click();}");
Normally I use this control on it's own page and the button works. This time round however, I am loading this control into a Div that is present on the home page of my site (that way I can show the contents with a little bit of JQuery). However, when I bring the control in this way, the onClick event doesn't fire and I am not sure what could cause that.
I don't have any code sample but the nature of the site makes it difficult to provide any that would make sense.
In short, what would stop this event firing now?
p.s I have tried adding validation groups to all other buttons and validation controls on the page and there is only ONE form present on the page.
EDIT: I have only just added the validation stuff in to see if that does anything. By default it has been like this and still didn't work:
As mentioned as well, this works when I use this control on it's own page (loaded directly into Default.aspx) so I don't think the case of onClick matters.
EDIT2: I have just noticed that when I click this button, other validation controls on my page are being triggered even though they have their own DIFFERENT validation group?!
I would like to add an Onclick event to one button (ShipEdit is the name of the button), I would like to redirect user by clicking this button to new page that is (../Account/MyAccount.aspx) and send 2 variables that is (LineItem.LineKey and shipment.ShipmentKey) with the URL: I need to write a code in this module:
Have a form with a button and some text fields on it.When I hit enter after filling a field the event for the button fires up event onClick.How can I get around this since on one of the fields I have a textchangeEvent I to fire up.
I have the following code and no matter what I do onclick event for button is not firing. I tried deleting the button, the pages. Redid the code. Moved the web project to a different computer and nothing. It worked a few hours ago.
i got this requirement to do using vb.net. there is a page on server which is html with borders,text and tables. i have to convert it to pdf onclick of button. this is not asp.net but vb.net.
I am having trouble with two buttons on a page. I dragged them on to the page and then double- clicked each of them in order to create the OnClick events.
I added my code to the events, but when I click the buttons, their respective events aren't being called.
I'm using an AjaxToolKit Accordion Control and when I put a button in, the "onclick" event is not raising. When I use a dropDownList, if I select "autoPostBack", the event raises normaly (instead, nothing occours). But with buttons I cannot define the "autoPostBack" (its implicit?).
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 } }
The above is the code generated for asp server button button control on browser. Now my query is that irrespective of ValidateValue() returning true/false __doPostBack('ApplyBtn','') function is not showing any effect for me. My understanding is that string passed to onclick acts like function body, and return will from first function will return control preventing second function from execution. Is that correct?