Sponsored Ad

Thursday, July 15, 2010

How Dynamically Add Rows into ASP.NET Table using C#

This sample will help to add rows and cells into a asp.net table using C#.

You need to first create a row and then create a cell assign the properties of cell and then add the cell to row and then row to table.

 

How Dynamically Add Rows into ASP.NET Table using C#

 

How Dynamically Add Rows into ASP.NET Table using C# Example:

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        TableRow tr = new TableRow();
        TableCell tc = new TableCell();
        tc.Text = "PetsMantra.com";
        tr.Cells.Add(tc);
        TableCell tc2 = new TableCell();
        tc2.Text = "Join2Green.com";
        tr.Cells.Add(tc2);
        tblSample.Rows.Add(tr);
    }
</script>

<html>
<head>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        <asp:table ID="tblSample" runat="server"></asp:table>
        </div>
    </form>
</body>
</html>

Wednesday, July 14, 2010

How to restrict a dropdown selection in C#

You can restring a user to select particular value. Here is sample code to implement this using C#. on selection change event you can check for values and if it matches. Chnge the selected index to 0.

 

How to restrict a dropdown selection in C#

How to restrict a dropdown selection in C# Example:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >
<script runat="server">

    protected void cmbMyList_SelectedIndexChanged(object sender, EventArgs e)
    {   
        Response.Write("You Have Selected: " + cmbMyList.SelectedValue + "<br>");
        if (cmbMyList.SelectedValue.ToString() == "Select 2")
        {
            Response.Write("You are not allowed to select 'Select 2'" );
            cmbMyList.SelectedIndex = 0;
        }      
    }
</script>

<html xmlns="">
<head>
    <title>
     </title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
           <asp:DropDownList ID="cmbMyList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="cmbMyList_SelectedIndexChanged">
           <asp:ListItem Text="Select 1" Value="Select 1"></asp:ListItem>
           <asp:ListItem Text="Select 2" Value="Select 2"></asp:ListItem>
           <asp:ListItem Text="Select 3" Value="Select 3"></asp:ListItem>
           <asp:ListItem Text="Select 4" Value="Select 4"></asp:ListItem>
           </asp:DropDownList>
        </div>
    </form>
</body>
</html>

Saturday, July 10, 2010

How to call both server side and client side methods on button click

This simple method helps you to call server side C# method and client side javascript methods togather with one click. just assign these two parameters with name of C# and javascript functions.

OnClick

OnClientClick

 

How to call both server side and client side methods on button click

 

How to call both server side and client side methods on button click Example:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ">
<script runat="server">
    protected void btnClick_Click(object sender, EventArgs e)
{
Response.Write("Server side click.");
}
</script>
<script language="javascript">
function ClientClick()
{
alert('Client side click');
}
</script>

<html xmlns="">
<head>
    <title>call javascript and C# methods on single click.</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="btnClick"  runat="server" Text="Button" OnClientClick="ClientClick()" OnClick="btnClick_Click" />
        </div>
    </form>
</body>
</html>

Friday, July 9, 2010

How to use Trigger textchange event in textbox using C#

This C# program will help you deal with OnTextChanged event of textbox. This event occur when textbox text changes and user leaves the textbox.

you can perform any operation as per your requirement.

 

How to use Trigger textchange event in textbox using C#

 

How to use Trigger textchange event in textbox using C# Example:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >

<script runat="server">
    protected void txtTextChanged_TextChanged(object sender, EventArgs e)
    {
      Response.Write("OnTextChanged event occured.");
    }

</script>

<html xmlns="">
<head>
    <title>How to use Trigger textchange event in textbox using C#.</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="txtTextChanged" runat="server" AutoPostBack="True" OnTextChanged="txtTextChanged_TextChanged"></asp:TextBox>
        </div>
    </form>
</body>
</html>

Thursday, July 8, 2010

How to Add MultiLine Textbox/TextArea in ASP.NET

To add multiline text box add a simple textbox in asp.net and set textmode property to MultiLine.

TextMode="MultiLine"

you can assign the maximum allowed characters also, to restrict error and unwanted spamming. if want that in particular mode textbox should not editable, you can add readonly property to false.

 

How to Add MultiLine Textbox/TextArea in ASP.NET

 

How to Add MultiLine Textbox/TextArea in ASP.NET Example:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >

<html xmlns="">
<head>
    <title>How to use use textbox.</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" TextMode="MultiLine"  MaxLength="1000" Height="100" Width="300" runat="server"></asp:TextBox>
        </div>
    </form>
</body>
</html>

Wednesday, July 7, 2010

How to change HTML Meta tags using HtmlGenericControl in C#

This example wil help you to define your own meta tags using c#. just define a meta tag me ASPX page and assign values ( name and contents ) in C# file.

Like this line added in aspx page for description meta tag.

<meta id="MetaDescription" runat="server" />

And corresponding line added in C# which assign the values.

MetaDescription.Attributes["Name"] = "Description";

MetaDescription.Attributes["CONTENT"] = "Webpage meta description";

Now you can check the viewsource of the page to check the description meta tag.

<meta id="MetaDescription" Name="Description" CONTENT="Webpage meta description"></meta>

Webpage view source:

How to change HTML Meta tags using HtmlGenericControl in C#

How to change HTML Meta tags using HtmlGenericControl in C#

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        MetaDescription.Attributes["Name"] = "Description";
        MetaDescription.Attributes["CONTENT"] = "Webpage meta description";

        MetaKeyword.Attributes["Name"] = "Keyword";
        MetaKeyword.Attributes["CONTENT"] = "Webpage Meta Keywords";

    }
</script>

<html xmlns="">
<head>
    <title>Use of HtmlGenericControl class</title>
    <meta id="MetaDescription" runat="server" />
    <meta id="MetaKeyword" runat="server" />
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h1>
                How to change HTML Meta tags using HtmlGenericControl.</h1>
        </div>
    </form>
</body>
</html>

Sunday, July 4, 2010

Page Inbuilt CSS Example in ASP.NET

This code will help you to add inbuilt style sheet in your webpage. Inside head tag define your css as given in exampe.

 

clip_image002

clip_image002[4]

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >
<html xmlns=>
<head >
<title>ASP.NET - inbuilt CSS Example.</title>
<style type="text/css">
body {
font-family: Verdana;
}
a:link {
text-decoration: none;
color: Green;
}
a:hover {
text-decoration: underline;
color: red;
}
a:visited {
text-decoration: none;
color: blue;
}

</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href=>All india News</a>
</div>
</form>
</body>
</html>

Saturday, July 3, 2010

Validation Summary Example in ASP.NET

ASP.NET validaion summary is use to display summary for validation results. Suppose you have two controls first name and last name and there is a require field validation for each. Now if you add a validation summary, the failed validations will appear in validation summary instead of requirefieldvalidtions.

 

Validation Summary Example in ASP.NET

Validation Summary Example in ASP.NET

Validation Summary Example in ASP.NET

 

Validation Summary Example in ASP.NET Example:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >
<html>
<body>
    <form id="Form1" runat="server">
        <h4>
            Validation Summary Example</h4>
        <br />
        Enter First Name:
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1"
            runat="server" ErrorMessage="Please Enter First Name" InitialValue="" Text="*" />
        <br />
        Enter Last Name:
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="TextBox2"
            runat="server" ErrorMessage="Please Enter Last Name" InitialValue="" Text="*" />
        <br />
        <asp:Button ID="Button1" Text="Submit" runat="server" />
        <br />
        <br />
        <asp:ValidationSummary ID="ValidationSummary1" HeaderText="You must enter a value in the following fields:"
            DisplayMode="BulletList" EnableClientScript="true" runat="server" />
    </form>
</body>
</html>

Friday, July 2, 2010

How to check/validate to select a radio button option | RequiredFieldValidator

You can also verify that a value should be selected in radio button list before processing. The sample code is given below, Please let me know in case of any erros while implementation.

How to check/validate to select a radio button option | RequiredFieldValidator

How to check/validate to select a radio button option | RequiredFieldValidator

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<body>
    <form id="Form1" runat="server">
        <h4>
            Check for atleast one Radio button selection</h4>
        <br />
        Select a option:
        <asp:RadioButtonList ID="rdo_Websites" RepeatLayout="Flow" runat="server">
            <asp:ListItem>SoftwareTestingNet.com</asp:ListItem>
            <asp:ListItem>IndiHub.com</asp:ListItem>
            <asp:ListItem>BharatClick.com</asp:ListItem>
        </asp:RadioButtonList>
        <br />
        <asp:Button ID="Button1" Text="Submit" runat="server" />
        <br />
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="rdo_Websites"
           Text="Please select a option" runat="server" />
    </form>
</body>
</html>

Thursday, July 1, 2010

How to Check for Selected Value in DropDownList using ASP.NET | RequiredFieldValidator

Now you can veryfy for selcted value in combobox using asp.net control RequiredFieldValidator. This control verifys for values at client side. So you just need to assign name of combobox in validation control and all id done.

 

How to Check for Selected Value in DropDownList using ASP.NET | RequiredFieldValidator

How to Check for Selected Value in DropDownList using ASP.NET | RequiredFieldValidator

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >
<html>
<body>
    <form id="Form1" runat="server">
        <h4>
            Check for Selected Value</h4>
        <br />
        Select a Value:    
        <asp:DropDownList ID="DropDownList1" runat="server">
         <asp:ListItem></asp:ListItem>
        <asp:ListItem>Some Value</asp:ListItem>
        </asp:DropDownList>
        <br />
        <asp:Button ID="Button1" Text="Check" runat="server" />
        <br />
        <br />
        <asp:RequiredFieldValidator ID="MyRequiredFieldValidator" ControlToValidate="DropDownList1"
             ErrorMessage="Please select a value from doropdown to proceed." runat="server" />
    </form>
</body>
</html>

Sponsored Ad

Development Updates