Issue with Postback and Custom Validators in asp.net - c#

I need some help on an issue, related with asp.net validators and postback.
The issue is that I have a page say 'p1' and it has a master page 'm1'. In addition to these I also have a usercontrol 'u1'.
Now, the issue is that on master page 'm1', I have a button that initiates a postback. And on my usercontrol 'u1', I have some fields which I need to check/validate and stop the postback if the fields are invalid.
I have tried using customvalidators and forcefully calling Page.Validate() method. But by doing this, I can see the page.IsValid property is false but sill the postback happens. I have even tried to write a return statement if the control fields are invalid but it doesn't helps..
Please note that I do not want to make any changes in the masterpage as this may have high impact on the other pages.

If clicking a button causes postbacks even when the page is invalid (as in your case it is proven with Page.IsValid property is False), then this means the button isn't configured to check for validations on the client side.
Solution 1:
You need to intialize the "CausesValidation" Property of the Button with the value True"
However, this requires changing the markup of the "Button" on the markup. And this isn't something you wish to perform.
Solution 2:
You can from within your page, access the Button of the Master page from within the Load Event and Initialize the CausesValidation property of the button to True.
Have a look at the following link to find the button from within the master page:
https://msdn.microsoft.com/en-us/library/xxwa0ff0.aspx

Related

How go I get the javascript in my user control to work inside of a panel whose visibility is false on page load?

I am working on an ASP.NET/C# project in which we are placing a user control into an AJAX panel whose visibility is set to false on page load. The visibility of this panel is set to true once the user submits some parameters. However, although the user control and all of the other panel contents are visible once these parameters are set and submitted, the uc's javascript functions do not work.
I attempted to solve this issue by adding Page.Controls.Add(controlID) in Page_Load if it is not a postback. The functionality is all there when I do this, but I know that there is a better way to get the functionality because in this case the control is being added to the page twice.
EDIT: I put the controller in a div outside of the panel and got the control that I wanted out of it. However, if anyone can explain why the javascript of the control wasn't defined or offer a solution that keeps my UC in the panel, I would really appreciate it.
If for asp server control the visible property is set to false, then that control is not even rendered on the page. Since the User Control is inside the ajax server control, the user control is not rendered. Javascript code is executed by the browser hence it must be present on the page to run.
If you can set css property display: none to hide and display: block to show for the ajax server control, then user control and its javascript will be rendered on the page, however hidden from user for css property i set to display: none, so its javascript will be executed.
To set display property of ajax server control, you could use: ajaxControl.Styles.Add("display", "none").
Hope this helps you.

Postbackurl Vs Response.Redirect (losing data)

I have a button in my aspx page, which in code behind the value of postbackurl gets set. I updated the code to add click event handler and added Response.Redirect code to redirect user to the page once the button is clicked. The only reason i ended up adding click event was because i had to add some extra logic when the button was clicked including redirecting to same url, that was used in postbackurl. The page does get redirected on click however it seems like all the hidden fields from the field gets lost once the form get redirected to the url.
Is there anyway i can submit the form without loosing the hidden data.?
Maybe one way you can solve this problem is to use the server side Transfer() call.
http://msdn.microsoft.com/en-us/library/540y83hx(v=vs.85).aspx
In most cases I've seen what you really want to do is pass the information for the new page using URL parameters. Take all the stuff the new page needs and put it into the url (encrypt if it is security sensitive).
If the only issue you are having is when you want to stay on the same page, this is simple, just have the click event handler return without doing a response.redirect or a transfer.
Post the code if this is not working for you.

How to show validation errors after a postback

I am having some textboxes and dropdownlist controls but if i select the ddl value the validation errors are disappearing and after the button click they are reappearing but i want to show the errors even after a postback how can i do this??
Could you post your code so we can perhaps see what the specific issue might be? Without seeing any code, I would say try adding this into the Page_Load function:
if (IsPostBack)
Page.Validate();
Alternatively, add this to your DropDownList or whatever controls are initiating the postback:
CausesValidation="true"
.. as per the answer here: Validators do not Validate after postback occurs
Please remove your asp.net validators if added with the controls and also remove any client side validation in Java script. Now add validation code on the page you are redirecting to. If that validation fails redirect back to the controls page with proper messages to be shown
If you dont like the default behavior - don't use validation controls and implement them by yourself using client side programming.

stop setting onclick event for asp:LinkButton if no OnClick event was explicitly defined

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.
Solutions that include the following are not helpful:
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)
Solution that would be helpful if possible:
If I could see server side at runtime whether an OnClick event was explicitly set on an asp:LinkButton tag, this would solve my problem, too. any ideas?
How about rather than dynamically switching the controls (as you mentioned is a solution you don't want), you could always use an asp:HyperLink and set the NavigateUrl property to redirect your page back to itself with a query string of some sort indicating what was clicked.
If you don't want the post to happen at all, simply leave the NavigateUrl property blank.
Of course, this will be pretty worthless if the rest of the page is dependent on ViewState and such.
http://forums.asp.net/t/1129106.aspx
This link explains how to see server side at runtime whether an OnClick event was explicitly set using reflection

c# Ajax Lazy Loading

I need to populate 4 GridViews on an aspx page, but I only bind a datatable to one of them on page load. I need to pupulate the other 3 after page load.
does anyone know the best way to do this using ajax ?
Currently I'm using javascript to __doPostBack on a button that pupulates the 3 GridViews but unfortunately this forces a full page load even when using an update panel. I need the page to load, and then populate the GridViews as the datatables are returned.
any suggestions would be much apreciated.
The way you are doing it should work ok, although using jquery to populate a div via the $("#targetDiv").load("contentUrl"); function may be a cleaner way to do it. Anyway, in order to get your current implementation working, there could be a few things you want to look at:
I assume EnablePartialRendering is true on your ScriptManager (always worth checking!).
Make sure the eventTarget for the __dopostback call is set up as an async trigger for your update panels or that it is inside the UpdatePanel if you are only using one UpdatePanel. (See here for details)
Try returning false from the javascript code that executes in the onclick event handler if you have attached this to a button, to make sure the form is not being submitted normally by your browser when you click the button.
If I understand the question properly, you want the data to load after the page is in the browser. If this is the case, then you can fire an event with JavaScript when the page loads on the client.
One method I've used is to put a hidden (with CSS, not any property) button on the page and 'clicking' it with javascript. The event of the button click event will need to be wired in the page's code. Also the button would have to be in an update panel that either contains the grids you want to be bound or has the appropriate triggers to cause them to reload.
You might look at JQuery to get manage when this code gets fired. The $(document).ready(function(){ /* Your code here... */ }); method will fire after the entire DOM is available, which is faster than waiting on the entire page to load (images and so forth).

Categories

Resources