-->

Saturday, September 21, 2013

TextBlock Control Introduction: WPF

According to previous post, Label control derives from Content Control and can: display other type of data as well as string, perform all the functions of a content control. There are some drawbacks in label control such as it cannot support multi line texts.

Textblock control is derived from Framework Element, and can display string type of data only. It has a property TextWrapping which is used to display our text in multiple lines. Means text block can be used to display paragraphs in WPF. A textblock can be drawn in XAML code with below line:

<TextBlock Text="text block" Width="200"/>

It will show the above text in the width specified. If we want to show some more text, then we have to use TextWrapping property of this textblock. If we don’t enable text wrapping then it will only show the text that can be shown in specified width.
<TextBlock Text="Textblock control is derived from Framework
Element, and can display string type of data only.
It has a property TextWrapping which is used to
display our text in multiple lines. Means text
block can be used to display paragraphs in WPF"
Width="200" TextWrapping="Wrap"/>

Now when we run this code, it will show the text written above. The width will be fixed as 200 and height will be increased according to the given text.

How to use TextBlock Control in WPF XAML

If we also specified height here, then the text will not be shown. Just placed the height as 200 here and check out the output as:

Use TextBlock Control with Height in WPF XAML

Friday, September 20, 2013

How to remove selected item from ListBox in ASP.NET


<%@ 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)
    {
        if (ListBox1 .Items .Count !=0)
        {
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListBox ID="ListBox1" runat="server" Height="142px" Width="117px">
            <asp:ListItem>APPLE</asp:ListItem>
            <asp:ListItem>MANGO</asp:ListItem>
            <asp:ListItem>ORANGE</asp:ListItem>
        </asp:ListBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="REMOVE SELECTED" Width="143px" />
    </div>
    </form>
</body>
</html>

Output
Item in listbox in asp.net
How to remove selected item from ListBox in ASP.NET

Clear or Remove item from DropDownList in ASP.NET


<%@ 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)
    {
        DropDownList1.Items.Clear();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" Height="47px" Width="171px">
            <asp:ListItem>Apple </asp:ListItem>
            <asp:ListItem>Mango</asp:ListItem>
            <asp:ListItem>Orange</asp:ListItem>
        </asp:DropDownList>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="CLEAR"
            Width="88px" />
    </div>
    </form>
</body>
</html>

Output
item in dropdownlist in asp.net
Clear or Remove item from DropDownList in ASP.NET


How to create User Profiles in ASP.NET

You have now been familiarized with the basic concept of personalization. In ASP.NET  websites can be personalized by using the concept of user profiles and themes. Themes are used to enhance the look and feel of a Web page , but are incapable of storing and using the information of a specific user for personalization. In ASP.NET  you can use the concept of user profiles to display information of a specific user for personalization. For example , if a user revisits a shopping website , the details of the items purchased in the either visit can also be displayed.
A user profile is a collection of various properties that describes the information you want to store for a specific user. You can store the properties associated with a user in a user profile. The Data that is stored in a profile can be accessed programmatically.

Lets take an Example  How to create User Profiles in ASP.NET

Step-1 :  Write some properties under profile tag in web.config file

<system.web>
      <anonymousIdentification enabled ="true"/>
      <profile>
        <properties >
          <add name ="SName" allowAnonymous ="true"  />
          <add name ="CName" defaultValue ="ASP.NET" allowAnonymous ="true" />
          <add name ="RollNo" defaultValue ="101" allowAnonymous ="true" />
        </properties>
      </profile>
        <compilation debug="false" targetFramework="4.0" />
    </system.web>


Step-2: Write this code under ".aspx" page

<%@ 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)
    {
        Profile.SName = TextBox1.Text;
        Profile.CName = TextBox2.Text;
        Profile.RollNo = TextBox3.Text;
    }
</script>

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

        <br />
        Student Name :<%= Profile .SName %>
        <br />
        Course Name :<%= Profile .CName %>
        <br />
        Student RollNo:<%= Profile .RollNo  %>
        <br />
        Enter Student Name :
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        Enter Course Name :
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        Enter Roll No :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />

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



Default value Output
1. Default value show in output first
How to create User Profiles in ASP.NET
2. Enter value show in output
How to create User Profiles in ASP.NET
3. Close your current output browser window and re-run your application, profile  save the variable value

Thursday, September 19, 2013

Page Life Cycle in ASP.NET


When an ASP.NET page runs, the page goes through a life cycle in which it performs a series of processing steps. These include Initialization, Instantiating controls, restoring and maintaining state, running event handler code, and rendering.

Page Life Cycle in ASP.NET


(1)Page Initialization    = Page_Init
(2)View State Loading = LoadViewState
(3)PostBack data processing = LoadPostData
(4)Page Loading = Page_Load
(5)PostBack Change Notification = RaisePostDataChangedEvent
(6)PostBack Event Handling   = RaisePostBackEvent
(7)Page Pre Rendering Phase  =  Page_PreRender
(8)View State Saving = SaveViewState
(9)Page Rendering  = Page_Render
(10)Page Unloading = Page_UnLoad

How to Bind DataGridView with DataSet in Windows Forms: ADO.NET

In our previous post, I have described about the ways to bind datagridview in windows forms. The first way is to bind the datagridview with dataset and show the data of a table in the gridview. Here is the syntax of binding in c#:
dataGridView.DataSource= ”DataSet Name”;
dataGridView.DataMember=”Table Name”;

Now the time is to perform this code with our existing dataset and table. Drag-n-drop a datagridview on the window and write the following c# code in our form’s constructor:
SqlConnection connection = new SqlConnection();
connection.ConnectionString = "Data Source=(LocalDb)\\v11.0; Initial Catalog=StockDb; Integrated Security=True";
connection.Open();

SqlCommand command = new SqlCommand("select * from Groups", connection);

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(ds);

dataGridView1.DataSource = ds;
dataGridView1.DataMember = ds.Tables[0].ToString();

In the above code, first four lines are same as written in executing sql statements. Fifth line will create a new object of DataSet. Six line will create an object of SqlDataAdapter class which will create an adapter with specified command object.

Next line tell us to fill the dataset with the adapter, which contains all the rows returning by the command object.

Now the last two lines are used to specify the data source of datagridview and the table which will be bind to. When we run the code a window will show with a datagridview containing all the returning rows as shown in following image:

How to bind DataGridView with dataset in Windows Forms ADO.NET

How to Execute Parameterized SQL Statements: ADO.NET

As we know, SQL statements can be of simple type or may be parameterized. The parameterized query contains some parameters, which may be accepted through object of SqlParameter class. SqlParameter class is used to create the parameters used by the command object to execute the Sql queries.
Write the following code to select a record from the table groups which has a given name. The name is passed by the parameter i.e. Sql Parameter.
SqlConnection connection = new SqlConnection();
connection.ConnectionString = "Data Source= (LocalDb)\\v11.0; Initial Catalog=StockDb; Integrated Security=True";
connection.Open();
SqlCommand command = new SqlCommand("select * from Groups where code=@code", connection);
command.Parameters.AddWithValue("@code", 04);
SqlDataReader dr = command.ExecuteReader();

Look out the command object which have a variable name @code. It is the syntax of parameter used. In the next line, the parameter is added by using the function AddWithValue(). This method takes two parameter i.e. one for variable name (same as used in command object) and the second one is its value.

Now when we execute this code, a single record having code = 04 will be returned and can be accessed by data reader object dr.

We can check the rows as the same procedure as in previous post.
© Copyright 2013 Computer Programming | All Right Reserved