vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Thursday, March 29, 2012

Remove large white space or split string using Regular expression


Introduction:
In this article we split a large whitespace using regular expression

Normally if we use normal split function using white space, it separate by each white space character..But by using regular expression, its very easy to split string using whit space..
Here I illustrate an example by code…
We have string like this…
Code:
Dim str As String = "1234567            12345         678594"

We want to separate only the numbers, In that case normal split function won’t help us…For that we for or regex expression.
Code:

Dim str As String = "1234567            12345         678594"
        Dim str1 As String()
        Dim rgs As Regex = New Regex("  +")
        str1 = rgs.Split(str)

The result the sr1 contains only 3 array. By using this regex pattern (“    +”), it easy to avoid the extra white space.

I hope you enjoy this method to split a string in large white space……….

Wednesday, March 21, 2012

Client and server side state management system using in asp.net


Introduction:
Here you will see about the different state management system used in asp.net web page

What is State management:
·         State management is the process of saving user information over multiple request same or different web page
·         State is saved on the server and preserve continuity of user information.
·         A session is timeframe from when user enters a website until they exit.

There are two types of sate management system
1.       Client side management system.
2.       Sever side management system.

Client side management system:
·         Cookies
Its text file stores the information on client side.
To learn more about cookies with an example click the below link
·         View state
View state objects retain a value on multiple requests. Its saved the data for a single page.
·         Query string
Information stored at the end of the URL,so we can easily get the information from the url.
Server side management system:
·         Application sate
Store information is available to all users.
·         Session state
Store information is available to specific user.
·         Database or state server.
Use database or state server for scalability.
For know about application state ,session with an example click the below link..

show alert message to user before closing browser in asp.net


Introduction:
Show alert message to user when closing browser window
 Its basic need to alert a customer before closing browser. In vb.net we used to write the code in closing button but in asp.net we can't do like that, so we go for JavaScript to make same alert like in vb.net. This alert will display whenever user try to close the browser or move to the next page alert will be shown...
Below Code we have to write body tag on window before unload event call the script function..
<body onbeforeunload = "ConfirmClose(event)">

Script:
<script type="text/javascript">
    var msg = "Are you sure you want to leave close this page?";
    function ConfirmClose(e) {
        var evt = window.event ? event : e;
        if (evt == e) {
            if (!evt.clientY) {
                evt.returnValue = msg;
            }
        }
        else {
           if (evt.clientY < 0) {

                evt.returnValue = msg;
            }
        }
    }
    </script>

I hope you like …

Tuesday, March 20, 2012

Display images from the folder in asp.net



This seems to be common question..How to show a image directly from the folder..Instead of saving a images to database,
Normally we want to display all the images from the particular folder in asp.net..Then we follow easy process to give the image link to the image control dynamically.

In normal case we store the images in database then we retrieve it and show the images. But this process is totally different process. Just we get all the Images form the folder itself..In a previous post i discussed the way to store image URL or direct image in database. For you reference i mention the link as below


  
Here its easy code to show your image form folder..

Code:
if (!IsPostBack)
        {
            DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/images"));
            int i = 0;
            foreach (FileInfo fi in di.GetFiles())
            {
                //HtmlInputImage ib = new HtmlInputImage();
                Image ib = new Image();
                ib.ID = "ImageButton" + i;
               
                i++;
                ib.ImageUrl  = Server.MapPath("~/images")+"\\" + fi.Name;
                ib.Attributes.Add("onclick", "javascript:window.open('" +"../images/" + fi.Name + "');return false;");
                Placeholder1.Controls.Add(ib);
                Literal lit1 = new Literal();
                lit1.Text = "<br/>";
                Placeholder1.Controls.Add(lit1);
                         }

 Grid view:
Incase if you need to bind the image in grid view follow the below code.
DataTable dt = new DataTable();
            dt.Columns.Add("img");
            foreach (FileInfo fi in di.GetFiles())
            {
                DataRow dr = dt.NewRow();
                dr["img"] = Server.MapPath("~/images") + "\\" + fi.Name;
                dt.Rows.Add(dr);
            }
            GridView1.Datasource = dt;
            GridView1.Databind();

I hope you like this article …

Saturday, March 17, 2012

Print a webpage using asp.net


Introduction:

Here you have find the code to print a whole web page using html button. To print a page we use window. Print JavaScript .But its having a drawback print with button control, this button control we don’t like button come on the printing page, so we have to avoid the button printing using CSS.
.
Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hide print button</title>
<style type="text/css" media="screen">
.PrintButton{
display:block;
}
</style>
<style type="text/css" media="print">
.PrintButton{
display:none;
}
</style>
</head>
<body>
<form id="PrintForm" runat ="server">
<h1>
 print a page  with button hide</h1>

<br />
<div>
    <img src="images/k4.jpg" height ="100" width="100" />
</div>
<input id="btnPrint" type="button" value="Print" class="PrintButton" onclick="window.print();" />
</form>
</body>
</html>

Suppose if you want to close the window automatically after taking print out then add the below script in your page. It will close the page after taking printout

<script type="text/javascript">
        function revertback() {
            window.close();
                    }
       window.onafterprint = revertback
      
</script>

Conclusion:
Here you seen to take print out the asp.net page and learn to close a page after took print out.
I hope you like this article post your comments..

Friday, March 16, 2012

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding


Recently I worked with one of my project. When I hosted into live server I felt the connection would be very slow..Also sometimes I got this type of error. Pages loading would be slow..
Regarding thi issues I posted and searched many sites…But I can find the proper reason for this issues. In many cases they said increase the connection time out, but it will never workout for me……..
One of my friend said the solution for my problem. Its in database option..If you have come across this issues..
Open your database properties and click option menu in the properties
Change the autoclose as false. My database its in true then I changed into false..
Afterwards I never get the error, also site speed also increased. For an every database settings the default option autoclose as false…




I hope this article would help to solve these issues…..


Thursday, March 15, 2012

Zooming a image using JavaScript


Here its very simple  code in java script to do zoom   a  pictures..
In a previous article I wrote about the image zooming using ajax slide extender..for your reference I added the link below……..
http://www.dotnetcode.in/2012/02/resize-image-using-ajax-slider-extender.html

Here to add below code onclient click event in the button..Here I kept two button for zoom in and zoom out..When click the button the image has zoom for every click.

Even if you do small modification on that code for your needs it will work for the entire browser window…
JavaScript code:

<script type="text/javascript">
        var image = document.getElementById('Image2');
        function ZoomIn() {
            document.getElementById('Image2').width += 100;
            document.getElementById('Image2').height += 100;
        }
        function ZoomOut() {
            document.getElementById('Image2').width -= 100;
            document.getElementById('Image2').height -= 100;
        }
    </script>



I hope you like it..

Wednesday, March 14, 2012

How to avoid button postback in asp.net


Introduction:
In some cases we need to use button as only onclient click event,in that case we don’t  like the postback of button control.
To avoid the button postback the following code has helpful to solve the problem…..
Code:
  <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick ="scrfn();return false;" />

In button control just add only the return false in on client click event…Also if need to call any script function you can as its in on client click event….
Second method adds the below code in load event to avoid button post back…..
Button1.Attributes.Add("onclick", "return false;")

I hope you like this technique to avoid the button postback…

Tuesday, March 13, 2012

Autogenerate a random password in asp.net


Introduction:
Here we will see about to generate a  password or random character in asp.net.

This type of requirement we need many times, like user create a new login we need to provide random password for the particular user.

In that following case we used this method to generate the random password..
Code:
Function genpassword(ByRef length As Integer)
        Dim passwstr As String
        Dim randnumb As Integer
        Dim intrandom As Random = New Random()
        For i As Integer = 0 To length - 1
            randnumb = intrandom.Next(1, 20)
            passwstr += Microsoft.VisualBasic.Interaction.Choose(randnumb, "A", "1", "D", "2", "F", "3", "G", "4", "H", "5", "J", "6", "K", "7", "L", "8", "M", "9", "N", "P")
        Next
        Return passwstr
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = genpassword(8)
    End Sub

I hope you like this article…..happy coding
Continue your comment relation …………