Friday 11 October 2013

How to access Previous Page control in next page

How to access Previous Page control in next page



<form id="form1" runat="server">
        <div>
            First Name:&nbsp;<asp:TextBox ID="FirstName" runat="server"></asp:TextBox><br />
            Last Name:&nbsp;<asp:TextBox ID="LastName" runat="server"></asp:TextBox><br /><br />
            <asp:Button ID="Button1" runat="server" Text="Submit To Destination Page"PostBackUrl="~/CrossPagePostbacks/DestinationPage.aspx" />
        </div>
    </form>


When our user clicks the Submit button, all the values from the HTML Form on SourcePage.aspx will be transfered to the DestinationPage.aspx and we will also be able to get reference to the SourcePage.aspx in our DestinationPage with the PreviousPage property like this:

So in our DestinationPage.aspx.cs code-behind we can easily access two TextBox controls on SourcePage.aspx and show them in two label controls like this:
    protected void Page_Load(object sender, EventArgs e)
    {
        // first check if we had a cross page postback
        if ( (PreviousPage != null) && (PreviousPage.IsCrossPagePostBack) )
        {
            Page previousPage = PreviousPage;
            TextBox firstName = (TextBox)previousPage.FindControl("FirstName");
            TextBox lastName = (TextBox)previousPage.FindControl("LastName");
            // we can now use the values from TextBoxes and display them in two Label controls..
            labelFirstName.Text = firstName.Text;
            labelLastName.Text = lastName.Text;
         }
    }


You probably noticed that we first checked if PreviousPage property of current page (DestinationPage.aspx) is NOT NULL, this is done to avoid running our code in case that user opens our DestinationPage.aspx directly, without running a cross page postback.

Also here we checked the another PreviousPage property called IsCrossPagePostBack to see if we really had a CrossPagePostback.
(If Server.Transfer is used to redirect to this page, IsCrossPagePostBack property will be set to FALSE.

TIP: We can be completely sure that we have a  real CrossPagePostback ONLY IF:
  1. Page.PreviousPage is NOT NULL,
  2. PreviousPage.IsCrossPagePostback is true

Creating new Folder and Upload Image on Server

Creating new Folder and Upload Image on  Server
---------------------------------------------------------------------------------------------------------  
.aspx pages
---------------------------------------------------------------------------------------------------------  

 <table style="text-align: center" width="100%">
                        <tr>
                            <td align="left">
                                <br />
                                Upload picture :
                                <asp:FileUpload ID="fileuploadPic" runat="server" CssClass="flpUploadCss" />
                                <br />
                            </td>
                        </tr>
                        <tr>
                            <td align="center">
                                <br />
                                <center>
                               
                                    <br />
                                    <asp:Button ID="btnchangepic" runat="server" Text="Upload" OnClick="btnchangepic_Click"
                                        CssClass="btnblu_new" Height="25px" Width="80px" />
                                </center>
                            </td>
                        </tr>
</table>



---------------------------------------------------------------------------------------------------------  
.cs  code 
--------------------------------------------------------------------------------------------------------- 
protected void btnchangepic_Click(object sender, EventArgs e)
        {
            try
            {
                bool flag = true;
                if (fileuploadPic.HasFile)
                {
                    string fileExt = Path.GetExtension(fileuploadPic.FileName);
                    if (fileExt == ".jpeg" || fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".png")
                    {
                        flag = true;
                    }
                    else
                    {
                        flag = false;
                        // alert(this, "Upload only - .jpeg,.jpg,.gif,.png files", "");
                    }
                }
                if (flag)
                {
    if (fileuploadPic.HasFile)
                    {
                        long maxFileSize = 4216380;
                        string filename = fileuploadPic.FileName.Replace("'", "''");
             string strPath = Server.MapPath("~/UserImages");  / / it's folder name  on server in which                                    new directory is created

               

                        strPath += "\\" + oDC_User.FName + "-" + oDC_User.UserId;

                        if (!Directory.Exists(strPath))
                        {
                            Directory.CreateDirectory(strPath);
                        }

                        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(strPath);
                        foreach (FileInfo file in dir.GetFiles())
                        {
                            file.Delete();
                        }
                        if (fileuploadPic.PostedFile.ContentLength <= maxFileSize)
                        {
                            strPath = strPath + "\\" + filename;
                            fileuploadPic.SaveAs(strPath);
                        }
                 
                    }
   }
   }
            catch (Exception ex)
            {
           
                }
               
            }
        }

Thursday 10 October 2013

Interview Questions on .net, sql server, asp.net, ado.net for 2 years of experience

What are page directives?

The first line of an ASP.NET page is the page directive; you will find it on all ASP.NET pages. These directives are instructions for the page. It begins with the @Page directive and continues with the various attributes available to this directive.

It’s unreasonable to expect a candidate to know all of these attributes, but a few popular ones include the following.

AutoEventWireup: Indicates whether page events are autowired.
CodeBehind: The name of the compiled class associated with the page.
Debug: Indicates whether the page is compiled in debug mode (includes debug symbols).
EnableTheming: Indicates whether themes are used on the page.
EnableViewState: Indicates whether view state is maintained across pages.
ErrorPage: Specifies a target URL to be used when unhandled exceptions occur.
Language: Indicates the language used when compiling inline code on the page.
Trace: Signals whether tracing is enabled on the page.

What is a master page?

A master page is a template for one or more Web Forms. The master page defines how the page will be laid out when presented to the user, with placeholders for content. The MasterPageFile Page Directive in a content page’s header is one way to assign a master page. The content pages rely solely on content and leave layout to the master page. ASP.NET merges the content with the master page layout when the content page is requested by a user.
 
What is the code behind feature of ASP.NET?


The code behind feature divides ASP.NET page files into two files where one defines the user interface (.aspx), while the other contains all of the logic or code (.aspx.cs for C# and .aspx.vb for VB.NET). These two files are glued together with page directives like the following line, which ties the page to the specific code behind class.

<![CDATA[<%@ Page language="c#" Codebehind="UICode.cs" Inherits="Library.UICode" %>]]>
What are ASHX files?

ASP.NET Web handler files have an .ashx file extension. Web handlers work just like .aspx files except you don’t have to deal with the browser interface, thus no worrying about presentation. Web handlers are generally used to generate content dynamically like returning XML or an image. Web handlers use the IHttpHandler interface with the ProcessRequest() method invoked when the handler is requested. Web handlers are simpler than pages (fewer events and wiring), so they are ideal for performance-critical applications.
 
How does PostBack work?


PostBack is basically the ASP.NET submitting a form to it — it posts back to the current URL. The JavaScript __doPostBack function is placed on the page (look at the source within the browser) to facilitate. PostBack uses ViewState to remember controls and data. The IsPostBack property of the ASP.NET page allows you to determine if the loading of the page is the result of a postback event; this is done in the Page_Load event.

How can you pass values between ASP.NET pages?

There are numerous ways to accomplish this task; the option you choose depends on the environment. The oldest way to approach it is via the QueryString (i.e., passing values via URL); this is also one of the least secure ways because users can easily see the data and could possibly hack the site/page by changing parameter values. Next, you can use HTTP POST to pass values; these are available via a collection within the ASP.NET page. More specific to ASP.NET is the use of Session state, which makes information available to all pages within the ASP.NET application. Another approach is using public properties on the source page and accessing these properties on the target page. Also, you can use the PreviousPage property of the current page to access control information on the referring page. The last two require the source, and target pages are within the same ASP.NET application.

What are ASP.NET Server controls?

ASP.NET includes a number of built-in server controls that are the foundation of its Web programming model. They have various properties to control their behavior and appearance. These controls provide an event model where events are handled on the server (whereas HTML controls are handled in the client). Server controls have the ability to maintain state (via ViewState) across requests, and they can automatically detect the browser. With these controls, you will see the RunAt attribute (RunAt=”Server”) that signals its processing will be done on the server. A good example of these controls is the basic TextBox control (<asp:textbox runat="”Server”" xmlns:asp="#unknown">.
 
What is View State?


Basically, View State is how ASP.NET Web pages persists data across requests. It handles data that must be preserved between postbacks, and you can use it to store page-specific data. By default, View State is enabled on a page and its controls. This can be a problem as the amount of data and controls on a page increases resulting in more data for ASP.NET to maintain. This is accomplished via the hidden __VIEWSTATE field on a form (look at the page source in a browser), so more data in this field means a slower load time and slower overall processing, as it has to be posted to the server each time. You can limit the size of the data in View State by disabling controls that do not need to be persisted via the EnableViewState property. View State can be encrypted to address security concerns.

What is the global.asax file?


The global.asax file is an optional piece of an ASP.NET application. It is located in the root of the application directory structure. It cannot be directly loaded or requested by users. It provides a place to define application- and session-wide events. You can define your own events, but it does contain default Application events like when the application starts Application_Start and ends with Application_End. The same is true for Session events (Session_Start and Session_End).
 
How can you loop through all controls on an ASP.NET Web form?


You can easily traverse all controls on a form via the Web Form’s Controls collection. The GetType method can be used on each control to determine its type and how to work with it. Now, it gets tricky because the form contains a tree of controls; that is, some controls are contained within others (think of a Table). You would have to recursively loop through the controls to make sure everything is processed.
 
What is a web.config file? Machine.config?


The web.config is the basic configuration file for ASP.NET applications. It utilizes an XML format. It is used to define application settings, connection strings, and much more. These files can appear in multiple directories, and they are applied in a top-down approach; that is, configuration files apply to their container directory as well as all directories below it, but the configuration files in lower directories can override those in parent directories. This provides a way to granularly apply settings. The machine.config file contains ASP.NET settings for all of the applications on the server — it is at the top of the configuration file hierarchy, thus web.configs can override it.

Tuesday 8 October 2013

Implement multi language in Website

Implement multi language in Website

paste this code onto your website

Copy and paste the following code snippets onto every page you want to translate
Place this meta tag before the closing </head>
<meta name="google-translate-customization" content="ea148ba41b6b5538-aff83d5b70dcfb8f-g5feaf03ec7e12d8c-12"></meta>

Place this snippet where you'd like to display the Website Translator plugin on your page

<div id="google_translate_element"></div><script type="text/javascript">
function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
}

</script><script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>


Screen shot...
=====================================



Wednesday 18 September 2013

How to convert aspx page to pdf

Convert html to pdf in asp.net

We can  convert HTML or .aspx using iTextSharp liberary.
you can download here iTextSharp.dll.

using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

=================.CS  code=========================================

 protected void LinkButtonDownloadPdf_Click(object sender, EventArgs e)
        {
         
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=PrintTicket.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);

                pnlPerson.RenderControl(hw);

                pnlPerson.Style.Add("text-decoration", "none");
                pnlPerson.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
                pnlPerson.Style.Add("font-size", "8px");
                StringReader sr = new StringReader(sw.ToString());
                Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
                Response.Write(pdfDoc);
                Response.End();
         

        }
==========================.aspx code=================================


            <asp:LinkButton ID="LinkButtonDownloadPdf" runat="server" Text="Download PDF"             OnClick="LinkButtonDownloadPdf_Click" />
         
        </div>

        <asp:Panel ID="pnlPerson" runat="server">
<table width="100%" class="Paratext">
                                <tr>
                                    <td style="width: 15%">
                                        <b>Hotel Name</b>
                                    </td>
                                    <td style="width: 40%">
                                        <asp:Label ID="lblHotel" runat="server"></asp:Label>
                                    </td>
                                    <td style="width: 15%">
                                        <b>Telephone</b>
                                    </td>
                                    <td style="width: 30%">
                                        <asp:Label ID="lblHotelPhone" runat="server"></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td valign="top">
                                        <b>Address</b>
                                    </td>
                                    <td colspan="3">
                                        <asp:Label ID="lblHotelAddress" runat="server"></asp:Label>
                                    </td>
                                </tr>

                            </table>
   </asp:Panel>

Sunday 14 July 2013

How To Customize Ajax Tab CSS

Customize Ajax Tab CSS







<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title><a href="http://www.asp.net/ajaxlibrary/ajaxcontroltoolkitsamplesite/tabs/tabs.aspx">Ajax Tab</a> Customize CSS</title>
    <style>
        /* style sheet for tab*/
     
     
        .TabStyle .ajax__tab_header
        {
            cursor: pointer;
            background-color: #f1f1f1;
            font-size: 14px;
            font-weight: bold;
            font-family: Arial, Helvetica, sans-serif;
            height: 36px;
            border-bottom: 1px solid #bebebe;
            width:650px;
        }
     
        .TabStyle .ajax__tab_active .ajax__tab_tab
        {
            border: 1px solid;
            border-color: #bebebe #bebebe #e1e1e1 #bebebe;
            background-color: #e1e1e1;
            padding: 10px;
            border-bottom: none;
        }
        .TabStyle .ajax__tab_active .ajax__tab_tab:hover
        {
            border: 1px solid;
            border-color: #bebebe #bebebe #e1e1e1 #bebebe;
            background-color: #e1e1e1;
            padding: 10px;
            border-bottom: none;
        }
     
        .TabStyle .ajax__tab_tab
        {
            border: 1px solid;
            border-color: #e1e1e1 #e1e1e1 #bebebe #e1e1e1;
            background-color: #f1f1f1;
            color: #777777;
            cursor: pointer;
            text-decoration: none;
            padding: 10px;
        }
        .TabStyle .ajax__tab_tab:hover
        {
            border: 1px solid;
            border-color: #bebebe #bebebe #e1e1e1 #bebebe;
            background-color: #e1e1e1;
            color: #777777;
            cursor: pointer;
            text-decoration: none;
            padding: 10px;
            border-bottom: none;
        }
        .TabStyle .ajax__tab_active .ajax__tab_tab, .TabStyle .ajax__tab_tab, .TabStyle .ajax__tab_header .ajax__tab_tab
        {
            margin: 0px 0px 0px 0px;
        }
     
        .TabStyle .ajax__tab_body
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 10pt;
            border-top: 0;
            border: 1px solid #bebebe;
            border-top: none;
            padding: 5px;
            background-color: #e1e1e1;
            width:638px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:toolkitscriptmanager id="ToolkitScriptManager1" runat="server">
    </asp:toolkitscriptmanager>
    <asp:tabcontainer activetabindex="0" autopostback="true" cssclass="TabStyle" id="TabContainer1" runat="server" width="100%">
        <asp:tabpanel id="TabPanel1" runat="server" style="background-color: #bebebe;" tabindex="0">
            <headertemplate>
                &nbsp;&nbsp; My Account&nbsp;&nbsp;
            </headertemplate>
            <contenttemplate>
                <div style="background-color: white; height: 200px; width: 638px;">
                    You are in first Tab
                </div>
</contenttemplate>
        </asp:tabpanel>
        <asp:tabpanel id="TabPanel2" runat="server" style="background-color: #e1e1e1;" tabindex="1">
            <headertemplate>
                &nbsp;&nbsp; My Account&nbsp;&nbsp;
            </headertemplate>
            <contenttemplate>
                <div style="background-color: white; height: 200px; width: 638px;">
                    You are in Second Tab
                </div>
</contenttemplate>
        </asp:tabpanel>
        <asp:tabpanel id="TabPanel3" runat="server" style="background-color: #e1e1e1;" tabindex="2">
            <headertemplate>
                &nbsp;&nbsp; My Account&nbsp;&nbsp;
            </headertemplate>
            <contenttemplate>
                <div style="background-color: white; height: 200px; width: 638px;">
                    You are in Third Tab
                </div>
</contenttemplate>
        </asp:tabpanel>
    </asp:tabcontainer>
    </form>
</body>
</html>