Search This Blog

Showing posts with label asp.net. Show all posts
Showing posts with label asp.net. Show all posts

Wednesday, November 5, 2014

HyperLink ASP.NET Example

ASP.NET > System.WebWeb Controls > HyperLink

Displays a link to another Web page.

Events



Example

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/WebForm1.aspx">HyperLink</asp:HyperLink>




Web Controls ASP.NET

ASP.NET > System.Web > Web Controls

Web server controls run on the server.









ASP.NET Hyperlink OnClick Code Behind

ASP.NET > System.Web > Web Controls > > onClick

Script

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

function onClick(obj) {

}
lgt;/script>

C# Code behind

HyperLink1.Attributes.Add("onclick", "return onClick(this)");





Friday, March 28, 2014

ASP.NET System Web UI HtmlControls

ASP.NET > System.Web.UI.HtmlControls

Contains classes that allow you to create HTML server controls on a Web Forms page, so it allows you to programmatically control the HTML elements on a Web Forms page.





Create dynamic table runtime ASP.NET

ASP.NET > System.Web.UI.HtmlControls > HtmlTable 

HtmlTable allows programmatic access on the server to the HTML
element.


Example: Create dynamic table runtime ASP.NET

HTML:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Panel ID="Panel1" runat="server">
        </asp:Panel>
   
    </div>
    </form>
</body>
</html>






Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HtmlTable dTable = new HtmlTable();
            dTable.Border = 1;
            int tRow; 
            int tCell; 
            for (tRow = 0; tRow < 10; tRow++) 
            { 
                HtmlTableRow dTRow = new HtmlTableRow(); 
                for (tCell = 0; tCell < 5; tCell++) 
                { 
                    HtmlTableCell dTCell = new HtmlTableCell(); 
                    dTCell.InnerText = "Row: " + Convert.ToString(tRow + 1) + " Col: " + Convert.ToString(tCell + 1); 
                    dTRow.Controls.Add(dTCell); 
                } 
                dTable.Controls.Add(dTRow); 
            } 
            Panel1.Controls.Add(dTable); 

        }
    }
}




Tuesday, March 4, 2014

HttpResponse ASP.NET

ASP.NET > System.WebHttpResponse 

Encapsulates HTTP response information from an ASP.NET application.





ASP.NET System.Web

ASP.NET > System.Web

System.Web contains classes and interfaces that enable browser-server communication.





Tuesday, February 4, 2014

Using jQuery ajax to call asmx WebService methods

ASP.NET > WebService > jQuery

Using jQuery ajax to call asmx WebService methods

Example:




Code:

WebService1.asmx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace JQuery
{
    ///



    /// Summary description for WebService1
    ///
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string GetCode(string name)
        {
            return "Code for " + name + " is " + "1111";
        }
    }
}

HtmlPage1.html

<!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></title>
        <script src="jquery-2.1.0.min.js" type="text/javascript"></script>
</head>

<body>
    <form id="form1" runat="server">
    <div><br />Access Code</div>
    <div id="code"></div>

    <script type="text/javascript">
        $(document).ready(function () {
            $.ajax({
                type: "POST",
                url: "WebService1.asmx/GetCode",
                data: "name=John",
                dataType: "text",
                success: function (data) {
                    $("#code").html(data);
                }
            });
        });
    </script>
    </form>
</body>
</html>

Friday, January 24, 2014

What is ASP.NET

ASP.NET is a unified Web development model that includes the services necessary for you to build enterprise-class Web applications.





Friday, March 8, 2013

No cache ASP.NET

ASP.NET > HttpCachePolicy > SetCacheability

HttpCachePolicy.SetCacheability Method (HttpCacheability)


Sets the Cache-Control header to one of the values of HttpCacheability.


Example:

Set the Cache-Control header to no-cache.
Response.Cache.SetCacheability(HttpCacheability.NoCache);






Thursday, December 27, 2012

HttpContext.Session ASP.NET

ASP.NET > HttpContext > Session

Gets the HttpSessionState object for the current HTTP request.

ASP.NET pages contain a default reference to the System.Web namespace you can reference the members of HttpContext on an .aspx page without the fully qualified class reference to HttpContext. For example, you can use just Session("USER").

Note: If you want to use the members of HttpResponse from an ASP.NET code-behind module, you must include a reference to the System.Web and also fully qualify the reference.

For example, in a code-behind page you must specify the full name:
 
HttpContext.Current.Session("USER").







Thursday, December 13, 2012

Response.IsClientConnected asp.net

ASP.NET > Web > HttpResponse > IsClientConnected

The IsClientConnected property is a read-only property that indicates if the client has reset the connection to the server.

This property enables you greater control over circumstances where the client may have reset the connection to the server. For example, if a long period of time has elapsed between when a client request was made and when the server responded, it may be beneficial to make sure the client is still connected before continuing with something.

Example

If Not Response.IsClientConnected Then 
  Response.Redirect("login.aspx")
End if

Response.Redirect asp.net

Redirects a client to a new URL. Specifies the new URL and whether execution of the current page should terminate.

Parameters

url
The target location.
endResponse
Indicates whether execution of the current page should terminate.

bool continue;

// some code

if (continue)
{
    Response.Redirect("page.aspx", false);
}
else
{
    Response.End();
}

HttpCookie asp.net

ASP.NET > HttpCookie 

The HttpCookie class gets and sets properties of individual cookies.

HttpCookie loginCookie = Request.Cookies.Get("loginCookie");
if (loginCookie == null)
{
     loginCookie = new HttpCookie("loginCookie");
     loginCookie.Value = txtUsr.Text;
     loginCookie.Expires = DateTime.Now.AddDays(15);
     Response.Cookies.Add(loginCookie);
}





Thursday, November 1, 2012

Session Timeout asp.net web.config

ASP.NET > Session > Timeout

The Timeout property specifies the time-out period assigned to the Session object for the application, in minutes. If the user does not refresh or request a page within the time-out period, the session ends.
The default is 10 minutes.
Should not be lower than 4 minutes an higher than 20 minutes.

<sessionstate timeout="30">





Disable Back Button Interrnet Browser Asp .net

ASP.NETDisable Back Button IE

HttpCacheability.NoCache
Sets the Cache-Control: no-cache header

SetNoStore
Sets the Cache-Control: no-store HTTP header.

Example:

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();






Tuesday, October 9, 2012

Max upload file size in ASP.NET

ASP.NET > MaxRequestLength

The MaxRequestLength property specifies the limit for the buffering threshold of the input stream. For example, this limit can be used to prevent denial of service attacks that are caused by users who post large files to the server.

The default value is 4096 KB (4 MB).

Example: Set max size up to 64 MB in web.config

<httpRuntime
maxRequestLength="65536"
/>





Monday, August 13, 2012

Get Current Url ASP.NET (VB.NET)

ASP.NET > Get Current Url

The ServerVariables collection is used to retrieve the server variable values.

SERVER_NAME: Returns the server's host name, DNS alias, or IP address
SERVER_PORT: Returns the port number to which the request was sent
SCRIPT_NAME: Returns a virtual path to the script being executed

Example: Get Current Url with parameters

Public Shared Function GetCurrentUrl()
' currentUrl
 Dim currentUrl = "http://" &HttpContext.Current.Request.ServerVariables  ("SERVER_NAME").ToString()

 If HttpContext.Current.Request.ServerVariables("SERVER_PORT").ToString() <> "" Then
    currentUrl = currentUrl + ":" + HttpContext.Current.Request.ServerVariables("SERVER_PORT").ToString()
  End If
  currentUrl = currentUrl + HttpContext.Current.Request.ServerVariables ("SCRIPT_NAME").ToString()
 If HttpContext.Current.Request.QueryString.ToString() <> "" Then
   currentUrl += "?" + HttpContext.Current.Request.QueryString.ToString()
 End If
 Return currentUrl
End Function




Monday, July 16, 2012

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

ASP.NET > Errors

This error normally occurs when we use Response.Redirect or Server.Transfer or Response.End in your code before completing the actual process the page was doing.

In this case what you have to do is

In place of Response.End you have to use HttpContext.Current.ApplicationInstance.CompleteRequest

in place of Response.Redirect, use the Response.Redirect ("home.aspx", false)
in place of Server.Transfer, use the Server.Execute method





Tuesday, June 19, 2012

Restart IIS from asp net page

ASP.NET

ServiceController represents a Windows service and allows you to connect to a running or stopped service, manipulate it, or get information about it.
The Internet Information Services (IIS) World Wide Web Publishing Service (W3SVC) manages the HTTP protocol and HTTP performance counters.

Example :
Restart IIS from asp net page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceProcess;
using System.Threading;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ServiceController sc = new ServiceController("W3SVC");
            if (null != sc)
            {
                if (sc.Status == ServiceControllerStatus.Running)
                {
                    sc.Stop();
                    Thread.Sleep(2000);
                    sc.Start();
                    Response.Write("IIS restarted");
                }
                else
                {
                    Response.Write("IIS started");
                    sc.Start();
                }
            }
        }
    }
}