web 2.0

2009/04/22

JavaScript confirmation disables validation

I had to do a JavaScript confirmation before I perform the post back on a web form. What I have done is just call button.attributes.add("onClick", -- ) and added the confirmation.

It worked fine and showed the popup and did the post back if I clicked on the OK button. But if I have implemented some validations on the fields they doesn't fire them if the validation fails. For example if there is a required field validator the post back happens even without the values for that required field.

what I want is to do the validation and then do the post back. Luckily I've found a solution at http://forums.asp.net/t/963412.aspx?PageIndex=1 by sukdeb.


function Validate()
{
    var yes=confirm("Update this application?");
    if(yes)
    {
        Page_ClientValidate();
        return Page_IsValid;
    }
return false;

}

<asp:button runat="server" text="Button" onclientclick="return Validate()">

Pasted from http://forums.asp.net/t/963412.aspx?PageIndex=1

It worked fine for me. I have added the script as button.attributes.add("onClick", -- ) and it works fine for me even with the "onClick" event.

2009/03/31

Sort Fields which contains both Numeric and Characters

It was a long time since my last post. Finally I got something to write about. Store Procedures … something I hate most of the time. But today I felt good about them because I was able to find a solution for a sorting issue.

The particular column contained data like “98ABC-76543-DEF-12345-GHI10” and also “GHJ23”. I was supposed to sort this field on Numeric value for the last part in red. And for the other parts it should be treated as Numeric for the Numeric parts and as Strings for the others.  So what should I do was to split all parts and sort them separately and get numeric part in red and sort.

But wait a minute do we have to all the things??? I don’t think so... I have found (actually one of my friends) an easy way to do that. Here it is.

SELECT [name],

       RANK() OVER (ORDER BY

        CAST(PARSENAME(modifiedName, 5) AS  nvarchar ),

        CAST(PARSENAME(modifiedName, 4) AS  INT ),

        CAST(PARSENAME(modifiedName, 3) AS  nvarchar ),

        CAST(PARSENAME(modifiedName, 2) AS  INT ),

        CAST(dbo.fFilterNumeric (PARSENAME(modifiedName, 1)) AS  INT ) ) AS sorted_name

FROM

      (SELECT

       [name],

       REPLACE([Name], '-', '.') AS modifiedName

       FROM myTestTable)

AS myTestTableSorted

OK the next thing is to explin what I have done here.  First PARSENAME gets the string part as for the index from the end of the string. Since the PARSENAME is working only with '.' I had to replace the '-' first.

Then for the last part it should be sorted as for the numeric part. For that I have found a function written by IndianScorpion  on forums.asp.net to do that.

CREATE FUNCTION [dbo].fFilterNumeric

(@Src NVARCHAR (255))

RETURNS NVARCHAR (255)

AS

BEGIN

declare @Res nvarchar(255)

declare @i int, @l int, @c char

select @i=1, @l=len(@Src)

SET @Res = ''

while @i<=@l

begin

   set @c=upper(substring(@Src,@i,1))

    IF  isnumeric(@c)=1

        SET @Res = @Res + @c

   set @i=@i+1

end

 

return(@res)

END

 

2008/11/16

Show an animated gif onClientClick

To show an animated gif, something like loading onClientClick is a simple task. You just have to add a JavaScript function on the click event so the JS function will take care until the next page loads from the server. Inside the JS function we just have to hide the excising div and show the div which contains animated loading gif.

I have done it and it works fine on every other browser except IE. On IE the gif is shown but it is not animated. I have to do some R&D and what I found was IE doesn't do anything like that when it is loading another page (communicating with server) and was commented as a feature limitation of IE.

But there should be a way to do that, that's what I was thinking and I found a pretty simple way to do that. Here it is and if you have a better way please share it here.

<div id="divProgress" runat="server"> 
</div>

<div id="Div1" runat="server">

The web form comes here …

</div>


 

The JavaScript was like this.

<script type="text/javascript"> 
<!-- 
  
function ShowProgress() { 
  document.getElementById(
'<%=Div1.ClientID %>').style.display = 'none';       
  document.getElementById(
'<%=divProgress.ClientID %>').style.display = "block"; 
  document.getElementById(
'<%=divProgress.ClientID %>').innerHTML = '<div align="center"" class="ltGreenBorder"><br /><img alt="Progress" align="top" src="/images/pleasewait.gif" /> </div>';

    } 
//--> 
</script>


 

What I have done is simply updates innerHTML for the loading div. The gif started animating magically.