Thursday, July 9, 2009

MasterPagePropertiesAccess

In Master page

<div style="height: 40px; background: Yellow;">
      Header<br />
      <div runat="server" id="hotProductsContentContainer">Hot Products</div>
      <div>Menu</div>
      </div>
    <div>
      <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
      </asp:ContentPlaceHolder>
    </div>
    <div style="height: 40px; background: blue;">
      Footer</div>
    </div>


public string HotProductsContent
  {
    get { return hotProductsContentContainer.InnerHtml; }
    set { hotProductsContentContainer.InnerHtml = value; }
  }





//In Content page
protected void Page_Load(object sender, EventArgs e)
{
Master.HotProductsContent = "New Hot Products for Furniture";
}


Source ASP.NET Videos

MultiView

<form id="form1" runat="server">
        <br />
        <strong>MultiView<br />
            <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" RepeatDirection="Horizontal" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
                <asp:ListItem Value="0">Bulleted List</asp:ListItem>
                <asp:ListItem Value="1">File Upload</asp:ListItem>
                <asp:ListItem Value="2">URL Mapping</asp:ListItem>
            </asp:RadioButtonList><br />
            <br />
        </strong>
        <br />
        <asp:MultiView ID="MultiView1" runat="server">
            <asp:View ID="View1" runat="server">
        Bulleted List Control<br />
        <br />
        <asp:BulletedList ID="BulletedList1" runat="server" DataSourceID="XmlDataSource1"
            DataTextField="text" DataValueField="url" DisplayMode="HyperLink">
            <asp:ListItem Value="http://www.microsoft.com">Microsoft</asp:ListItem>
        </asp:BulletedList>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/hyperlinks.xml"></asp:XmlDataSource>
            </asp:View>
            <asp:View ID="View2" runat="server">
        File Upload Control<br />
        <br />
        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" /><br />
        <asp:HyperLink ID="HyperLink1" runat="server">HyperLink</asp:HyperLink></asp:View>
            <asp:View ID="View3" runat="server">
                URL mapping<br />
                <br />
        <a href="guid_{492f3e0b-848e-11da-9550-00e08161165f}.htm">guid_{492f3e0b-848e-11da-9550-00e08161165f}.htm</a><br />
        <br />
        <a href="guid.htm">guid.htm</a>
                <br />
            </asp:View>
        </asp:MultiView>
    </form>

//cODE


protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(("c:\\websites\\tricks\\upload\\" + FileUpload1.FileName));
            HyperLink1.Text = FileUpload1.FileName;
            HyperLink1.NavigateUrl = ("upload\\" + FileUpload1.FileName);
        }
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = Convert.ToInt32(RadioButtonList1.SelectedValue);
    }


Source ASP.NET Videos

PopUpInJavaScript

<script type="text/javascript">
<!--
var updated="";

// http://www.boutell.com/newfaq/creating/windowcenter.html
function wopen(url, name, w, h)
{
  // Fudge factors for window decoration space.
  // In my tests these work well on all platforms & browsers.
  w += 32;
  h += 96;
  wleft = (screen.width - w) / 2;
  wtop = (screen.height - h) / 2;
  // IE5 and other old browsers might allow a window that is
  // partially offscreen or wider than the screen. Fix that.
  // (Newer browsers fix this for us, but let's be thorough.)
  if (wleft < 0) {
    w = screen.width;
    wleft = 0;
  }
  if (wtop < 0) {
    h = screen.height;
    wtop = 0;
  }
  var win = window.open(url,
    name,
    'width=' + w + ', height=' + h + ', ' +
    'left=' + wleft + ', top=' + wtop + ', ' +
    'location=no, menubar=no, modal=yes' +
    'status=no, toolbar=no, scrollbars=no, resizable=no', 'tite=no', 'resizable=no', 'directories=no', 'status=no');
  // Just in case width and height are ignored
  win.resizeTo(w, h);
  // Just in case left and top are ignored
  win.moveTo(wleft, wtop);
  win.focus();
}
// -->
</script>
</head>
<body style="text-align: center">
    Click the Button to Upload Some Files<br />
    <br />
    <input id="AddFileButton" type="button" value="Add File" onclick="wopen('FileUpload.aspx', 'popup', 500, 300); return false;"/>


Source from other Website

SendingMails

Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click

    'Create instance of main mail message class.
    Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()

    'Configure mail mesage
    'Set the From address with user input
    '    mailMessage.From = New System.Net.Mail.MailAddress(txtFromAddress.Text.Trim())
    'Get From address in web.config
    mailMessage.From = New System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings("fromEmailAddress"))
    'Another option is the "from" attirbute in the <smtp> element in the web.config.

    'Set additinal addresses
    mailMessage.To.Add(New System.Net.Mail.MailAddress(txtToAddress.Text.Trim()))
    'mailMessage.CC
    'mailMessage.Bcc
    'mailMessage.ReplyTo

    'Set additional options
    mailMessage.Priority = Net.Mail.MailPriority.High
    'Text/HTML
    mailMessage.IsBodyHtml = False

    'Set the subjet and body text
    mailMessage.Subject = txtSubject.Text.Trim()
    mailMessage.Body = txtBody.Text.Trim()

    'Add one to many attachments
    'mailMessage.Attachments.Add(New System.Net.Mail.Attachment("c:\temp.txt")

    'Create an instance of the SmtpClient class for sending the email
    Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()

    'Use a Try/Catch block to trap sending errors
    'Especially useful when looping through multiple sends
    Try
      smtpClient.Send(mailMessage)
    Catch smtpExc As System.Net.Mail.SmtpException
      'Log error information on which email failed.
    Catch ex As Exception
      'Log general errors
    End Try

  End Sub


and in Web.Config

<appSettings>
    <add key="fromEmailAddress" value="YOUR EMAIL ADDRESS HERE"/>
  </appSettings>


<!--Mail settings-->
  <system.net>
    <mailSettings>
      <smtp>
        <network host="YOUR HOST HERE"/>
      </smtp>
    </mailSettings>
  </system.net>
  <!--Mail settings-->


Source ASP.NET Videos

Update Progress Bar Dynamic Change Label

<asp:UpdateProgress ID="updateProgress" runat="server">

<ProgressTemplate>
<asp:Label ID="LblHolder" runat="server" ForeColor="blue" CssClass="lblMsg">

Please wait while processing...

</asp:Label>

</ProgressTemplate>

</asp:UpdateProgress>

<script language="javascript" type="text/javascript">

var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_initializeRequest(InitializeRequest);

function InitializeRequest(sender, args)
{

// Get a reference to the element that raised the postback,

// and disables it.

$get(args._postBackElement.id).disabled = true;$get('LblHolder).innerHTML= = "This is a test";


}

Validation

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>Validation Sample</title>
  <style>
  body
  {
  font-size: 12pt; font-family: Arial;
  }
  .ValidationMessage
  {
  font-color:red;
  font-weight:bold;
  }
  </style>

  <script language="javascript">
  function isLengthValid(source,args)
  {
  args.IsValid=(args.Value.length >= 8);
  }
  </script>

</head>
<body>
  <form id="Form1" method="post" runat="server">
    <table border="0" cellpadding="2" cellspacing="0">
      <tr>
        <td colspan="3">
          <b>Contact Information</b>
        </td>
      </tr>
      <tr>
        <td align="right" style="height: 27px">
          First Name:
        </td>
        <td align="left" style="height: 27px">
          <asp:TextBox ID="txtFirstName" MaxLength="25" Columns="25" runat="server" />
        </td>
      </tr>
      <tr>
        <td align="right">
          Last Name:
        </td>
        <td align="left">
          <asp:TextBox ID="txtLastName" MaxLength="40" Columns="40" runat="server" />
          <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtLastName"
            CssClass="ValidationMessage" ErrorMessage="Last Name">R</asp:RequiredFieldValidator></td>
      </tr>
      <tr>
        <td align="right">
          Address:
        </td>
        <td align="left">
          <asp:TextBox ID="txtAddress" Columns="50" runat="server" />
        </td>
      </tr>
      <tr>
        <td align="right">
          State:
        </td>
        <td align="left">
          <asp:TextBox ID="txtState" Columns="2" MaxLength="2" runat="server" />&nbsp; Postal
          Code:&nbsp;
          <asp:TextBox ID="txtPostalCode" Columns="10" MaxLength="10" runat="server" />&nbsp;<asp:RegularExpressionValidator
            ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtPostalCode"
            CssClass="ValidationMessage" ErrorMessage="Postal Code" ValidationExpression="\d{5}(-\d{4})?">Postal code must be 5 numeric digits.</asp:RegularExpressionValidator></td>
      </tr>
      <tr>
        <td align="right">
          Phone:
        </td>
        <td align="left">
          <asp:TextBox ID="txtPhone" Columns="20" MaxLength="20" runat="server" />&nbsp;<asp:RequiredFieldValidator
            ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtPhone" CssClass="ValidationMessage"
            Display="Dynamic" ErrorMessage="Phone">R</asp:RequiredFieldValidator>
          <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtPhone"
            CssClass="ValidationMessage" ErrorMessage="Phone" ValidationExpression="(^x\s*[0-9]{5}$)|(^(\([1-9][0-9]{2}\)\s)?[1-9][0-9]{2}-[0-9]{4}(\sx\s*[0-9]{5})?$)">Phone must be in the format: (XXX) XXX-XXXX</asp:RegularExpressionValidator></td>
      </tr>
      <tr>
        <td align="right">
          Date of Birth:
        </td>
        <td align="left">
          <asp:TextBox ID="txtDOB" Columns="10" MaxLength="10" runat="server" />&nbsp;<asp:RequiredFieldValidator
            ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtDOB" CssClass="ValidationMessage"
            Display="Dynamic" ErrorMessage="Date of Birth">R</asp:RequiredFieldValidator>
          <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="txtDOB"
            CssClass="ValidationMessage" Display="Dynamic" ErrorMessage="Date of Birth" ValidationExpression="^\d{1,2}\/\d{1,2}\/\d{4}$">Date must be in the format: mm/dd/yyyy</asp:RegularExpressionValidator>
          <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtDOB"
            CssClass="ValidationMessage" ErrorMessage="Date of Birth" MinimumValue="1850/1/1"
            Type="Date">Date must be between 1850 and today.</asp:RangeValidator></td>
      </tr>
      <tr>
        <td colspan="2" style="height: 23px">
          <b>Log-In</b>
        </td>
      </tr>
      <tr>
        <td align="right">
          Email Address:
        </td>
        <td align="left">
          <asp:TextBox ID="txtEmail" Columns="35" MaxLength="50" runat="server" />&nbsp;
          <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtEmail"
            CssClass="ValidationMessage" Display="Dynamic" ErrorMessage="Email">R</asp:RequiredFieldValidator>
          <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="txtEmail"
            CssClass="ValidationMessage" ErrorMessage="Email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">Not a valid email format. Must be email@host.domain</asp:RegularExpressionValidator></td>
      </tr>
      <tr>
        <td align="right">
          Password:
        </td>
        <td align="left">
          <asp:TextBox ID="txtPassword" TextMode="Password" MaxLength="50" runat="server" />&nbsp;
          <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtPassword"
            CssClass="ValidationMessage" Display="Dynamic" ErrorMessage="Password">R</asp:RequiredFieldValidator>
          <asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server" ControlToValidate="txtPassword"
            CssClass="ValidationMessage" Display="Dynamic" ErrorMessage="Password" ValidationExpression=".*[!@#$%^&*+;:].*">Password must include one of these (!@#$%^&amp;*+;:)</asp:RegularExpressionValidator>
          <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtPassword"
            CssClass="ValidationMessage" ErrorMessage="Password" ClientValidationFunction="isLengthValid" OnServerValidate="CustomValidator1_ServerValidate">Password must be 8 characters of greater.</asp:CustomValidator></td>
      </tr>
      <tr>
        <td align="right">
          Re-enter Password:
        </td>
        <td align="left">
          <asp:TextBox ID="txtPasswordReEnter" TextMode="Password" MaxLength="50" runat="server" />&nbsp;
          <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txtPasswordReEnter"
            CssClass="ValidationMessage" Display="Dynamic" ErrorMessage="Re-enter Password">R</asp:RequiredFieldValidator>
          <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtPassword"
            ControlToValidate="txtPasswordReEnter" CssClass="ValidationMessage" ErrorMessage="Re-enter Password">Passwords do not match. Please re-enter.</asp:CompareValidator></td>
      </tr>
      <tr>
        <td align="center" colspan="2">
          <input id="Submit1" runat="server" type="submit" value="Login"></td>
      </tr>
      <tr>
        <td colspan="2">
          &nbsp;<asp:ValidationSummary ID="ValidationSummary1" runat="server" CssClass="ValidationMessage"
            HeaderText="You must enter a valid value in the following fields:" />
        </td>
      </tr>
    </table>
  </form>
</body>
</html>


In Code :-
protected void Page_Load(object sender, EventArgs e)
  {
    //Set the MaximumValue for the range validator to today's date.
    RangeValidator1.MaximumValue = DateTime.Today.ToString("yyyy/MM/dd");
  }
  protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
  {
    //Password can not be < 8 characters.
    args.IsValid = (args.Value.Length >= 8);
  }

ValidationGroup

<body>

<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" Runat="server" ValidationGroup="First"></asp:TextBox>

<asp:TextBox ID="TextBox2" Runat="server" ValidationGroup="First"></asp:TextBox><br />

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" Runat="server" ValidationGroup="First"
ErrorMessage="TextBox1 should not be blank" ControlToValidate="TextBox1">

</asp:RequiredFieldValidator>
<asp:Button ID="Submit1" Runat="server" ValidationGroup="First" Text="Submit 1" />

<br />
<br />

<asp:TextBox ID="TextBox3" Runat="server" ValidationGroup="Second"></asp:TextBox>

<asp:TextBox ID="TextBox4" Runat="server" ValidationGroup="Second"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" Runat="server" ErrorMessage=" TextBox3 should not be blank"
ControlToValidate="TextBox3" ValidationGroup="Second">
</asp:RequiredFieldValidator>

<asp:Button ID="Submit2" Runat="server" ValidationGroup="Second" Text="Submit 2" />

</div>
</form>
</body>