Sunday, January 30, 2011

Dynamically accessing contols on page using XML in asp.net

1.First create the XML (Page1.xml) like this
<?xml version="1.0" encoding="utf-8" ?>
<RoleMaster>
  <Role RoleType="Admin">
    <Control>
    <ID ControlType="TextBox" AccessType="R">
      txtnname
    </ID>
    </Control>
    <Control>
      <ID ControlType="TextBox" AccessType="W">
        txtpwd
      </ID>
    </Control>
    <Control>
      <ID ControlType="TextBox" AccessType="R">
        txtAdd
      </ID>
    </Control>
    <Control>
      <ID ControlType="TextBox" AccessType="W">
        txtCounty
      </ID>
    </Control>
    <Control>
      <ID ControlType="TextBox" AccessType="W">
        txtPhone
      </ID>
    </Control>
    <Control>
      <ID ControlType="DDL" AccessType="W">
        ddlState
      </ID>
    </Control>
  </Role>
  <Role RoleType="Agent">
    <Control>
      <ID ControlType="TextBox" AccessType="W">
        txtnname
      </ID>
    </Control>
    <Control>
      <ID ControlType="TextBox" AccessType="R">
        txtAdd
      </ID>
    </Control>
    <Control>
      <ID ControlType="TextBox" AccessType="W">
        txtCounty
      </ID>
    </Control>
    <Control>
      <ID ControlType="TextBox" AccessType="R">
        txtPhone
      </ID>
    </Control>
    <Control>
      <ID ControlType="TextBox" AccessType="W">
        ddlState
      </ID>
    </Control>
  </Role>
</RoleMaster>

2.Add the following code in aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Page1.aspx.cs" Inherits="Page1" %>
<!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>Test Page1</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
  <asp:Panel ID="pantest" runat="server">       
    <table cellpadding="0" cellspacing="0" border="0" width="50%" align="center">
        <tr>
            <td>
                User Name</td>
            <td>
                <asp:TextBox ID="txtnname" runat="server" Visible="False"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Password</td>
            <td>
                <asp:TextBox ID="txtpwd" runat="server" Visible="False"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Address</td>
            <td>
                <asp:TextBox ID="txtAdd" runat="server" Visible="False"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                County</td>
            <td>
                <asp:TextBox ID="txtCounty" runat="server" Visible="False"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Phone</td>
            <td>
                <asp:TextBox ID="txtPhone" runat="server" Visible="False"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                State</td>
            <td>
                <asp:DropDownList ID="ddlState" runat="server" Visible="False">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    </asp:Panel>
   
    </div>
    </form>
</body>
</html>
3. Code for accesing XML and making contol visible or false using XML.In the same way you can dymanically create contol on page.

 protected void Page_Load(object sender, EventArgs e)
    {
        // Create instance of XmlDocument class
        XmlDocument XMLRead = new XmlDocument();
        // Load Xml file  
        XMLRead.Load(Server.MapPath("Page1.xml"));                       
        XmlNodeList nodes = XMLRead.SelectNodes(@"RoleMaster/Role");
                foreach (XmlNode node in nodes)
                {  
                    XmlNodeList nodes2 = node.ChildNodes;
                    string RoleTypeCheck = node.Attributes["RoleType"].Value;
                    if (RoleTypeCheck == "Admin")
                    {
                        for (int i = 0, ii = nodes2.Count; i < ii; i++)
                        {
                            XmlNode n = nodes2.Item(i);
                            XmlNodeList n2 = n.ChildNodes;
                            foreach (XmlNode n2a in n2)
                            {
                                string ControlID = n2a.InnerText.Trim();
                                string ControlType = n2a.Attributes["ControlType"].Value;
                                string AccessMode = n2a.Attributes["AccessType"].Value;
                                AddControl(ControlType, ControlID, AccessMode);
                            }
                        }
                    }
                }
    }

  // send any random ID "ControlId",Control Dtype
    public void AddControl(string controlType, string ControlId ,string AccessMode)
    {
        if (controlType == "Label")
        {
            //Label Mynewlabel = new Label();
            //Mynewlabel.ID = ControlId;
            //Mynewlabel.Visible = true;
            //pantest.Controls.Add(Mynewlabel);
            Label LBL = (Label)Page.FindControl(ControlId);
            LBL.Visible = true;
        }

        if (controlType == "TextBox")
        {
            TextBox TXBox =(TextBox) Page.FindControl(ControlId);
            TXBox.Visible = true;
            if (AccessMode=="R")
            {
                TXBox.ReadOnly = true;
            }
            else
            {
                TXBox.ReadOnly = false;
            }
        }
        if (controlType == "DDL")
        {
            DropDownList Mydropdown = (DropDownList)Page.FindControl(ControlId);
            Mydropdown.Visible = true;
        }
    }
}

Friday, January 28, 2011

How to access Default printer or client PC from web application

1. include this javascript in header section of aspx page
 <script language="vbscript" src="Defaultprinter.vbs"  type="text/vbscript"></script>
 <script language="javascript" type="text/javascript">
function printercall()
{
    document.getElementById("<%=Put some ID here%>").value=Printertest();
}
</script>

2.We need to create a vbscript(Defaultprinter.vbs) that will access client pc default printer and call its method from javascript as mention above.In the same way you can access client PC drive as well.

Function Printertest()
Dim strKey, wshShell,printerreturn
' Create WScript Shell object to read the registry
Set wshShell = CreateObject( "WScript.Shell" )
' Read the current default printer from registry
strKey = "HKEY_CURRENT_USER\Software\Microsoft" _
       & "\Windows NT\CurrentVersion\Windows\Device"
   
    'msgbox(strKey)
printerreturn=Trim( Split( wshShell.RegRead( strKey ), "," )(0) )
msgbox (printerreturn)
' Release the Shell object
Set wshShell = Nothing
Printertest=printerreturn
End Function

3. call javascript usually as we call on button click or page load or as per ur wish.

btnPrint.Attributes.Add("OnClick", "printercall()");

Hope this help you.




Monday, January 24, 2011

Javascript for Redirect to login page from pop up of pop up after session out

1.Write this javascript in header section of aspx page.
<script language="javascript" type="text/javascript">
function(mstrLoginURL)
{
window.close();
if(window.opener &&  !window.opener.close)
{
window.opener.location.reload(true);
}
}
</script>

2.write this code in .cs page on page load
if(Session["CID"].ToString() == null )
{
string mstrLoginURL = ResolveUrl("Login.aspx");
Page.ClientScript.RegisterStartupScript(this.GetType(), "aa", "closepopup('" + mstrLoginURL + "')", true);
}


Hope this help you.

Friday, January 21, 2011

Function returning more than one value in SQL Server

Function returning more than one value in SQL Server
Create
FUNCTION GetWarrantNotesDetails (@WarrantID varchar(22))
RETURNS @WarrantDetails Table
( Remarks varchar(1000)
, NoteDate datetime
, AddedBy varchar(36)
)
AS
BEGIN
INSERT INTO @WarrantDetails (Remarks, NoteDate, AddedBy)
SELECT TOP(1) Remarks, NoteDate,[exec].cname
FROM warrant_notes iNNER JOIN [exec] ON
cast(warrant_notes.AddedBy as varbinary) = cast(dbo.[exec].cid as varbinary)
WHERE WarrantID=@WarrantID AND AddedBy<>'Admin' ORDER BY NoteDate DESC
RETURN
END


Function returning one value in SQL Server


Create FUNCTION [dbo].[GetRoleDetais](@RoleID varchar(10)) 
returns varchar(50) 
AS 
BEGIN 
Declare @Roledetails as varchar(50)   
select @Roledetails=RoleTitle  FROM ProfileMaster WHERE ProfileID=@RoleID 
RETURN @Roledetails  
END


Hope this help you.

Thursday, January 20, 2011

Adding hyperlink in Gridview cell and perform operation on it

1  .Aspx page code:

use this javascript for confirm dialog box  in header tab

<script language="javascript" type="text/javascript">
confirmB(status) // Active/Inactive{

{
if(status == "Active")if(window.confirm("Do you really want to Inativate this Admin User?"))return true;else
}
return false;else{if(window.confirm("Do you really want to Activate this Admin User?"))return true;else
}
}
return false;
</script><asp:GridView ID="gvAdmin" runat="server" SkinID="gridviewSkin" DataKeyNames="UserID,isActive" OnRowCommand="gvAdmin_RowCommand" Height="140px" OnPageIndexChanging="gvAdmin_PageIndexChanging" AutoGenerateColumns="False"><FooterStyle BackColor="#6D2C90" ForeColor="White" Height="20px"/><RowStyle BackColor="#E9E6F0" ForeColor="Black" Height="20px" /><AlternatingRowStyle BackColor="#DFD9EB" ForeColor="Black" /><PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" /><SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" /><HeaderStyle BackColor="#6D2C90" Font-Bold="True" ForeColor="White" /><Columns><asp:BoundField HeaderText="Full Name" HeaderStyle-Width="25%" HeaderStyle-Height="25px" DataField="fullname"ItemStyle-HorizontalAlign="Left"><HeaderStyle Width="25%"></HeaderStyle><ItemStyle Wrap="true" /></asp:BoundField><asp:BoundField HeaderText="EMail Address" HeaderStyle-Width="25%" DataField="EMAIL"ItemStyle-HorizontalAlign="Left"><HeaderStyle Width="25%"></HeaderStyle><ItemStyle Wrap="true" /></asp:BoundField><asp:BoundField HeaderText="User Name" HeaderStyle-Width="20%" DataField="LOGINID" ItemStyle-HorizontalAlign="Left"><HeaderStyle Width="20%"></HeaderStyle><ItemStyle Wrap="true" /></asp:BoundField><asp:BoundField HeaderText="Phone/MOBILE1#" HeaderStyle-Width="10%" DataField="MOBILE"ItemStyle-HorizontalAlign="Center"><HeaderStyle Width="10%"></HeaderStyle><ItemStyle Wrap="true" /></asp:BoundField>
<asp:TemplateField HeaderText="Status"><ItemTemplate>
<asp:LinkButton ID="lnkStatus" runat="server" Text='<%#Eval("isActive") %>' CommandArgument='<%#Eval("UserID") +"|"+ Eval("isActive")+"|"+ Eval("CID") %>' CommandName="Activate" OnClientClick='<%#Eval("isActive", "return confirmB(\"{0}\")")%>'></asp:LinkButton>
</ItemTemplate> </asp:TemplateField>
<asp:TemplateField HeaderText="Allow Upload Document"><ItemTemplate>
<asp:LinkButton ID="lnkuploadStatus" runat="server" Text='<%#Eval("UploadDocument") %>' CommandArgument='<%#Eval("UserID") +"|"+ Eval("UploadDocument")+"|"+ Eval("CID") %>' CommandName="Upload" OnClientClick='<%#Eval("UploadDocument", "return confirmupload(\"{0}\")")%>'></asp:LinkButton>
</ItemTemplate> </asp:TemplateField>
</Columns>

2. SQL stored procedure for filling gridview

CREATE
PROCEDURE fillgriedview AS BEGIN SELECT UserID, LOGINID, FIRSTNAME + ' ' + LASTNAME AS fullname, EMAIL, PHONE1 +'/'+ MOBILE1 as MOBILE, CASE ISNULL(ISACTIVE,0) WHEN 0 THEN 'Inactive' ELSE 'Active' END AS isActive,CID,CASE ISNULL(UploadDocument,0) WHEN 0 THEN 'Inactive' ELSE 'Active' END
AS UploadDocument FROM dbo.Users WHERE ROLE IN('Cadmin') ORDER BY FIRSTNAME, LASTNAME DESC END

3. code behind page

// write this on page load
  if (!IsPostBack)

BindGrid()
 private void BindGrid()
{
     write code for filling griedview as per ur need
}
protected  void gvAdmin_RowCommand(object sender, GridViewCommandEventArgs e)

{
if (e.CommandName.Equals("Activate"))
{
// Write code here which will work on clicking gridview cell hyperlink and u can get value of link like this

(e.CommandArgument.ToString().Split('|')[1].ToString().ToUpper() == "ACTIVE") ? 0 : 1;}

Convert.ToInt32(e.CommandArgument.ToString().Split('|')[0].ToString());
}
}

Hope this help you.