-->

Wednesday, September 25, 2013

Instance of class cann’t access member of the class within the class

“Clubing data member and member function known as class.”

lets take an Example
Class A
{
Data member;
Member function;
}

If we create instance of the class in other class, that instance cannot access member of the class

lets take an Example

Class B
{
A  obj=new A();
A.Datamember;  // Error  instance member cann’t access datamember within the class
}

Solution :
Every executable statementmust be in a definition block( inside a function , constructor)
Class B
{
A obj =new A();
Public B()
{
Obj.DataMember=value; // you can access the datamember

}

Tuesday, September 24, 2013

How to Use TextBox Control in WPF

The previous controls i.e. Label, TextBlock and etc. are used to display fix text in a window. Their text can be changed but through the programming, not by the user. Now to input some value by the user, or enable text editing, the text box control is used. The textbox control can contain only unformatted text in its text property.

<TextBox Width="200" Height=”25” Text="WPF Textbox control"></TextBox>

The above code will place a textbox control having width two hundred and above text in the textbox. The textbox is shown in the following image:

How to use Single line textbox in WPF XAML


In the above textbox only one line can be written, means we can’t either enter a new line in to the textbox or cant inset more lines by default. To enable multiline, it have two properties to do that i.e. TextWrapping and AcceptReturn. To insert a multiline textbox, write following code:

<TextBox Width="200" Height="40" Margin="5" Text="WPF Textbox control" TextWrapping="Wrap" AcceptsReturn="True"></TextBox>

Now when we run and write some more lines, then it will show something like this:

How to use Multiline textbox control in WPF XAML


In the next post we will enable spell check for this textbox control.

How to Apply style on control at runtime.

CSS introduction

CSS is a new web page layout method that has been added to HTML to give we developer more control over their desing and content layout. CSS allows a designer to create a standard set of commands(either embeded inside web page or from an external page) that controls the style of all subsequent pages.

With CSS you can add style (fonts , spacing, colors, size and links ) to web documents . Its more advanced techniques that controls the layout of the page without the use of tables or other cumbersome HTML.

 Lets take an simple example 


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
    {
        BulletedList1.Style.Add(" background-color", "blue");
        BulletedList1.Style.Add(" font-size", "xx-large;");
    }
 
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Apply style"
            onclick="Button1_Click" Width="154px" />
        <asp:BulletedList ID="BulletedList1" runat="server">
            <asp:ListItem>Apple </asp:ListItem>
            <asp:ListItem>Mango</asp:ListItem>
            <asp:ListItem>Orange</asp:ListItem>
        </asp:BulletedList>
    </div>
    </form>
</body>
</html>

Output
How to Apply style on control at runtime.
How to Apply style on control at runtime.

How to access Html select tag in code file

HtmlSelect Class

The HtmlSelect class creates an HTML select control, which can display either a list box or a drop-down list box depending on its attribute. The items of the select control are stored in <option>elements inside the <select> element. As soon as you add a list box or drop-down list box to a Web form using HTML server controls, ASP.NET adds a default , empty <option> element to the <select> element. The HtmlSelect class gives you access to the HTML <select> element s in server code.

Properties of the HtmlSelect Class

DataMember  : Obtains or sets the set of data to bind to the HtmlSelect control from a DataSource with multiple sets of Data.
DataSource : Obtains or sets the source of information to bind to the HtmlSelect control.
DataSourceId : Obtains or sets the ID of the Data source control that the HtmlSelect control should use to retrieve its data source.
DataTextField : Obtains or sets the field from the data source to bind to the System.Web.UI.WebControls.ListItem.Text property of each item in the HtmlSelect control.
DataValueField : Obtains or sets the field from the data source to bind to the System.Web.UI.WebControls.ListItem.value property of each item in the HtmlSelect control.
InnerHtml : Obtains or sets the conect between the opening and closing tags of the control without automatically converting special characters to their equivalent HTMl entities.
Items : Obtains a collection that contains the item listed in an HtmlSelect control.
Multiple : Obtains or sets a value indicating whether multiple items can be selected concurrently in the HtmlSelect control.
Name : Obtains or sets the unique identifier name associated with the HtmlSelect control.
SelectedIndex : Obtains or sets the ordinal index of the selected item in an HtmlSelect control.
Size : Obtains or sets the height (in row ) of the HtmlSelect control.
Value : Obtains the value of the selected item in the HtmlSelect control or sets the SelectedIndex property of the control to the index of the first item in the list with the specified value.

Event of the HtmlSelect Class


ServerChange : Occurs when the select control is changed. You can handle this event on the server.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void click_server(object server, EventArgs e)
    {
        sp1.InnerHtml = Select1.Value.ToString();
        int i = 0;
        for(i=0;i<Select1 .Items.Count;i++)
        {
            if (Select1 .Items [i].Selected)
            {
                sp2.InnerHtml += Select1.Value.ToString();
            }
        }
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Please Select Item : <br />
        <select id="Select1" runat ="server" >
            <option value ="Apple" selected ="selected">Apple</option>
            <option value ="Mango">Mango</option>
                   <option value ="Orange">Orange</option>
        </select>
        <span runat ="server" id="sp1">
        </span>
        <span runat ="server" id="sp2">
        </span>
        <input id="Submit1" type="submit" value="submit" runat ="server" onserverclick ="click_server" />
    </div>
    </form>
</body>
</html>

Output
How to access Html select tag in code file

How to access Html Image in code in ASP.NET

The HtmlImage class

The HtmlImage Class creates an HTML <img> element, which is used to display images on the web page. Using the properties of this class, you can change the image size and the image displayed as well as the alignment of the image with respect to other HTML elements. The HTML Image class gives you access to the HTML,<img> elements in server code.

Public Properties of the HtmlImage Class

Property
Description
Align
Obtains or sets the image’s alignment with respect to other HTML elements.
Alt
Obtains or sets the alternative text to display if an image cannot be displayed.
Border
Obtains or sets the width of the image’s border
Height
Obtains or sets the image’s height
Src
Obtains or sets the URL of the image
Width
Obtains or sets the image’s width

Lets take an Simple example
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void server_click(Object sender, EventArgs e)
    {
        first.InnerHtml = "Welcome Message";
       
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        Access Html Image in Code page<br />
        <input type="image" runat ="server" src ="submit.jpg" id="image1" onserverclick ="server_click" />
        <div id="first" runat ="server">
        </div>
        <br />
 
    </div>
    </form>
</body>
</html>


Output
How to access Html Image in code in ASP.NET

Sunday, September 22, 2013

How to pass value from one page to another page in ASP.NET

There are various techniques to pass values from one page to another page . These are Session, ViewState, QueryString, findControl , etc
Here we use one by one for passing values from one page to another page.

Use Session: 

Syntax : 
Session["Session variable"] = variable;
variable = Session["Session variable"].Tostring();

Note : Session value store in all page of browser.

Use QueryString 

Syntax : Basically query string pass in URL
http://dotprogramming.blogspot.com/default.aspx?id=7    // here id=7 define query string data

lets take an simple example
Step-1 : Take two Web forms (first.aspx and second.aspx)
Step-2 : first page contain one textbox , one button and one hyperlink control

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["username"] = TextBox1.Text;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Enter Session data :
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Store Session"
            onclick="Button1_Click" />
        <br />
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl ="~/second.aspx?id=7" >QueryString</asp:HyperLink>
        <br />
    </div>
    </form>
</body>
</html>

Step-3 : Second page contains two label control.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "Session data " + Session["username"].ToString();
        Label2.Text = "query string data" + Page.Request.QueryString["id"].ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
        <asp:Label ID="Label2" runat="server" Text=""></asp:Label>

    </div>
    </form>
</body>
</html>


Output
How to pass value from one page to another page in ASP.NET

How to pass value from one page to another page in ASP.NET

How to use LoginView Control in ASP.NET

The LoginView Control

The LoginView control is a Web Server control, which is used to display two different views of a Web page of any website, depending on whether the user has logged on to a Web page as a registered user or a visitor. The LoginView control provides a way of altering the look of the page or showing different content to different groups of users. This control has the built-in functionality to gether the current user's status and roles. If the user is authenticated, the control displays the appropriate information to the user with the help of its three view templates which are:
  • AnonymousTemplate : Displayed when the user is not logged in
  • LoggedInTemplate : Displayed when the user is logged in 
  • RoleGroups : Displayed when the user who has logged in, is a member of a specific role with defined role group templates
Note :  Before apply this example create a user using Administrative toolbar

There are some steps for using LoginView Control in ASP.NET
Step-1: Drop LoginView Control onto the ("Default.aspx") page, In source page a LoginView contol having three template first one is <AnonymousTemplate> template second one is <LoggedInTemplate> and last one is <RoleGroups> template.

Step-2: In <AnonymousTemplate> Either you can write some text or place login control. Here this example contain some text with link.

<AnonymousTemplate>
        please login now
        <a href="login.aspx">Login</a>     
     
        </AnonymousTemplate>
Step-3: In <LoggedInTemplate> you can place LoginName Control or LoginStatus Control with welcome message.
Step-4: Run Your Application

Complete code is 
Default.aspx page code



<%@ 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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:LoginView ID="LoginView1" runat="server">
        <RoleGroups></RoleGroups>
        <AnonymousTemplate>
        please login now
        <a href="login.aspx">Login</a>
     
     
        </AnonymousTemplate>
        <LoggedInTemplate>
        Welcome ,
            <asp:LoginName ID="LoginName1" runat="server" />
            <asp:LoginStatus ID="LoginStatus1" runat="server" />
        </LoggedInTemplate>
        </asp:LoginView>
 
    </div>
    </form>
</body>
</html>

Login.aspx page code



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

<!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>
 
        <asp:Login ID="Login1" runat="server">
        </asp:Login>
 
    </div>
    </form>
</body>
</html>

Output
Anonymous template result in asp.net

login page link open in asp.net

LoggedIn Template result in asp.net

© Copyright 2013 Computer Programming | All Right Reserved