Master Page button triggers validation controls - asp.net

There are several linkbutton controls in my masterpage, I bind click event to redirect to other pages, it works. But if my content page has Validator, it doesn't works. When I click the linkbutton, the validator will stop the event, what should I do to resolve it? Thanks.
Page.aspx
<asp:LinkButton ID="lnkbtnHomePage" runat="server" Font-Underline="true" ForeColor="White" OnClick="lnkbtnHomePage_Click">HomePage</asp:LinkButton>
Page.aspx.cs
protected void lnkbtnHomePage_Click(object sender, EventArgs e)
{
Response.Redirect("Index.aspx");
}

There are two options which you can use for this.
The first is to add the attribute CausesValidation="false" to the buttons which you do not want to trigger any validation on the page.
The second option is to add the ValidationGroup="[groupname]" attribute to the controls and buttons which you want to trigger the validation.
The first option is good if you have a one or two buttons on the page which shouldn't trigger the validation, however the second options gives you a safeguard against you adding a new controls which could interfere with your validation.
The second option is also very handy when you have different sections of a page which have their own validation groups and shouldn't effect others. For example if you have some standard controls on your master page which are shown throughout your site, but then have individual pages with their own forms. You can easily control their behaviour.
However, as Yassine Houssni has mentioned, you would be better off just using standard <a> tags or even <asp:HyperLink> controls for simple behaviour as changing pages. If your link suddenly changes, then you will need to recompile the project rather than being able to update the link within the .aspx/master page.

hope this help http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.causesvalidation.aspx
u can pass validation for link button
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.causesvalidation.aspx

Related

How to prevent validation for an event handler of a user control?

I have a user control with a delete button. When a button is clicked, an event fires which deletes a record from the database. Now, the control is placed in Default.aspx. The whole body markup of Default.aspx (including the user control with its button itself) resides in <form runat="server"> as required by ASP.NET. Everything works so far.
However, the problem is when I put some validation controls inside Default.aspx (meaning inside <form runat="server"> because otherwise the page will report server errors). When validation controls are added, the delete button in the user control stops working. Clicking on this button no longer triggers the event as before.
Now, I disabled event validation in Default.aspx using EnableEventValidation="false" directive. I am also including UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None; in the code behind file. However, none of this helps.
How do I fix this problem and make the button clickable?
Update:
I know for sure that the validation controls are causing the problem, because I only need to add EnableClientScript="False" to each of them, and the button becomes clickable. But I don't want to rewrite validation on the client side manually!
It turns out the PostBack cannot occur if the form is not IsValid regardless of what element causes it. As long as that element (button) is inside the form with runat="server"that is invalid, the posback will not happen.
A very simple workaround is to just make the Button in my user control bypass validation: CausesValidation="False" (thanks to this question).
Another solution and maybe a more efficient one, is to use ValidationGroup. This way, all TextBoxes together with the Submit Button will belong to one group, and those controls that do not belong to that group will NOT be validated. In fact, they might have their own ValidationGroup; this will avoid interference between different controls within one Web Form.

UpdatePanel error when dynamically adding User controls into a page

Allright so I have a slight issue when I want to load some basic usercontrols which contain an UpdatePanel inside to another page which is an usercontrol.
The set up:
Whenever an user clicks on a button a pop-up shows up containing some basic info on a certain person and a tab which contains the companies he worked for. The amount of companies he/she works for can range from 1 to 4~, so I do a query then for each company I get I add a view to a multiview, this view contains multiple simple user controls (Textfields inside an updatepanel). Now whenever I go to the page I get this error:
Cannot unregister UpdatePanel with ID 'UpdatePanel2' since it was not registered with the ScriptManager. This might occur if the UpdatePanel was removed from the control tree and later added again, which is not supported. Parameter name: updatePanel
Now I know this is a common error caused by generating UpdatePanels dynamically which aren't registered with the ScriptMaster. I can't add a PreInit event handler to the page which adds the UserControls with the UpdatePanels since it's an UserControl itself.
My question is:
How can I get rid of this nasty error in a not so nasty way e.g. not a hardcoded sub routine which adds the UpdatePanel to the scriptmaster
You could define the functionality you want to add inside your user controls. I have done the same in the past, adding the following, inside of my user control.
override protected void OnInit(EventArgs e) {
// your code here
}
Add a ScriptManager on your MasterPage before the UpdatePanel. And your updatePanel is a bit confusing so might change it as well. :)

Hide Checkbox on a particular page

I am working on ASP.NET application where I am reusing a user control. The user control contains a checkbox and bunch of other controls. I want to display all the controls inside the user control on all the pages but on one single page I want to hide the checkbox.
I was thinking that I can use the databind methods and see if I am on the "pagex" then hide the checkbox. Is there any other way to solve this problem?
If you have access to the code for the control you should be add a new property to the control for hiding/showing the checkbox and then just pass in the property depending upon what page you are on. You'd have to pass in the show/hide property on the load event of the page.
Do you have access to the code??
I would specify a parameter in the code behind for your ascx file. Example:
public bool HideCB = false;
THen when you put your User Control on your aspx page do this:
<uc:TestControl id="TestControl" runat="Server" HideCB="true" />
This way you can do a check on if(HideCB) to determine if you want to make it visible or not.

Hiding a servercontrol

How can i hide a control thats located on my aspx file from within the aspx.cs file?
I have a textbox and a button, and the idea is when i press the button both those controls disapear and a dynamic control appears.
Basicly a way to make it invisible would to place something in the click event of that button or is there a other way?
Or would there be a better way to sovle this without having to make the control actually "unvisible"?
Thanks.
In the event handler of your button's click event, set the desired controls Visible property to false. You could also accomplish this via jquery without a postback, i.e. OnClientClick.
EDIT
Ok, the css approach below will mean that your control still renders but will be made invisible by the browser but will still be there in the mark up. If you don't want to it render at all set the visible to false as other guys here have mentioned.
It's one of those where there are many ways to skin this particular cat and it really depends what you want to achieve :)
Original Post
You can change it's cssClass to one that contains display:none;
<style type="text/css">
.ShowMyControl
{
display:block;
}
.HideMyControl
{
display:none;
}
</style>
Then in your code behind you can flip the styles on the button_click...
MyControl.cssClass = "ShowMyControl";
Or
MyControl.cssClass = "HideMyControl";
What you may want to do though is place your controls in asp:Panel controls and then flip the cssClass of the Panel instead of on each control. That'll work too.
This is one way to do it based on the server side model you appear to be using, however, you may want to look at utilising AJAX calls & JavaScript to do the dynamic showing & hiding on the client side. This may not be appropriate though - it depends on your design pattern really
You can subscribe to a server-side Click event of the button and set the Visible property of your user control to false:
protected void cmdHide_Click(object sender, EventArgs e)
{
myCustomControl.Visible = false;
}
However this will cause in your custom control not to be rendered at all. Furthermore the page will be reloaded since you're using server-side event.
If you don't want the page to be reloaded then you can hide it using Javascript:
Markup:
<asp:Button ID="cmdHide" Text="Hide" runat="server" />
<asp:Panel ID="myPanel" runat="server">
<cust:MyCustomControl ... />
</asp:Panel>
Code-behind:
cmdHide.OnClientClick = string.Format("document.getElementById('{0}').style.display = 'none'; return false;", myPanel.ClientID);
-- Pavel

Find out when a <button> button caused a postback in ASP.NET?

What's a simple good way to find out if a html button (the newer not input) caused the postback?
If the button is a HtmlButton control then you can just handle its ServerClick event:
<button id="MyButton" runat="server" onserverclick="MyButton_Click">Foo</button>
And then in your code-behind:
protected void MyButton_Click(object sender, EventArgs e)
{
// if you reach here it means that MyButton was clicked
}
Easiest way to do it is to add an OnClick event and just add sample code in the code behind. Then I usually add a breakpoint in the code.
I'm assuming you want to know how server-side ASP.NET code can determine if a given HTML button was pressed to cause the post back?
I can't think of any excellent solutions. Two sub-par solutions below:
1.) Add a client-side onclick to the button and have it modify a form element that the server has access to (i.e. hdnButtonClicked). The page_load will check that element and the value will allow it to determine if the HTML button was clicked.
2.) Have the button trigger a server-side method. Easiest way is to convert the button to an ASP.NET control and wire it up with an event handler but I'm sure you could work up some JavaScript to trigger a server-side method.
Unfortunately, I don't think information pertaining to how an HTML form was submitted is available on post back without some form of client side preparation. ASP.NET does this behind the scenes.

Resources