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

Programmatic DataBinding in Asp.Net


An alternative to declarative databinding, you can programmatically bind any of the List controls to a data source

Example:
Lets create a BulletList which shows the list of Fruits.

<asp:BulletedList
            ID="blFruits"
            runat="server" />

And in Page_Load() event write this code. 

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<String> fruits = new List<string>();
            fruits.Add("Apple");
            fruits.Add("Banana");
            fruits.Add("Mango");
            blFruits.DataSource = fruits;
            blFruits.DataBind();
        }
    }

if(!IsPostBack) prevents the re-initialisation of the BulletList items during postbacks.

Read About Declarative DataBinding : Declarative DataBinding in Asp.Net

Read more...

Declarative DataBinding in Asp.Net


You can bind any of the List controls to a data source. The List controls support both
declarative databinding and programmatic databinding

Example:

Suppose you have a movies table in your database. It has two columns named Id and Title.
<asp:DropDownList
            ID="ddlMovies"
            DataSourceID="srcMovies"
            DataTextField="Title"
            DataValueField="Id"
            runat="server" />

        <asp:SqlDataSource
            ID="srcMovies"
            SelectCommand="SELECT Id, Title FROM Movies"
            ConnectionString="yourConnectionString"
            runat="server" />

The DropDownList control’s DataSourceID property points to the ID of the SqlDataSource control. The SqlDataSource control retrieves the records from the Movies database table. The DropDownList control grabs these records from the SqlDataSource control and creates a ListItem control for each data item. 

The DropDownList control has both its DataTextField and DataValueField properties set. When the DropDownList control creates each of its list items, it uses the values of the DataTextField and DataValueField properties to set the Text and Value properties of each list item.

Read About Programmatic DataBinding :  ProgrammaticDataBinding in Asp.Net

Read more...

Radio Button List in Asp.Net

Radio Button List contains list of Radio Buttons.

Example:
<asp:RadioButtonList
            ID="rblMovies"
            runat="server">
            <asp:ListItem
                Text="The Remains of the Day"
                Value="movie1" />
            <asp:ListItem
                Text="Star Wars"
                Value="movie2" />
            <asp:ListItem
                Text="Pulp Fiction"
                Value="movie3" />
        </asp:RadioButtonList>


Above code contains a RadioButtonList control that contains three ListItem controls that correspond to the three radio buttons. All the List controls use the ListItem control to represent individual list items.

The ListItem control supports the following five properties:

Attributes—Enables you to add HTML attributes to a list item.
Enabled—Enables you to disable a list item.
Selected—Enables you to mark a list item as selected.
Text—Enables you to specify the text displayed by the List Item.
Value—Enables you to specify a hidden value associated with the List Item.
You use the Text property to indicate the text that you want the option to display, and the Value property to indicate a hidden value associated with the option. For example, the hidden value might represent the value of a primary key column in a database table.

The Selected property enables you to show a list item as selected. Selected radio buttons and check boxes appear checked. The selected option in a DropDownList is the default option displayed. Selected options in a ListBox appear highlighted. And in the case of a BulletedList control, the selected property has no effect whatsoever.

The Enabled property has different effects when used with different List controls. When you set a ListItem control’s Enabled property to the value False when using the DropDownList or ListBox controls, the list item is not rendered to the browser. When you use this property with a CheckBoxList, RadioButtonList, or BulletedList control, the list item is ghosted and nonfunctional.
Read more...

Eval() and Bind() Two-Way DataBinding Expressions in Asp.Net

The ASP.NET Framework supports both one-way DataBinding expressions and two-way DataBinding expressions.

In a oneway DataBinding expression, you use the DataBinding expression to display the value of a data item. You use the Eval() method to display the value of a one-way DataBinding expression.

In a two-way DataBinding expression, you not only can display the value of a data item,
you also can modify the value of a data item. You use the Bind() method when working with a    two-way DataBinding expression.

Example:

Suppose you have a movies table in your database. It has columns named Id, Title, Director, DateReleased.

<asp:FormView id="FormView1" DataKeyNames="Id" DataSourceId="srcMovies" DefaultMode="Edit" AllowPaging="true" Runat="server">
<EditItemTemplate>

<asp:Label
id="lblTitle"
Text="Title:"
AssociatedControlID="txtTitle"
Runat="server" />

<asp:TextBox
id="txtTitle"
Text='<%#Bind(“Title")%>'
Runat="server" />
<br />

<asp:Label
id="lblDirector"
Text="Director:"
AssociatedControlID="txtDirector"
Runat="server" />

<asp:TextBox
id="txtDirector"
Text='<%#Bind(“Director")%>'
Runat="server" />
<br />

<asp:Button
id="btnUpdate"
Text="Update"
CommandName="Update"
Runat="server" />
</EditItemTemplate>
</asp:FormView>

<asp:SqlDataSource id="srcMovies" ConnectionString="yourConnectionString"
SelectCommand="SELECT Id, Title,Director,DateReleased FROM Movies"
UpdateCommand="UPDATE Movies SET Title=@Title,
Director=@Director WHERE Id=@Id"
Runat="server" />

The FormView contains an EditItemTemplate. The EditItemTemplate contains three TextBox controls. Each TextBox control has a two-way DataBinding expression assigned to its Text property.

The DataBinding expressions associate the TextBox control properties with the properties of the data item being edited. When you click the Update button, any changes you make to the Text properties are updated in the Movies database table.


Read more...

DataBinding Expressions in Asp.Net

A DataBinding expression is a special type of expression not evaluated until runtime. You mark a databinding expression in a page by wrapping the expression in opening <%# and closing %> brackets.

A DataBinding expression isn’t evaluated until a control’s DataBinding event is raised.

When you bind a DataBound control to a DataSource control declaratively, this event is raised automatically. When you bind a DataSource control to a data source programmatically, the DataBinding event is raised when you call the DataBind() method.

Example:

Suppose you have a movies table in your database. It has two columns named "Title" and "DateReleased".

<asp:DataList id="DataList1" DataSourceId="srcMovies" Runat="server">
    <ItemTemplate>
    <b>Movie Title:</b> <%#Eval("Title")%>
    <br />
    <b>Date Released:</b> <%#Eval("DateReleased", "{0:D}") %>
    <hr />
    </ItemTemplate>
    </asp:DataList>

<asp:SqlDataSource
id="srcMovies"
ConnectionString="your connection string"
SelectCommand="SELECT Title,DateReleased FROM Movies"
Runat="server" />


The first DataBinding expression displays the title of the movie and the second DataBinding expression displays the date the movie was released. Both DataBinding expressions call the Eval() method. The Eval() method is a protected method of the Page class. Behind the scenes, the Page.Eval() method calls the static
(shared) DataBinder.Eval() method. If you want to be verbose, instead of using the Eval() method, you could use the following two expressions:

<%# DataBinder.Eval(Container.DataItem, “Title”) %>
<%# DataBinder.Eval(Container.DataItem, “DateReleased”, “{0:D}” ) %>

In ASP.NET version 1.x, you had to use DataBinder.Eval() when displaying data items in a template.
You can use <%# FormatTitle(Eval(“Title”)) %>. This method formats each of the titles displayed by the Repeater control by making each title bold and uppercase

Note: Technically, the Eval() method uses reflection when evaluating the data item to find
a property with a certain name. You do pay a performance penalty when you use
reflection.

As an alternative, you can improve the performance of your DataBinding expressions by casting the data items to a particular type like this:

<%# ((System.Data.DataRowView)Container.DataItem)[“Title”] %>

Read more...

FormView: Change Mode of FormView dynamically

FormView :

<asp:FormView runat="server" ID="formViewContactInformation" DataKeyField="ID"  DataSourceID="dsContactInformation"> </asp:FormView>

1. To change the mode of FormView to Edit Mode, you have to call ChangeMode method of FormView and pass the mode.

              formViewContactInformation.ChangeMode(FormViewMode.Edit);

2. To change the mode of FormView to Insert Mode

            formViewContactInformation.ChangeMode(FormViewMode.Insert);

3. To change the mode of FormView to ReadOnly Mode

            formViewContactInformation.ChangeMode(FormViewMode.ReadOnly);


Read more...

Understanding MSIL, CLR and JIT

MSIL = Microsoft Intermediate Language
CLR = Common Language Runtime
JIT = Just In Time compiler

MSIL is the "machine code like" language that the C# compiles your code into.

The CLR is the "machine" that the MSIL runs on.

When runinng the MSIL the CLR converts your code into real machine code using the JIT.

In this context just in time stands for that only the MSIL needed for the moment is beeing compiled into machine code.

The real machine code is run on yor computers actual CPU.
CodeExecution cycle



Read more...

WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive)

You need a web.config appSetting key to enable the pre 4.5 validation mode.

Specifies how ASP.NET globally enables the built-in validator controls to use unobtrusive JavaScript for client-side validation logic.

Type: UnobtrusiveValidationMode
Default value: None

Remarks: If this key value is set to "None" [default], the ASP.NET application will use the pre-4.5 behavior  JavaScript inline in the pages) for client-side validation logic. If this key value is set to "WebForms",  ASP.NET uses HTML5 data-attributes and late bound JavaScript from an added script reference for client-side validation logic.

Add this to your web.config :
    <appSettings>
      <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
    </appSettings>
Read more...