Archive

Archive for September, 2009

Displaying Online users using Memebership with Linq

September 27, 2009 Leave a comment

Hi

try this example to display Online users using Memebership with Linq :-

var online = from MembershipUser u in Membership.GetAllUsers()
                  where u.IsOnline == true
                  select u;
GridView1.DataSource = online;
GridView1.DataBind();

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net, Linq Tags:

Printing DIV Contents using Print Preview

September 20, 2009 1 comment

Hi

try this example to Print DIV Contents using Print Preview

<form id="Form1" method="post" runat="server">
 <div id="Div1">
  Printable content
 </div>
 <input type="button" value="Print" onclick="JavaScript:printPartOfPage('Div1');">

<script type="text/javascript">
<!--
function printPartOfPage(elementId)
{
 var printContent = document.getElementById(elementId);
 var windowUrl = 'about:blank';
 var uniqueName = new Date();
 var windowName = 'Print' + uniqueName.getTime();
 var printWindow = window.open(windowUrl, windowName, 'left=50000,top=50000,width=0,height=0');

 printWindow.document.write(printContent.innerHTML);
 printWindow.document.close();
 printWindow.focus();
 printWindow.print();
 printWindow.close();
}
// -->
</script>

</form>

//—— another way

<form id="Form1" method="post" runat="server">
 <div id="Div1">
  Printable content
 </div>
 <input type="button" value="Print" onclick="JavaScript:printPreviewDiv('Div1');">

 <script type="text/javascript">
 <!--
 function printPreviewDiv(elementId)
 {
  var printContent = document.getElementById(elementId);
  var windowUrl = 'about:blank';
  var uniqueName = new Date();
  var windowName = 'Print' + uniqueName.getTime();
  var printWindow = window.open(windowUrl, windowName, 'left=50000,top=50000,width=0,height=0');
  var printPreviewObject = '<object id="printPreviewElement" width="0" height="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>';

  printWindow.document.write(printContent.innerHTML);
  printWindow.document.write(printPreviewObject);
  printWindow.document.write('<script language=JavaScript>');
  printWindow.document.write('printPreviewElement.ExecWB(7, 2);');
  printWindow.document.write('printPreviewElement.outerHTML = "";');
  printWindow.document.write('</script>');
  printWindow.document.close();
  printWindow.focus();
  printWindow.close();
 }
 // -->
 </script>
</form>

 Hope this helps

Good Luck

Categories: ASP.Net, Javascript

Alphabetic Paging with Gridview

September 18, 2009 Leave a comment

Hi

try this example :-

<asp:linkbutton id="btnAll" runat="server" text="ALL" onclick="btnAll_Click" />
 <asp:repeater id="rptLetters" runat="server" datasourceid="sdsLetters">
  <headertemplate>
    |
  </headertemplate>
  <itemtemplate>
   <asp:linkbutton id="btnLetter" runat="server" onclick="btnLetter_Click" text='<%#Eval("Letter")%>' />
  </itemtemplate>
  <separatortemplate>
    |
  </separatortemplate>
 </asp:repeater>
 <asp:sqldatasource id="sdsLetters" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
  selectcommand="SELECT DISTINCT LEFT(ProductName, 1) AS [Letter] FROM [Products]">
 </asp:sqldatasource>
 <asp:gridview id="gvProducts" runat="server" datakeynames="ProductID" datasourceid="sdsProducts"
  style="margin-top: 12px;">
 </asp:gridview>
 <asp:sqldatasource id="sdsProducts" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
  selectcommand="SELECT ProductID, ProductName FROM [Products] ORDER BY [ProductName]">
 </asp:sqldatasource>

and in code behind :-

protected void btnAll_Click(object sender, EventArgs e)
 {
  gvProducts.DataBind();
 }

 protected void btnLetter_Click(object sender, EventArgs e)
 {
  LinkButton btnLetter = sender as LinkButton;
  if (btnLetter == null) { return; }
  sdsProducts.SelectCommand = String.Format(
   "SELECT ProductID, ProductName FROM [Products] WHERE ([ProductName] LIKE '{0}%') ORDER BY [ProductName]",
   btnLetter.Text);
 }

Hope this helps

Good Luck

Categories: ASP.Net Tags:

Disable Edit and Insert and New Buttons in DetailsView according to Roles

September 16, 2009 4 comments

Hi

try this example :-

<asp:TemplateField ShowHeader="False">
    <ItemTemplate>
         <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" Visible='<%# GetShowEditButton() %>'
                             CommandName="Edit" Text="Edit"></asp:LinkButton>
         &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" Visible='<%# GetShowInsertButton() %>'
                             CommandName="New" Text="New"></asp:LinkButton>
         &nbsp;<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False"
                             CommandName="Delete" Text="Delete" Visible='<%# GetShowDeleteButton() %>'></asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>

in your codebehind, you have the following:

   

 protected bool GetShowEditButton()
    {
        return Roles.IsUserInRole("Admin");
    }
    protected bool GetShowInsertButton()
    {
        return Roles.IsUserInRole("Admin");
    }
    protected bool GetShowDeleteButton()
    {
        return Roles.IsUserInRole("Admin");
    }

Hope this helps

Good Luck

Categories: ASP.Net

Check UserName Availability with JQuery using Memebership

September 11, 2009 Leave a comment

Hi

try this example to Check UserName Availability with JQuery using Memebership

First You’ll need to download jquery.js from http://docs.jquery.com/Downloading_jQuery.

The current version is 1.2.6.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script src="scripts/jquery-1.2.6.js" type="text/javascript"></script>
<script type = "text/javascript">
function ShowAvailability() {
    $.ajax({
        type: "POST",
        url: "Default.aspx/CheckUserName",
        data: '{userName: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function(response) {
            alert(response);
        }
    });
}
function OnSuccess(response) {
    var mesg = $("#mesg")[0];

    switch (response) {
        case "true":
            mesg.style.color = "green";
            mesg.innerHTML = "Available";
            break;
        case "false":
            mesg.style.color = "red";
            mesg.innerHTML = "Not Available";
            break;
        case "error":
            mesg.style.color = "red";
            mesg.innerHTML = "Error occured";
            break;                    
    }
}
function OnChange(txt) {
   $("#mesg")[0].innerHTML = "";
}
</script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
         UserName :
    <asp:TextBox ID="txtUserName" runat="server"
        onkeyup = "OnChange(this)"></asp:TextBox>
    <input id="btnCheck" type="button" value="Show Availability"
        onclick = "ShowAvailability()" />
    <br />
    <span id = "mesg"></span>
        </div>
    </form>
</body>
</html>

and in code behind :-

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    [System.Web.Services.WebMethod]
    public static string CheckUserName(string userName)
    {
        string returnValue = string.Empty;
        try
        {
            if (Membership.GetUser(userName) != null)
            {
                returnValue = "false";
            }
            else
            {
                returnValue = "true";
            }
        }
        catch
        {
            returnValue = "error";
        }
        return returnValue;
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Hope this helps

Good Luck

Categories: ASP.Net, Jquery

Show Popup Calender in DIV

September 1, 2009 Leave a comment

Hi

try this example to show Popup calender in DIV using Javascript

<div id="dateField" style="display:none;">
  <asp:Calendar id="calDate"
  OnSelectionChanged="calDate_SelectionChanged"
  Runat="server" />
</div>

<asp:TextBox id="txtDate" Runat="server" />
<img src="cal.png" onclick="popupCalendar()" />

<script type="text/javascript">
  function popupCalendar()
  {
    var dateField = document.getElementById('dateField');

    // toggle the div
    if (dateField.style.display == 'none')
        dateField.style.display = 'block';
    else
        dateField.style.display = 'none';
  }
</script>

in code behind ;-

protected void calDate_SelectionChanged(object sender, EventArgs e)
{
  txtDate.Text = calDate.SelectedDate.ToString("d");
}

Hope this helps

Good Luck

Categories: ASP.Net, Javascript