How to use Master and Content page

March 22, 2013 |

MasterPage.master


  1. <%@ Master Language="C#" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <script runat="server">  
  6.   
  7. </script>  
  8.   
  9. <html xmlns="http://www.w3.org/1999/xhtml">  
  10. <head runat="server">  
  11.     <title>asp.net master page example: how to use master and content page</title>  
  12. </head>  
  13. <body>  
  14.     <form id="form1" runat="server">  
  15.     <div>  
  16.         <h1 style="color:Red">My Site</h1>  
  17.         <hr />  
  18.         <table>  
  19.             <tr valign="top">  
  20.                 <td>  
  21.                     <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">  
  22.                     </asp:ContentPlaceHolder>  
  23.                 </td>  
  24.                 <td>  
  25.                     <asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">  
  26.                     </asp:ContentPlaceHolder>  
  27.                 </td>  
  28.             </tr>  
  29.         </table>  
  30.         <hr />  
  31.         <div style="color:Gray">Page footer</div>  
  32.     </div>  
  33.     </form>  
  34. </body>  
  35. </html>  
ContentPage.aspx
  1. <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" Title="" %>  
  2.   
  3. <script runat="server">  
  4.   
  5. </script>  
  6.   
  7. <asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">  
  8.     <asp:Calendar   
  9.         ID="Calendar1"  
  10.         runat="server"  
  11.         BackColor="Orange"  
  12.         BorderColor="DarkOrange"  
  13.         ForeColor="Snow"  
  14.         >  
  15.         <TitleStyle BackColor="DarkOrange" />  
  16.         <SelectedDayStyle BackColor="Crimson" />  
  17.     </asp:Calendar>  
  18. </asp:Content>  
  19.   
  20. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">  
  21.     <asp:Image   
  22.         ID="Image1"  
  23.         runat="server"  
  24.         ImageUrl="~/Images/sunset.jpg"  
  25.         />  
  26. </asp:Content>  




ContentPage2.aspx

  1. <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" Title="" %>  
  2.   
  3. <script runat="server">  
  4.   
  5. </script>  
  6.   
  7. <asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">  
  8.     <asp:Calendar   
  9.         ID="Calendar1"  
  10.         runat="server"  
  11.         BackColor="DodgerBlue"  
  12.         BorderColor="DarkBlue"  
  13.         ForeColor="Snow"  
  14.         >  
  15.         <TitleStyle BackColor="DarkBlue" />  
  16.         <SelectedDayStyle BackColor="LightBlue" ForeColor="DarkBlue" />  
  17.     </asp:Calendar>  
  18. </asp:Content>  
  19.   
  20. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">  
  21.     <asp:Image   
  22.         ID="Image1"  
  23.         runat="server"  
  24.         ImageUrl="~/Images/sea_beach2.jpg"  
  25.         Width="50%"  
  26.         Height="50%"  
  27.         />  
  28. </asp:Content>  


How to display data in grid view

March 21, 2013 |

DISPLAYSTUDENT.ASPX.CS

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class DisplayUsers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            SqlConnection con = new SqlConnection(DataBase.connect);
            SqlDataAdapter da = new SqlDataAdapter("select * from Stud_Info", con);
            SqlCommandBuilder cm = new SqlCommandBuilder(da);
            DataSet ds = new DataSet();
            da.Fill(ds, "Stud_Info");
            if (ds.Tables["Stud_Info"].Rows.Count > 0)
            {
                GridView1.DataSource = ds.Tables["Stud_Info"];
                GridView1.DataBind();
            }
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      
    }
}

DISPLAYSTUDENT.ASPX

<%@ Page Language="C#" MasterPageFile="~/Hostel.master" AutoEventWireup="true" CodeFile="DisplayStudent.aspx.cs" Inherits="DisplayUsers" Title="DisplayStudent" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            height: 16px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  
    <table class="style1">
        <tr>
            <td align="center" colspan="2">
                <asp:Label ID="Label1" runat="server" Text="Student Details" Font-Size="Medium"></asp:Label>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2" colspan="2">
                <asp:Panel ID="Panel1" runat="server" ScrollBars="Both" Height="471px"
                    Width="682px">
                    <asp:GridView ID="GridView1" runat="server" BackColor="White"
                        BorderColor="#CC9966" BorderStyle="None"
                        BorderWidth="1px" CellPadding="4"
                        Height="467px" Width="682px"
                           onselectedindexchanged="GridView1_SelectedIndexChanged">
                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                        <FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <EditRowStyle BackColor="#999999" />
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    </asp:GridView>
                </asp:Panel>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Back"
                    Width="151px" />
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
   
</asp:Content>





Student Details
 
Stud_IDStud_NameStud_YearStud_AddressStud_CityStud_StateStud_CountryStud_EmailHostelStud_PhoneStud_FeeEntry_DateExit_DateStud_RoomF_EID
500006101Koyyana Venkata SarthakFirstAzad NagarDelhiDelhiIndiakoyyana@ymail.comBravo9050383232Paid3/8/2013 12:00:00 AM3/20/2013 12:00:00 AM45ter@gmail.com
500012328Partha MehrotraSecond New Model townAssamAssamIndiapartha@gmail.comBravo9876543212Paid3/12/2013 12:00:00 AM3/21/2013 12:00:00 AM09gmail@gmail.com
500012717Kshitij DeopaThirdModeltownPanipatHaryanaIndiadeopa@gmail.comBravo9823456654Paid3/7/2013 12:00:00 AM3/18/2013 12:00:00 AM88 
500017072Sachin AgarwalSecondH.No 465 Kirti NagarDelhiDelhiIndiasachin@gmai.comAlpha9723454345Pending  13sachin@gmail.com
500543250SumitFirstPanipatpanipatharyanaSri Lankaesh@gmail.comAlpha3214509876Pending3/14/2013 12:00:00 AM 23 
500543255SumitFirstPanipatpanipatharyanaSri Lankaesh@gmail.comAlpha3214509876Pending3/14/2013 12:00:00 AM 23 
500564399YogeshSecondPanipatpanipatharyanaChinaes@gmail.comCharlie9070575432Pending3/12/2013 12:00:00 AM 45 
500959312RohitFirstDelhiDelhiDelhiIndiarohit@gmail.comDelta9896092048Paid2/25/2013 12:00:00 AM 32 
505967521AnilFirstDelhiDelhiHaryanaIndiaanil@gmail.comAlpha9896097048Pending3/13/2013 12:00:00 AM3/22/2013 12:00:00 AM10 
 


  • RatingOnChangedEvent.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" %>  
  2.   
  3. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>  
  4.   
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  6. <script runat="server">  
  7.     protected void Rating1_Changed(object sender, EventArgs e)  
  8.     {  
  9.         int ratingValue = Rating1.CurrentRating;  
  10.         if (ratingValue <= 3)  
  11.         {  
  12.             Label1.ForeColor = System.Drawing.Color.Red;  
  13.         }  
  14.         else  
  15.         {  
  16.             Label1.ForeColor = System.Drawing.Color.Green;  
  17.         }  
  18.         Label1.Text = "You Rated: " + ratingValue;  
  19.     }  
  20. </script>  
  21.   
  22. <html xmlns="http://www.w3.org/1999/xhtml">  
  23. <head id="Head1" runat="server">  
  24.     <title>Ajax Rating - How to use OnChanged event in asp.net Rating</title>  
  25.     <style type="text/css">  
  26.         .StarCss {   
  27.             background-image: url(/Image/star.png);  
  28.             height:24px;  
  29.             width:24px;  
  30.         }  
  31.         .FilledStarCss {  
  32.             background-image: url(/Image/filledstar.png);  
  33.             height:24px;  
  34.             width:24px;  
  35.         }  
  36.         .EmptyStarCss {  
  37.             background-image: url(/Image/star.png);  
  38.             height:24px;  
  39.             width:24px;  
  40.         }  
  41.         .WaitingStarCss {  
  42.             background-image: url(/Image/waitingstar.png);  
  43.             height:24px;  
  44.             width:24px;  
  45.         }  
  46.     </style>  
  47.   
  48. </head>  
  49. <body>  
  50.     <form id="form1" runat="server">  
  51.     <div>  
  52.         <h2 style="color:DarkBlue; font-style:italic;">  
  53.             ASP.NET Ajax Rating - How to use OnChanged  
  54.             <br /> event in asp.net Rating  
  55.         </h2>  
  56.         <hr width="500" align="left" color="LightBlue" />  
  57.         <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
  58.         </asp:ToolkitScriptManager>  
  59.         <table border="0" cellpadding="4" cellspacing="4">  
  60.             <tr>  
  61.                 <td>  
  62.                     <asp:Image   
  63.                         ID="Image1"  
  64.                         runat="server"  
  65.                         ImageUrl="~/Image/RedBird.jpg"  
  66.                         Height="250"  
  67.                         />  
  68.                 </td>  
  69.                 <td>  
  70.                     <asp:Label   
  71.                         ID="Label1"  
  72.                         runat="server"  
  73.                         Font-Size="X-Large"  
  74.                         Font-Italic="true"  
  75.                         Font-Names="Comic Sans MS"  
  76.                         >  
  77.                     </asp:Label>  
  78.                     <br /><br />  
  79.                     <asp:Label   
  80.                         ID="Label2"  
  81.                         runat="server"  
  82.                         ForeColor="SandyBrown"  
  83.                         Font-Size="Large"  
  84.                         Text="Rate this image"  
  85.                         >  
  86.                     </asp:Label>  
  87.                     <br />  
  88.                     <asp:Rating   
  89.                         ID="Rating1"  
  90.                         runat="server"  
  91.                         StarCssClass="StarCss"  
  92.                         FilledStarCssClass="FilledStarCss"  
  93.                         EmptyStarCssClass="EmptyStarCss"  
  94.                         WaitingStarCssClass="WaitingStarCss"  
  95.                         AutoPostBack="true"  
  96.                         OnChanged="Rating1_Changed"  
  97.                         >  
  98.                     </asp:Rating>  
  99.                 </td>  
  100.             </tr>  
  101.         </table>  
  102.     </div>  
  103.     </form>  
  104. </body>  
  105. </html>  









CreateAndApplyCSSStyle.aspx


  1. <%@ Page Language="C#" AutoEventWireup="true" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <script runat="server">  
  5.     protected void Button1_Click(object sender, System.EventArgs e)  
  6.     {  
  7.         BulletedList1.Style.Add("Padding", "10px 25px 10px 55px");  
  8.         BulletedList1.Style.Add("font-family", "Courier New");  
  9.         BulletedList1.Style.Add("font-size", "x-large");  
  10.         BulletedList1.Style.Add("font-style", "italic");  
  11.         BulletedList1.Style.Add("text-decoration", "underline");  
  12.         BulletedList1.Style.Add("border", "2px dotted darkred");  
  13.         BulletedList1.Style.Add("background-color", "pink");  
  14.         BulletedList1.Style.Add("color", "deeppink");  
  15.     }  
  16. </script>  
  17.   
  18. <html xmlns="http://www.w3.org/1999/xhtml">  
  19. <head id="Head1" runat="server">  
  20.     <title>How to create and apply CSS style programmatically in asp.net control</title>  
  21. </head>  
  22. <body>  
  23.     <form id="form1" runat="server">  
  24.     <div>  
  25.         <h2 style="color:MidnightBlue; font-style:italic;">  
  26.             How to create and apply CSS style  
  27.             <br /> programmatically in asp.net control  
  28.         </h2>  
  29.         <hr width="450" align="left" color="SkyBlue"/>  
  30.         <asp:BulletedList  
  31.              ID="BulletedList1"  
  32.              runat="server"  
  33.              Width="500"  
  34.              BackColor="Crimson"  
  35.              ForeColor="Snow"  
  36.              >  
  37.              <asp:ListItem>Crimson</asp:ListItem>  
  38.              <asp:ListItem>MidnightBlue</asp:ListItem>  
  39.              <asp:ListItem>CornFlower</asp:ListItem>  
  40.              <asp:ListItem>DarkSalmon</asp:ListItem>  
  41.              <asp:ListItem>IndianRed</asp:ListItem>  
  42.              <asp:ListItem>OliveDrab</asp:ListItem>  
  43.         </asp:BulletedList>  
  44.         <asp:Button   
  45.             ID="Button1"  
  46.             runat="server"  
  47.             OnClick="Button1_Click"  
  48.             Text="Create And Apply CSS Style In BulletedList"  
  49.             Height="45"  
  50.             Font-Bold="true"  
  51.             ForeColor="DarkBlue"  />
  52.     </div>  
  53.     </form>  
  54. </body>  
  55. </html>       
  56.                                                                                                                                                    Demo                                



Ajax updatepanel example with triggers in Asp.Net

March 19, 2013 |

Introduction:


In this article I will explain what is Ajax updatepanel control, advantages of updatepanel control and how to use multiple updatepanels in asp.net.

Description:

Previously I explained many articles relating to 
Ajax. Now I will explain what is ajax updatepanel control and use of ajax updatepanel control in asp.net.
During work with our applications if we entered any values in textbox controls and click on a button in form we will see full postback of our page and we will lost all the controls values whatever we entered previously this happend because of postback. If we want to avoid this full postback of page and round trip to server we need to write much code instead of writing much code we can use ajax updatepanel control.
Ajax updatepanel will help us to avoid full postback of the page i.e., avoid refresh of the whole page content with postback and stop flickering of the page which is associated with a postback and allows only partial postbacks. By using Ajax updatepanel we can refresh only required part of page instead of refreshing whole page.
Ajax updatepanel contains property called UpdateMode this property is used to specify whether UpdatePanel is always refreshed during a partial render or if it refresh only when a particular trigger hit. By default updatepanel contains UpdateMode="Always" if we want to set it conditionally we need to change this property UpdateMode="Conditional"
Ajax updatepanel control contains two child tags those are ContentTemplate and Triggers.
a palpable is the antidote for civilization.
ContentTemplate is used to hold the content of the page means suppose we designed page with some controls we will place controls inside of the ContentTemplate
Triggers we used in a situation like need to refresh updatepanel only whenever I click some button control in that situation I will define those controls with this Triggers child tag.
Our Sample update panel control will be like this
<asp:UpdatePanel ID="UpdatePanel2" runat="server"
 UpdateMode="Conditional">
 <ContentTemplate>
 <asp:Label ID="Label2" runat="server"
 ForeColor="red" />
……………………………………………………..
………………………………………………………
……………………………………………………….
 </ContentTemplate>
 <Triggers>
 <asp:AsyncPostBackTrigger ControlID="Button1"
 EventName="Click" />
</Triggers>
 </asp:UpdatePanel>
Now we will create one sample application with updatepanels for that first create application and design your aspx page will be likes this
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head id="Head1" runat="server">
 <title>UpdatePanel Example in asp.net</title>
 </head>
 <body>
 <form id="form1" runat="server">
 <asp:ScriptManager ID="ScriptManager1" runat="server"/>
 <div>
 <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
 <ContentTemplate>
 <fieldset style="width:30%">
 <legend>Update Panel-1</legend>
 <asp:Label ID="lbl1" runat="server" ForeColor="green"/><br />
 <asp:Button ID="btnUpdate1" runat="server" Text="Update Both Panels" OnClick="btnUpdate1_Click" />
 <asp:Button ID="btnUpdate2" runat="server" Text="Update This Panel" OnClick="btnUpdate2_Click" />
 </fieldset>
 </ContentTemplate>
 </asp:UpdatePanel>
 <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
 <ContentTemplate>
 <fieldset style="width:30%">
 <legend>Update Panel-2</legend>
  <asp:Label ID="lbl2" runat="server" ForeColor="red" />
  </fieldset>
 </ContentTemplate>
 <Triggers>
 <asp:AsyncPostBackTrigger ControlID="btnUpdate1" EventName="Click" />
 </Triggers>
 </asp:UpdatePanel>
 </div>
 </form>
 </body>
</html>
If you observe above code in UpdatePanel2 I defined Triggers property with btnUpdate1. Here UpdatePanel2 content will update only whenever we click on btnUpdate1 because we defined UpdatePanel2 property UpdateMode="Conditional" and we set AsyncPostBackTrigger property with btnUpdate1
Write following code in code behind
C#.NET
protected void btnUpdate1_Click(object sender, EventArgs e)
{
lbl1.Text = DateTime.Now.ToLongTimeString();
lbl2.Text = DateTime.Now.ToLongTimeString();
}
protected void btnUpdate2_Click(object sender, EventArgs e)
{
lbl1.Text = DateTime.Now.ToLongTimeString();
lbl2.Text = DateTime.Now.ToLongTimeString();
}
VB Code
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub btnUpdate1_Click(ByVal sender As Object, ByVal e As EventArgs)
lbl1.Text = DateTime.Now.ToLongTimeString()
lbl2.Text = DateTime.Now.ToLongTimeString()
End Sub
Protected Sub btnUpdate2_Click(ByVal sender As Object, ByVal e As EventArgs)
lbl1.Text = DateTime.Now.ToLongTimeString()
lbl2.Text = DateTime.Now.ToLongTimeString()
End Sub


Demo



If you observe above sample whenever I click on button “Update Both Panels” it’s updating data in both updatepanels but if click on button “Update This Panel” it’s updating data in first updatepanel because in both updatepanels we defined condition UpdateMode= "Conditional" and set Triggers conditions because of that here updatepanels will update conditionally and in UpdatePanel2 we written AsyncPostBackTrigger property to update panel only whenver we click on btnUpdate1


<a href="https://plus.google.com/104448558101212792097" rel="publisher">Google+</a>