tag:blogger.com,1999:blog-89303506311701393002023-11-15T11:05:57.253-05:00.NETThe .NET Framework allows developers to use the same set of skills to rapidly buid great applications for the web, windows, services and more.Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.comBlogger39125tag:blogger.com,1999:blog-8930350631170139300.post-42227302216695568062013-02-15T09:50:00.001-05:002013-02-15T13:40:45.972-05:00Number Validation in Textbox of ASP.NET Using Regular Expression Validator<pre><code><span style="font-family: Georgia, Times New Roman, serif;">Below is a simple example of Number Validation in Textbox of ASP.Net Using Regular Expression Validator.
It will also allow all decimal number, excluding all alphanumeric characters.
<b><asp:TextBox ID="AssetsTextBox" runat="server" Width="240px"></asp:TextBox>
<asp:RegularExpressionValidator ID="revAssets" runat="server"
ErrorMessage="Assets must be a number" Font-Bold="False"
Font-Size="Small"
ValidationExpression="^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$"
ControlToValidate="AssetsTextBox" Display="Dynamic" >
</asp:RegularExpressionValidator></b>
</span></code></pre>
Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-79158301976801232302013-02-09T19:34:00.001-05:002013-02-09T19:47:31.790-05:00Display Text Upside down using CSS3<pre><code><span style="font-family: Georgia, Times New Roman, serif;">Below example will help you to learn how to Display some text Upside down on the webpage using CSS3.
<b><html>
<head>
<title>Display Text Up side down Example...</title>
<style type="text/css">
.displayUpsideDown
{
-o-transform: rotate(-180deg); /* Opera 10.5 */
-webkit-transform: rotate(-180deg); /* Safari 3.1+, Chrome */
filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=2); /* IE6,IE7 */
ms-filter: "progid:DXImageTransform.Microsoft.BasicImage(rotation=2)"; /* IE8 */
-moz-transform: rotate(-180deg); /* FF3.5+ */
position: absolute;
}
</style>
</head>
<body>
<div class="displayUpsideDown">ENJOY SAMPLE TEXT HERE!!!</div>
</body>
</html></b>
</span></code></pre>
Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-79309661033654931182012-01-04T14:21:00.002-05:002013-02-07T13:53:21.265-05:00ASP.NET MVC 3 Framework<span style="font-family: Georgia, 'Times New Roman', serif;">Many people are searching for a BEST book for ASP.NET MVC 3 Frameowrk. I can guarantee you will enjoy reading below book for ASP.NET MVC 3 Framework.<br />
</span> <br />
<span style="font-family: Georgia, 'Times New Roman', serif;"></span><br />
<span style="font-family: Georgia, 'Times New Roman', serif;"><b><a href="http://www.amazon.com/gp/product/1430234040?ie=UTF8&tag=aspnettelligent-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=1430234040%22">http://www.amazon.com/gp/product/1430234040?ie=UTF8&tag=aspnettelligent-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=1430234040"</a></b></span><br />
<div>
<span style="font-family: Georgia, 'Times New Roman', serif;"><br />
</span></div>
Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-58553238317479120492011-11-01T10:24:00.003-04:002013-02-07T13:53:28.877-05:00Remove dots(.), hyphens(-), spaces,braces’(‘ and ‘)’ from string in C#<span style="font-family: Georgia, "Times New Roman", serif;">If you want to remove dots(.), hyphens(-), spaces, braces ‘(‘ and ‘)’ from a string in C# then you can use any solution given below.<br />
<br />
string str = "A sample string having many . and ......It also has a hyphen (-) and too Many hyphens(------) Enjoy!!";<br />
<br />
<strong>// Solution1: Use of String concat Function and Lambda Extension Method</strong><br />
var solution1 = string.Concat(str.Where(i => !new[] { '.', ' ', '-', '(', ')' }.Contains(i)));<br />
<br />
Response.Write(solution1);<br />
<br />
<br />
<strong>// Solution2: Use of Regular Expression. (Make sure you add using System.Text.RegularExpressions; on top of your page before using below solution2)</strong><br />
var solution2 = Regex.Replace(str, "[ ().-]+", "");<br />
<br />
Response.Write(solution2);<br />
<br />
<br />
<strong>// Solution3 : Use of Lambda and Extension Methods</strong><br />
var solution3 = str.ToCharArray().Where(i => i != ' ' && i != '-' && i != '.' && i != '(' && i != ')' ).Aggregate(" ", (a, b) => a + b);<br />
<br />
Response.Write(solution3);</span>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com1tag:blogger.com,1999:blog-8930350631170139300.post-91327917241706054152011-07-07T15:17:00.002-04:002011-07-07T15:21:37.327-04:00Remove Duplicates From List in C#<span style="font-family: Georgia, "Times New Roman", serif;">To <strong>Remove Duplicates From List</strong> can be done by many different ways. You can use Distinct extension method to remove duplicates.<br />
<br />
Below program that removes duplicates from the list in C#.<br />
<br />
<strong>using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
<br />
class Program<br />
{<br />
static void Main()<br />
{<br />
// List having duplicate integer elements.<br />
List<int> list = new List<int>();<br />
list.Add(7);<br />
list.Add(2);<br />
list.Add(5);<br />
list.Add(5);<br />
list.Add(2);<br />
list.Add(3);<br />
list.Add(7);</strong></span><br />
<span style="font-family: Georgia, "Times New Roman", serif;"><strong><br />
</strong></span><span style="font-family: Georgia, "Times New Roman", serif;"><strong> // Get distinct elements and convert again into a list.<br />
List<int> distinct = list.Distinct().ToList();<br />
<br />
foreach (int value in distinct)<br />
{<br />
Console.WriteLine("Distinct : {0}", value);<br />
}<br />
}<br />
}</strong></span><br />
<span style="font-family: Georgia, "Times New Roman", serif;"><strong><br />
</strong></span><span style="font-family: Georgia, "Times New Roman", serif;"><span style="font-size: large;"><strong>OR </strong></span><br />
<br />
You can use <strong>LINQ to object to remove duplicates from list</strong>,</span><br />
<br />
<span style="font-family: Georgia, "Times New Roman", serif;"><strong>using System;<br />
using System.Collections.Generic;<br />
using System.Linq;</strong></span><br />
<br />
<span style="font-family: Georgia, "Times New Roman", serif;"><strong>class Program<br />
{<br />
static void Main()<br />
{<br />
// List having duplicate string elements.<br />
List<string> list = new List<string>();<br />
list.Add("A");<br />
list.Add("A"); <br />
list.Add("C");<br />
list.Add("D");<br />
list.Add("B");<br />
list.Add("A");<br />
list.Add("E");<br />
list.Add("E");</strong></span><br />
<br />
<span style="font-family: Georgia, "Times New Roman", serif;"><strong> // Get distinct elements.<br />
var distinct = (from item in list orderby item select item).Distinct();<br />
<br />
foreach (string value in distinct)<br />
{<br />
Console.WriteLine("Distinct : {0}", value);<br />
}<br />
}<br />
}</strong></span>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-32629474261811676052011-06-30T11:01:00.001-04:002019-04-27T09:56:43.214-04:00Sort Integer and String Array in C#<span style="font-family: "georgia" , "times new roman" , serif;"><strong><span style="font-size: large;">Example 1: Sort Int[] Array</span></strong><br />
This example int[] array that you have in your C# program. You can call static Array.Sort Method and use it to sort a integer array in place. The result is an ascending order of numbers.<br />
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
<br />
class Program<br />
{<br />
static void Main(string[] args)<br />
{<br />
<br />
int[] data = { 999,9,19,555,5,18,399};<br />
Array.Sort(data);<br />
<br />
foreach (int i in data)<br />
Console.WriteLine(i);<br />
<br />
Console.ReadLine();<br />
}<br />
}</span><br />
<br />
<span style="font-family: "georgia" , "times new roman" , serif;"><strong><span style="font-size: large;">Example 2: Sort string[] Array</span></strong><br />
This example string[] array that you have in your C# program. You can call static Array.Sort Method and use it to sort a string array in place. The result is an alphabetical sort.</span><br />
<br />
<span style="font-family: "georgia" , "times new roman" , serif;">using System;<br />
<br />
class Program<br />
{<br />
static void Main()<br />
{<br />
string[] str = new string[]<br />
{<br />
"British",<br />
"Indian",<br />
"American",<br />
"Chinese",<br />
"Malaysians"<br />
};<br />
Array.Sort(str);<br />
<br />
foreach (string s in str)<br />
Console.WriteLine(s); <br />
<br />
Console.ReadLine();<br />
}</span><br />
<br />
<span style="font-family: "georgia" , "times new roman" , serif;"><span style="font-size: large;"><strong>Example 3: Sort string Array with LINQ</strong></span></span><br />
<span style="font-family: "georgia" , "times new roman" , serif;">This example string[] array that you have in your C# program. You can call static Array.Sort Method and use it to sort a string array in place. The result is an alphabetical sort. We will use LINQ query expression to order its contents.</span><br />
<br />
<span style="font-family: "georgia" , "times new roman" , serif;"> </span><span style="font-family: "georgia" , "times new roman" , serif;">using System;<br />
using System.Linq;<br />
<br />
class Program<br />
{<br />
static void Main()<br />
{<br />
string[] str = new string[]<br />
{<br />
"British",<br />
"Indian",<br />
"American",<br />
"Chinese",<br />
"Malaysians"<br />
};<br />
var sort = from s in str<br />
orderby s<br />
select s;<br />
<br />
foreach (string c in sort)<br />
Console.WriteLine(c);<br />
<br />
Console.ReadLine();<br />
}<br />
}</span><br />
<span style="font-family: "georgia" , "times new roman" , serif;"><br />
</span><span style="font-family: "georgia" , "times new roman" , serif;"><strong><span style="font-size: large;">Example 4: Sort string Array in reverse using LINQ</span></strong></span><br />
<span style="font-family: "georgia" , "times new roman" , serif;">This example string[] array that you have in your C# program. You can call static Array.Sort Method and use it to sort a string array in place. The result is an alphabetical decending sort. We will use LINQ query expression to order its contents.<br />
<br />
using System;<br />
using System.Linq;<br />
<br />
class Program<br />
{<br />
static void Main()<br />
{<br />
string[] str = new string[]<br />
{<br />
"British",<br />
"Indian",<br />
"American",<br />
"Chinese",<br />
"Malaysians"<br />
};<br />
var desc = from s in str<br />
orderby s descending<br />
select s;<br />
<br />
foreach (string s in desc)<br />
Console.WriteLine(s);<br />
<br />
Console.ReadLine();<br />
}<br />
}</span><br />
<br />
<span style="font-family: "georgia" , "times new roman" , serif;"><strong><span style="font-size: large;">Example 5: Sort List</span></strong></span><br />
<span style="font-family: "georgia" , "times new roman" , serif;">List<string> is a generic List collection of strings. It is stored as a string array. We can use LINQ exact same way we used in Example 3 and 4.<br />
<br />
using System;<br />
using System.Collections.Generic;</span><span style="font-family: "georgia" , "times new roman" , serif;">class Program<br />
{<br />
static void Main()<br />
{<br />
List<string> lst = new List<string>()<br />
{<br />
"British",<br />
"Indian",<br />
"American",<br />
"Chinese",<br />
"Malaysians"<br />
};<br />
lst.Sort();<br />
foreach (string s in lst)<br />
Console.WriteLine(s);</span><br />
<span style="font-family: "georgia" , "times new roman" , serif;"><br />
Console.ReadLine();<br />
}<br />
}<br />
</span>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com3tag:blogger.com,1999:blog-8930350631170139300.post-25509559988368198492011-06-09T10:15:00.009-04:002011-06-14T11:49:17.115-04:00Split String VB.NET Examples<span style="font-family: Georgia, "Times New Roman", serif;">How to Split String in VB.NET? There are different ways to call Split function in VB.NET. Below solutions guide you VB.NET Split string/sentence with examples.<br />
<br />
<strong><span style="font-size: large;">Solution 1:</span></strong></span><br />
<span style="font-family: Georgia, "Times New Roman", serif;"><strong>Simple Split Example. Below example uses Split on String.</strong><br />
' Input string<br />
Dim str As String = "This is a test string"<br />
<br />
' Split string based on spaces<br />
Dim words As String() = str.Split(New Char() {" "c})<br />
<br />
' Use For Each loop over words and display them. See the result below in Output.<br />
Dim word As String<br />
For Each word In words<br />
Console.WriteLine(word)<br />
Next</span><br />
<br />
<span style="font-family: Georgia, "Times New Roman", serif;"><strong><span style="font-size: large;">Solution 2:</span></strong></span><br />
<span style="font-family: Georgia, "Times New Roman", serif;"><strong>Split Parts of a file path in VB.NET. Below example that splits file path.</strong><br />
' file system path we need to split<br />
Dim str As String = "C:\Users\TestUser\Documents\Doc"<br />
<br />
' Split the string on the backslash character (\)<br />
Dim parts As String() = str.Split(New Char() {"\"c})<br />
<br />
' Loop through result strings with For Each. <br />
Dim part As String<br />
For Each part In parts<br />
Console.WriteLine(part)<br />
Next</span><br />
<br />
<span style="font-family: Georgia, "Times New Roman", serif;"><strong><span style="font-size: large;">Solution 3:</span></strong></span><br />
<span style="font-family: Georgia, "Times New Roman", serif;"><strong>Split string or sentence base on words in VB.NET</strong></span><span style="font-family: Georgia, "Times New Roman", serif;"><strong><br />
</strong>There are cases where you need to extract the words from a string/sentence in VB.NET. Below solution can handle non-word characters differently than the String Split method. For the same we need to use Regex.Split to parse the words in a string.<br />
<br />
Add below line on top of your page.<br />
Imports System.Text.RegularExpressions<br />
<br />
Below code will splits words in VB.NET<br />
Dim str As String<br />
Dim arr As String() = SplitWordsInString("Oh! this is a simple task.")<br />
<br />
For Each str In arr<br />
Console.WriteLine(str)<br />
Next<br />
Console.ReadLine()<br />
<br />
Private Function SplitWordsInString (ByVal str As String) As String() <br />
Return Regex.Split(str, "\W+")<br />
End Function</span>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com8tag:blogger.com,1999:blog-8930350631170139300.post-47883395077988266692011-06-01T10:12:00.003-04:002011-06-04T22:53:22.852-04:00ASP.NET Webservices - "The test form is only available for requests from the local machine."<span style="font-family: Georgia, "Times New Roman", serif;">Many times users get an error as "The test form is only available for requests from the local machine." when they try to invoke web service from remote machine.<br />
<br />
To enable the Service to be invoked from remote machine, we need to add the following settings to the Web.Config file of the Web Service Application.<br />
<br />
<strong><system.web><br />
<webServices><br />
<protocols><br />
<add name="HttpGet"/> <br />
<add name="HttpPost"/> <br />
</protocols> <br />
</webServices><br />
</system.web><br />
</strong>This would enable the Web service to be able to be invoked from remote machine. However, this invoking would work only for simple data types and would not work in the case of complex datatypes.<br />
<br />
</span>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-13054370928727057642011-05-10T10:21:00.002-04:002011-06-03T09:35:27.254-04:00Changing Master Page at Runtime<pre "times="" georgia,="" new="" roman",="" serif;?=""><code><span style="font-family: Georgia, "Times New Roman", serif;"><strong><span style="font-size: large;">Solution 1:
Changing Master Page at Runtime by user code.</span></strong>
Many times we have to change Master Page at Runtime by user code. Page_PreInit event will execute just before that page is render. We can write a simple code in Page_PreInit event like below,
protected void Page_PreInit(object sender, EventArgs e)
{
if (Membership.GetUser() == null) //check if the user is logged in or not
this.MasterPageFile = "~/General.master";
else
this.MasterPageFile = "~/MyPortal.master";
}</span></code><span style="font-family: Georgia, "Times New Roman", serif;">
</span></pre><pre "times="" georgia,="" new="" roman",="" serif;?=""><code><span style="font-family: Georgia, "Times New Roman", serif;"><strong><span style="font-size: large;">Solution 2:
Changing Master Page at Runtime by user code based on users roles and responsibilities.</span></strong>
Sometimes there different types of users for the same application. All users have their own roles and responsibilities for the application.
For e.g There are different types of users for my portal application. They all should be able to browse their role related master page.
1. Executive Directors
2. Admin
3. Investment Officers
4. Users
For above portal application senerio we can write code in content Page_PreInit event like below,</span></code><span style="font-family: Georgia, "Times New Roman", serif;">
</span></pre><pre "times="" georgia,="" new="" roman",="" serif;?=""><code>protected void Page_PreInit(object sender, EventArgs e)
{
int varRole = 0;
int.TryParse(Session["Role"].ToString(),out varRole);
if (varRole == 1)
{
this.MasterPageFile = "ExecDir.master";
}
if (varRole == 2)
{
this.MasterPageFile = "Admin.master";
}
if (varRole == 3)
{
this.MasterPageFile = "Officer.master";
}
if (varRole == 4)
{
this.MasterPageFile = "User.master";
}
}
</code></pre>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-26349872897802094462011-05-04T20:51:00.007-04:002011-06-09T15:46:17.274-04:00How to Rename a File<span style="color: black; font-family: Georgia, "Times New Roman", serif;"><strong>How to Rename a File? To Rename a file in C# is,</strong></span><br />
<br />
<span style="color: black;"><span style="font-family: Georgia, "Times New Roman", serif;"><span style="color: blue;">string</span> path1 = <span style="color: #a31515;">@"c:\MyTest1.txt"</span>;<br />
<span style="color: blue;">string</span> path2 = <span style="color: #a31515;">@"c:\MyTest2.txt"</span>;</span></span><br />
<br />
<span style="color: black; font-family: Georgia, "Times New Roman", serif;">System.IO.File.Move(path1,path2);</span><br />
<br />
<br />
<span style="color: black; font-family: Georgia, "Times New Roman", serif;"><strong>To Rename a file in VB.Net is,</strong></span><br />
<br />
<span style="color: black;"><span style="font-family: Georgia, "Times New Roman", serif;"><span style="color: blue;">Dim</span> path1 <span style="color: blue;">As</span> <span style="color: blue;">String</span> = <span style="color: #a31515;">"c:\MyTest1.txt"</span><br />
<span style="color: blue;">Dim</span> path2 <span style="color: blue;">As</span> <span style="color: blue;">String</span> = <span style="color: #a31515;">"c:\MyTest2.txt"</span></span></span><br />
<br />
<span style="color: #a31515;"><span style="color: black; font-family: Georgia, "Times New Roman", serif;">System.IO.File.Move(path1,path2)</span></span><span style="color: black;"><br />
<br />
<span style="font-family: Georgia, "Times New Roman", serif;"></span></span><br />
<br />
<span style="color: black;"><br />
<span style="font-family: Georgia, "Times New Roman", serif;"></span></span>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-57358465549475910662011-05-01T11:27:00.002-04:002011-06-03T11:29:55.789-04:00Clear All TextBoxes on a form<span style="font-family: Georgia, "Times New Roman", serif;">If you want to clear/Reset all Textboxes on a form, below solution will help you clear all Textboxes at a time.<br />
You can write a simple funtion using below code,<br />
<br />
<strong>public static void ClearAllTextBoxes(Control oControl)<br />
{<br />
foreach (Control ct in oControl.Controls)<br />
{<br />
if (ct is TextBox)<br />
{<br />
TextBox tb = (TextBox)ct.FindControl(ct.ID);<br />
tb.Text = string.Empty;<br />
}<br />
if (ct.HasControls())<br />
{<br />
ClearAllTextBoxes(ct);<br />
}<br />
}<br />
}</strong><br />
Call above Funtion using below code,<br />
<strong>ClearAllTextBoxes(this);</strong></span>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-63748777124611039052011-04-15T15:22:00.007-04:002011-06-09T15:50:22.573-04:00WindowsIdentity.GetCurrent().Name in ASP.NET<pre "times="" georgia,="" new="" roman",="" serif;?=""><code><span style="font-family: Georgia, "Times New Roman", serif;">You can use following code, if you want to know the user under which your ASP.NET application is running.
Add following namespace on top of your web page,
<strong>using System.Security.Principal;</strong>
Add following code to Page_Load event,
<strong>WindowsIdentity id = WindowsIdentity.GetCurrent();
Response.Write("Account Name: " + id.Name + "<br>");</strong>
</span></code></pre>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-54992694045058191892011-04-11T14:14:00.002-04:002011-05-31T13:06:49.957-04:00Display Current Time on the web page<pre "times="" georgia,="" new="" roman",="" serif;?=""><code><span style="font-family: Georgia, "Times New Roman", serif;">Below example helps you to display current time on the web page. Textbox value will be updated with the current time every second.
Add following code to the page,
<strong><script type="text/javascript">
function DisplayTime()
{
var currentDate = new Date();
document.getElementById("<%= TextTime.ClientID %>").value = currentDate.toLocaleTimeString();
window.setTimeout("DisplayTime()", 1000);
}
</script>
<script type="text/javascript">
window.setTimeout("DisplayTime()", 1000);
</script>
<asp:TextBox ID="TextTime" runat="server"></asp:TextBox></strong>
</span></code></pre>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-4485475712374594062011-04-10T23:09:00.000-04:002011-05-25T08:54:15.099-04:00Javascript Alert message from code behind in ASP.NET<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="mso-bidi-font-weight: bold;"><span style="font-family: Georgia, "Times New Roman", serif;">We all know that we can display javascript alert message from code behind in ASP.NET using Page.ClientScript.RegisterStartupScript.</span></span></div><div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="mso-bidi-font-weight: bold;"><span style="font-family: Georgia, "Times New Roman", serif;">Page.ClientScript.RegisterStartupScript(this.GetType(),"myscript","alert('hello world!');");</span></span></div><div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="mso-bidi-font-weight: bold;"><span style="font-family: Georgia, "Times New Roman", serif;">However, above method can break under ASP.NET AJAX environment. For AJAX we need to use ScriptManager.RegisterStartupScript. Below code displays javascript alert message both in AJAX environment and on regular page as well.</span></span></div><div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="mso-bidi-font-weight: bold;"><span style="font-family: Georgia, "Times New Roman", serif;"><strong>ScriptManager.RegisterStartupScript(this,this.GetType(),"myscript","alert('hello world!');",true);</strong></span></span></div><div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="mso-bidi-font-weight: bold;"><span style="font-family: Georgia, "Times New Roman", serif;">For more information on ClientScriptManager<span xmlns="">.</span>RegisterStartupScript Method you can visit,</span></span></div><div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="mso-bidi-font-weight: bold;"><span style="color: black; font-family: Georgia, "Times New Roman", serif;"><a href="http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript.aspx">http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript.aspx</a></span></span></div>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com2tag:blogger.com,1999:blog-8930350631170139300.post-62494660846601868452011-04-09T08:47:00.002-04:002011-06-09T08:49:54.178-04:00Space Example in VB.NET<span style="font-family: Georgia, "Times New Roman", serif;">If you want to return a string that contains the specified numbers of spaces or if you want to append a number with specified number of spaces then below example will help you solve the problem.<br />
You can use below code too if you want to append or add blank spaces to the right of the string or number.<br />
<br />
<strong>Dim part2 as Integer<br />
Dim part3 As String<br />
<br />
part2 = 12345<br />
part3 = part2.ToString() + Space(5)<br />
MsgBox(part3.Length())</strong><br />
<br />
Above example will add 5 blank spaces to the number 12345. So the Length of the number is 10 and the messagebox will return 10.<br />
</span>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-16890938422965743962011-04-08T08:38:00.002-04:002011-06-08T08:44:26.982-04:00Open a new Window in JavaScript<span style="font-family: Georgia, "Times New Roman", serif;">How to Open a new Window in Javascript/using Javascript is easy. Copy below code in your page.<br />
<br />
If you want to open a new window on button clik then use below code,<br />
<strong><input type="button" value="Open New Window" onClick="OpenNewWindow('http://www.google.com')" /></strong><br />
<br />
If you want to open a new window using hyperlink then use below code,<br />
<strong><a href="javascript:OpenNewWindow('http://www.google.com/')">Open Google</a></strong><br />
<br />
Copy below javascript code between your <head> and </head> tags on the page.<br />
<strong><script type="text/javascript"><br />
function OpenNewWindow(url)<br />
{<br />
newwindow = window.open(url, 'mywindow', 'width=600,height=700'); <br />
newwindow.focus();<br />
}<br />
</script></strong></span>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-13452255012873395762011-03-31T13:42:00.002-04:002011-05-31T11:56:43.291-04:00Display Textbox count on a Label<pre "times="" georgia,="" new="" roman",="" serif;?=""><code><span style="font-family: Georgia, "Times New Roman", serif;">This solution will help you use asp Text box and a Label to display the count of characters,
Add below code on your Default.aspx,
<strong><asp:TextBox ID="txtComments" runat="server" TextMode="MultiLine"
MaxLength="160" ></asp:TextBox>
<asp:Label ID="lblCharacters" runat="server"></asp:Label></strong>
Add below javascript code on your default.aspx,
<strong><script language="javascript" type="text/javascript">
function DisplayCount()
{
var length = document.getElementById('<%=txtComments.ClientID%>').value.length;
document.getElementById('<%=lblCharacters.ClientID%>').innerText = length;
}
</script></strong>
Add following code on your Page_load event,
<strong>txtComments.Attributes.Add("onkeypress", "javascript:return DisplayCount();");</strong>
After adding above code, you can see the count on the label when you type in the textbox.
</span></code></pre>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-84785026264756210392011-03-22T14:43:00.011-04:002011-06-10T12:59:59.376-04:00Disable Right click on web page<pre "times="" georgia,="" new="" roman",="" serif;?=""><code><span style="font-family: Georgia, "Times New Roman", serif;">How to disable right click on web page or If you want to protect your source code or many times programmers may need to disable right click on web page. You can copy below script code in between <head> and </head> tag.
</span><span style="font-family: Georgia, "Times New Roman", serif;"><strong><script type="text/javascript">
var message="You Can't right click on this page!";
function clickIE()
{
if (event.button==2)
{
alert(message);
return false;
}
}
if (document.all&&!document.getElementById)
{
document.onmousedown=clickIE;
}
document.oncontextmenu=new Function("alert(message);return false")
</script></strong>
</span></code></pre>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-14907552433210600592011-03-08T09:21:00.005-05:002011-06-08T09:24:07.675-04:00Get Assembly Name<span style="font-family: Georgia, "Times New Roman", serif;">Sometimes you are asked to get/find Assembly name of a class.<br />
<br />
You can use below code to get/find Assembly Name in VB.Net,<br />
<strong>Dim assembly As System.Reflection.Assembly = System.Reflection.Assembly.GetAssembly(GetType(Class1))<br />
Response.Write(assembly.GetName().Name)</strong></span><br />
<span style="font-family: Georgia, "Times New Roman", serif;"><strong><br />
</strong><br />
You can use below code to get/find Assembly Name in C#,<br />
<strong>System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof(Class1));<br />
Response.Write(assembly.GetName().Name);</strong><br />
<br />
Here in above code Assembly.GetAssembly Method gets the currently loaded assembly in which the specified class is defined.<br />
</span>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-52736955450237082192011-03-03T10:49:00.001-05:002011-05-25T15:45:50.435-04:00DateTime in different formats<span style="font-family: Georgia, "Times New Roman", serif;">Using <strong>DateTime.Tostring()</strong> method, you can display Date and/or Time based on your requirement. </span><br />
<span style="font-family: Georgia, "Times New Roman", serif;">Find the code below displaying Date in 24 different formats. </span><span style="font-size: x-small;"></span><br />
<br />
Response.Write("Displaying DateTime in 24 Different Formats....");<br />
<br />
strFormat = dt.ToString("MM/dd/yyyy");<br />
Response.Write("Format 1: " + strFormat);<br />
<br />
strFormat = dt.ToString("dddd, dd MMMM yyyy");<br />
Response.Write("Format 2: " + strFormat);<br />
<br />
strFormat = dt.ToString("dddd, dd MMMM yyyy HH:mm");<br />
Response.Write("Format 3: " + strFormat);<br />
<br />
strFormat = dt.ToString("dddd, dd MMMM yyyy hh:mm tt");<br />
Response.Write("Format 4: " + strFormat);<br />
<br />
strFormat = dt.ToString("dddd, dd MMMM yyyy H:mm");<br />
Response.Write("Format 5: " + strFormat);<br />
<br />
strFormat = dt.ToString("dddd, dd MMMM yyyy h:mm tt");<br />
Response.Write("Format 6: " + strFormat);<br />
<br />
strFormat = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss");<br />
Response.Write("Format 7: " + strFormat);<br />
<br />
strFormat = dt.ToString("MM/dd/yyyy HH:mm");<br />
Response.Write("Format 8: " + strFormat);<br />
<br />
strFormat = dt.ToString("MM/dd/yyyy hh:mm tt");<br />
Response.Write("Format 9: " + strFormat);<br />
<br />
strFormat = dt.ToString("MM/dd/yyyy H:mm");<br />
Response.Write("Format 10: " + strFormat);<br />
<br />
strFormat = dt.ToString("MM/dd/yyyy h:mm tt");<br />
Response.Write("Format 11: " + strFormat);<br />
<br />
strFormat = dt.ToString("MM/dd/yyyy HH:mm:ss");<br />
Response.Write("Format 12: " + strFormat);<br />
<br />
strFormat = dt.ToString("MMMM dd");<br />
Response.Write("Format 13: " + strFormat);<br />
<br />
strFormat = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK");<br />
Response.Write("Format 14: " + strFormat);<br />
<br />
strFormat = dt.ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'");<br />
Response.Write("Format 15: " + strFormat);<br />
<br />
strFormat = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss");<br />
Response.Write("Format 16: " + strFormat);<br />
<br />
strFormat = dt.ToString("HH:mm");<br />
Response.Write("Format 17: " + strFormat);<br />
<br />
strFormat = dt.ToString("hh:mm tt");<br />
Response.Write("Format 18: " + strFormat);<br />
<br />
strFormat = dt.ToString("H:mm");<br />
Response.Write("Format 19: " + strFormat);<br />
<br />
strFormat = dt.ToString("h:mm tt");<br />
Response.Write("Format 20: " + strFormat);<br />
<br />
strFormat = dt.ToString("HH:mm:ss");<br />
Response.Write("Format 21: " + strFormat);<br />
<br />
strFormat = dt.ToString("yyyy'-'MM'-'dd HH':'mm':'ss'Z'");<br />
Response.Write("Format 22: " + strFormat);<br />
<br />
strFormat = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss");<br />
Response.Write("Format 23: " + strFormat);<br />
<br />
strFormat = dt.ToString("yyyy MMMM");<br />
Response.Write("Format 24: " + strFormat);Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-34526765843475325712011-02-12T08:22:00.014-05:002011-06-04T22:48:35.934-04:00Reading Flat file in VB.NET<span style="font-family: Georgia, "Times New Roman", serif;">Below example will help you Reading flat file in VB.Net.<br />
<br />
Using myReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\test.txt")<br />
myReader.TextFieldType = FileIO.FieldType.Delimited<br />
' In this case data in flat file are separated by pipe("|"). <br />
myReader.SetDelimiters("|") <br />
<br />
Dim currentRow As String()<br />
While Not myReader.EndOfData<br />
Try<br />
currentRow = myReader.ReadFields()<br />
Dim currentField As String<br />
For Each currentField In currentRow<br />
MsgBox(currentField)<br />
Next<br />
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException<br />
MsgBox("Line " & ex.Message & "is not valid and will be skipped.")<br />
End Try<br />
End While<br />
End Using<br />
</span>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-19018796019131306482011-02-02T22:00:00.001-05:002011-05-23T11:01:21.058-04:00Sending email from ASP.NET<span style="font-family: Georgia, "Times New Roman", serif;">Sending Email from an ASP.NET web application is a simple task.</span><br />
<br />
<span style="font-family: Georgia, "Times New Roman", serif;">For sending Email from ASP.NET web application we need to add one namespace,</span><br />
<br />
<span style="font-family: Georgia, "Times New Roman", serif;"><strong>using System.Net.Mail;</strong></span><br />
<br />
<span style="font-family: Georgia, "Times New Roman", serif;">After this we can create a method , sendEmail().</span><br />
<br />
<span style="font-family: Georgia, "Times New Roman", serif;"><strong>public void sendEmail()<br />
{<br />
MailMessage message = new MailMessage();<br />
message.From = new MailAddress("</strong></span><a href="mailto:test@gmail.com"><span style="font-family: Georgia, "Times New Roman", serif;"><strong>test@gmail.com</strong></span></a><span style="font-family: Georgia, "Times New Roman", serif;"><strong>");<br />
message.To.Add(new MailAddress("</strong></span><a href="mailto:test1@gmail.com"><span style="font-family: Georgia, "Times New Roman", serif;"><strong>test1@gmail.com</strong></span></a><span style="font-family: Georgia, "Times New Roman", serif;"><strong>"));<br />
message.Subject = "Sending an email from ASP.NET web application";<br />
message.Body = "Your Email Content here…";<br />
SmtpClient client = new SmtpClient();<br />
client.Host = "Give your mail server IP";<br />
client.Port = Put port number;<br />
client.Send(message );<br />
<br />
}</strong></span><br />
<br />
<span style="font-family: Georgia, "Times New Roman", serif;">You can use this function anywhere from application.</span>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com0tag:blogger.com,1999:blog-8930350631170139300.post-33789486565907685902011-01-28T11:55:00.009-05:002011-06-04T23:01:52.250-04:00Validate Email Address using RegularExpressionValidator<pre "times="" georgia,="" new="" roman",="" serif;?=""><code><span style="font-family: Georgia, "Times New Roman", serif;">Sometimes there might be the requirement to Validate Email Address using RegularExpressionValidator in application.
</span></code></pre><pre "times="" georgia,="" new="" roman",="" serif;?=""><code><span style="font-family: Georgia, "Times New Roman", serif;">If Email address Textbox on your form is Required Field, then try to use following code on your page.
</span><span style="font-family: Georgia, "Times New Roman", serif;"><strong><asp:TextBox ID="txtEmail" runat="server" />
<asp:RequiredFieldValidator ID="txtEmailRequired" runat="server"
ControlToValidate="txtEmail"
Text="* Required"
Display="Dynamic" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="eg: emailAdd@domain.com"
ControlToValidate="txtEmail"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Display="Dynamic">
</asp:RegularExpressionValidator></strong>
If Email address Textbox on your form is <strong>NOT</strong> a Required Field, then you can use following code on your page.
</span><span style="font-family: Georgia, "Times New Roman", serif;"><strong><asp:TextBox ID="txtEmail" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="eg: emailAdd@domain.com"
ControlToValidate="txtEmail"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Display="Dynamic">
</asp:RegularExpressionValidator></strong>
</span></code></pre>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com1tag:blogger.com,1999:blog-8930350631170139300.post-82098948560205149372011-01-26T12:21:00.005-05:002011-06-03T12:24:30.325-04:00Validate Password and Confirm Password using CompareValidator<span style="font-family: Georgia, "Times New Roman", serif;">When you want to compare <strong>Password</strong> and <strong>Confirm Password</strong> Textbox value, you can use CompareValidator using below code.<br />
<br />
<strong><asp:TextBox ID="txtPassword" runat="server" TextMode="Password" /><br />
<asp:RequiredFieldValidator ID="RequiredPass" runat="server" ControlToValidate="txtPassword" Text="*" /><br />
<br />
Confirm Password <asp:TextBox ID="txtPasswordCompare" runat="server" TextMode="Password" /><br />
<asp:CompareValidator runat="server" ID="ComparePass" ControlToValidate="txtPassword" ControlToCompare="txtPasswordCompare" Text="Password mismatch" /></strong><br />
</span>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com2tag:blogger.com,1999:blog-8930350631170139300.post-34741943743513325722011-01-14T12:26:00.004-05:002011-05-26T17:31:45.540-04:00Remove Querystring item in ASP.NET<pre "times="" georgia,="" new="" roman",="" serif;?=""><code><span style="font-family: Georgia, "Times New Roman", serif;">If we try to remove/delete a query string directly using below code, we will get an error - collection is read-only.
<strong>Request.QueryString.Remove("QSname")</strong>
In order to solve above error problem, we need to write below code before we remove them.
<strong>// reflect to readonly property
PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
// make collection editable
isreadonly.SetValue(this.Request.QueryString, false, null);
// remove
this.Request.QueryString.Remove("QSname");</strong>
</span></code></pre>Nishahttp://www.blogger.com/profile/14821636527787410778noreply@blogger.com2