Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Sunday, 31 January 2016

HOW TO CREATE AN HTML BUTTON ON RUNTIME IN ASP.NET USING C#

Today in Engineer's World, we are discussing ASP.NET & C# query faced by a developer in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS 

Click imagination hunt to see latest Blogs


How to create an HTML button on runtime in Asp.net using C#

Creating a button control by using the drop down functionality is very easy and simple. Also, adding its various properties include – text, style, width, height, weight, events are too simple. But, when the thing is about creating it dynamically. Most developers get nervous and confused. So, here we tried something that may help you out with this query.

In this, Blog we would learn to design an Html Button control dynamically steps by step.

NOTE: See the code understand every step more clearly.

Step-1: Create a New Empty Asp.net Web site. Add a Web Form (Html_Button_Display_Runtime.aspx).
Step-2: Press F7 on the web form. To write server side coding.
Step-3: This step is all about the creation of Html Button control dynamically.
Step-4: This step will tell us how to declare Text property of a button and also how to put CSS (i.e. styling) on Button Text.
Step-5: This step will tell us how to put CSS (i.e. styling) on Html Button.
Step-6: This step will tell us how to add a button click event handler.
            (I): Adding Button click Event Handler
            (II): Adding Button click Event Definition
Step-7: Last step is the most necessary step. This will add/bind the Html Button control on the Page.


Practical Implementation:

View code (Html_Button_Display_Runtime.aspx.cs)

using System;
using System.Web.UI.HtmlControls;

public partial class Html_Button_Display_Runtime : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        /*Step-3: Creating Html Button Control*/
        HtmlButton btn1 = new HtmlButton();

        /*Step-4: Applying CSS (i.e. styling) on Button Text*/
        btn1.InnerHtml = "<strong><font style='color: #808080; font-size:20px;'>Html Button Clicked</font></strong>";   /*----> Changing style for text*/

        /*Step-5: Changing style on Html Button control*/
        btn1.Style.Add("background-color", "black");
        btn1.Style.Add("font-weight", "bold");
        btn1.Style.Add("color", "white");
        btn1.Style.Add("width", "150px");
        btn1.Style.Add("height", "60px");

        /*Step-6(I): Adding Button click Event Handler*/
        btn1.ServerClick+=new EventHandler(btn1_ServerClick);

        /*Step-7: Adding Html Button control to Page*/
        Page.Controls.Add(btn1);               
    }

    /*Step-6(II): Adding Button click Definition*/
    protected void btn1_ServerClick(object sender, EventArgs e)
    {
        Response.Write("<script>alert('Button pressed!')</script>");
    }
}

Debug (CTRL+SHIFT+B) or (F6) the code to check for any compile time error. If not, run the Application by pressing (CTRL+F5). You’ll get your output like this.

OUTPUT:
 
HTML Button creation during Run-time

Similarly, you can prepare your design for Server Button control.


For any query, comment us below.


Related Questions: -


Q-1 Choose the correct output for the following code?

using System;
using System.Web.UI.HtmlControls;

public partial class Html_Button_Display_Runtime : System.Web.UI.Page
{
    private HtmlButton htmBtn = new HtmlButton();

    protected void Page_Load(object sender, EventArgs e)
    {
        htmBtn.InnerText = "click me";
        Page.Controls.Add(htmBtn);
    }
}

A) creates a button control
B) instantiates button control
C) initializes a button control
D) A and B
E) B and C
F) A and C
Ans. Option(D) 

Q-2 What will be the type used for Html Button control?
A) text
B) submit
C) image
Ans. Option(B)

Click imagination hunt to see latest Blogs

Keep learning and sharing…

Friday, 29 January 2016

HOW TO FIND CLIENT IP ADDRESS OF USER IN ASP.NET C#


Today in Engineer's World, we are discussing ASP.NET & C# query faced by a developer in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS 

Click imaginationhunt.blogspot to see latest Blogs


HOW TO FIND CLIENT IP ADDRESS OF USER IN ASP.NET C#

In our previous blog, we have prepared the design. Now, it’s time to create the logic to fetch the IP Address and bind them with label controls.
Now, you are thinking why we have taken four labels? Actually, we have 2 different ways to get the IP Address, that’s why we have taken 2 labels.

Things to Remember: (Before start writing code)

1. “System.Net” Namespace – This namespace provides a simple programming interface for various protocols that used on the network. Read more about namespaces
2. “System.Web” Namespace – This namespace contains types that enable browser/server communication. Read more about namespaces
For e.g. - “HttpContext” is a type that is used to encapsulate all HTTP-specific information about an individual HTTP request.

Practical Implementation:

View Code Behind

using System;
using System.Web;
using System.Net;

public partial class GetIPAddress : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "Label1: " + GetIP1();
        Label2.Text = "Label2: " + GetIP2();
    }

    //Get IPAddress Method 1
    public string GetIP1()
    {
        string ipaddress;
        try
        {
            ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (ipaddress == "" || ipaddress == null)
            {
                ipaddress = Request.ServerVariables["REMOTE_ADDR"];
                return ipaddress;
            }
            return ipaddress;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

    //Get IPAddress Method 2
    protected string GetIP2()
    {
        try
        {
            string VisitorsIPAddr = string.Empty;
            if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
            {
                VisitorsIPAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
            {
                VisitorsIPAddr = HttpContext.Current.Request.UserHostAddress;
            }
            return VisitorsIPAddr;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
}

Debug and Run the application. On the output screen, you may find the fetched IP Address in the respected labels.

Output:



Hence, you can find IP Address by any of these methods. Good Luck!!!


Related Questions:


Q-1 What is DNS?
Ans- DNS – DNS stand for Domain Name System/Server/Service. It is a system which translates or convert the domain name into an IP Address.
For e.g. – DNS translate www.liveviar.com to 182.50.151.47

Q-2 What is the localhost assigned IP for IPV4 and IPV6?
Ans- Localhost means services (i.e. application) and the server is hosted on the same machine and its hosted services can’t be accessed outside.
For IPV4 it is 127.0.0.1
For IPV6 it is 0.0.0.0.0.0.0.1 or ::1

IP Address 127.0.0.1 for IPV4 and 0.0.0.0.0.0.0.1/::1 for IPV6 are most commonly used loopback address.

Q-3 Which namespaces are used for the networking operation in Asp.net C#?
A) System.Net
B) System.Net.Mail
C) System.Web
D) System.Web.Mail
E) All the above mentioned
Ans- Option (A) and (B).

Q-4 How many ports of TCP/IP are reserved for specific protocols?
A) 10
B) 1024
C) 2048
D) 512
Ans- Option (B)

Q-5 How many bits are present in a single IP address?
A) 8
B) 16
C) 32
D) 64
Ans- Option (C)

Click imaginationhunt.blogspot to see latest blogs

Keep learning and sharing…

Thursday, 28 January 2016

HOW TO FIND CLIENT IP ADDRESS OF USER IN ASP.NET C#


Today in Engineer's World, we are discussing ASP.NET & C# query faced by a developer in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS 

Click imaginationhunt.blogspot to see latest Blogs

  
How to find client IP Address of user in Asp.net C#

Finding the User Machine IP is a very basic thing. Why would it become a necessity? It starts when a developer builds an application where they need to track the status of visitor that is viewing its application. At that, time knowing the user Machine IP becomes a matter of security and identifying the user location.

Let’s see some direct way in which we can find the IP Address.

Step-1: Create a New Website. Add a Web Form (GetIPAddress.aspx).
Step-2: Add a new Web Form. Prepare a layout design with 2 Label controls.

Click imaginationhunt.blogspot to see latest blogs

Practical Implementation:

Web Form (Source Code)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GetIPAddress.aspx.cs" Inherits="GetIPAddress" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div style="width:190px; height:180px; border:7px groove; padding-left:20px;">
    <h3 style="color:Orange; font-weight:bold;"><u>GET IP ADDRESS</u></h3>
    <br />
        <asp:Label ID="Label1" runat="server" Text="Label-1" Font-Bold="true" ForeColor="Green"></asp:Label><br />
        <asp:Label ID="Label2" runat="server" Text="Label-2" Font-Bold="true" ForeColor="Chocolate"></asp:Label>
    </div>
    </form>
</body>
</html>

Step-3: Debug(CTRL+SHIFT+B). Check for compile error if any.
Step-4: On Build succeeded. Run(CTRL+F6).
Step-5: See the output window. Both labels are shown.


OUTPUT WINDOW:



Related Questions:


Q-1 What is IP Address?
Ans- IP stands for Internet Protocol. An Internet Protocol Address is a unique address that identifies a machine from other machines on the Internet. This protocol gives us a way to send data from one machine to another via the Internet.

Q-2 What are the possible reason to track IP of the machine?
Ans- There are various possible reasons for tracking IP of client or user Machine:
(i) To maintain security of application from untrusted users and hackers
(ii) Identifying location
(iii) It tells us, which Internet provider and internet service we are using.

Click imaginationhunt.blogspot to see latest blogs

Keep learning and sharing…

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...