Showing posts with label asp. Show all posts
Showing posts with label asp. Show all posts

Wednesday, 17 November 2010

Using javascript validation on an ASP .Net page

Whilst in normal scenarios you would use the .net validators for ASP I found several scenarios where they were got in the way, especially if I had more than one button on a form performing tasks different to the area that needed validating.

I decided to do some javascript validation like I would have done with classic ASP instead and use an alert message box rather than displaying items on the page. Turns out it was pretty easy, first change the button(or other control) to use its OnClientClick method to call a javascript function like so:
<asp:Button ID="btnPerformTask" runat="server" Text="Perform Task" 
OnClick="btnPerformTask_Click" OnClientClick="return validateMe();" />

Then create the javascript function to perform the validation (here I have done it against a textbox with an id of txtInputToCheck):
function validateMe() {

if (document.getElementById('<%=txtInputToCheck.ClientID%>').value.length == 0) {
alert("Text has not been entered.");
return false;
}

return true;

}

I needed to use the txtInputToCheck.ClientID as the ID that appears on the page when rendered is not the same as you define in the HTML of your page. Then I simply check the value length to see if its been entered and return false if so.

Returning false stops the page from posting and returning true allows it to do so, return true is put at the end so if all validation is ok I am letting the page continue.

Tuesday, 16 November 2010

User Control Cast Error

Came across an error I haven't had before using ASP .Net so thought I would share this with the world in case someone else had the same problem. After updating a page and copying it to the webserver when I came to look at it again I got this message:

Unable to cast object of type 'ASP.controls_student_ascx' to type 'Controls_Student'

In order to resolve this I had to clear my cache and then it worked fine.

Monday, 15 November 2010

Using javascript to create full screen windows in ASP .Net

My current role at DatabridgeMiS involves creating a web version of existing software, which can be tricky when trying to create the same functionality of popup windows that can be maximized to full screen.

When using the Ajax Control Toolkit I can use the ModalPopupExtender to create simple popup windows by setting the target to a panel that is invisible by default. I can set the panels width and height to control the size of my popup window.

However, I wanted to be able to achieve full width and height but I found using percentages of 100% for both and the body worked ok to some extent but looked confusing in places plus I wanted specific sizing for some items.

So I decided to create my own code to perform this action myself. I already had jquery attached to my page so I could use the document.Ready function to insert my code.

First I needed to find the width and height of the current viewport:

var viewportwidth;
var viewportheight;

if (typeof window.innerWidth != 'undefined') {
viewportwidth = window.innerWidth;
viewportheight = window.innerHeight;
}
else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
viewportheight = document.documentElement.clientHeight;
viewportwidth = document.documentElement.clientWidth;
}
else {
viewportheight = document.getElementsByTagName('body')[0].clientHeight;
viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
}

The code above performs different checks as different browsers use different methods of obtaining the viewport information and using only one method wouldn't work across the board. Next I used a jquery style function to find all 'panels' that I had put a class of 'fullscreen' on:

$('.fullscreen').each(function (i) {
$(this).width(viewportwidth);
$(this).height(viewportheight);
});

This then regardless of browser makes my popup full screen (although watch out for margin or padding settings that could enlarge it).

Then I simply used other jquery style functions to set elements height/width within the popup by simply subtracting the viewport settings. Have a play yourself and check it out on different browsers and screens, works brilliantly.