Skip to main content

Posts

Showing posts from August, 2008

Creating the List of Drives in the TreeView Control

you can see the code that uses the static GeTDrives method of the DriveInfo class to get a list of all installed drives, then iterates through them. For each fixed, formatted, and available (ready) drive, the code creates a new node containing details of the drive. It then sets the ImageUrl to the custom image, specifies that clicking this node will cause a postback that executes the "populate on demand" event handler, and adds the node to the treeView. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreeView.aspx.cs" Inherits="TreeView" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>Untitled Page</title></head><body><formid="form1"runat="server"><div><asp:TreeViewID…

How to: Create Templated ASP.NET User Controls

Another feature that is often ignored with user controls, but that can be very useful, is the template. Templates are often associated with server controls and are an important part of both the DataList and the Repeater controls. However, their usefulness is not limited to server controls. Templates, however, allow us to provide a means for the page designer to supply HTML content that will be rendered within our control. Templates allow our user controls to be more flexible because they are used across pages within our application.1.In the .ascx file, add an ASP.NET PlaceHolder control where you want the template to appear. 2.In the user control's code, implement a property of type ITemplate. 3.Define a server control class that implements the INamingContainer interface as a container in which to create an instance of the template. This is called the template's naming container. 4.Apply the TemplateContainerAttribute to the property that implements ITemplate and pass t…

How To Display vertical record in GridView

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="VerticalData.aspx.cs" Inherits="VerticalData" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>Untitled Page</title></head><body><formid="form1"runat="server"><div><asp:GridViewID="GridView1"runat="Server"></asp:GridView></div></form></body></html>using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; publicpartialclass VerticalData : System.Web.UI.Page { protectedvoid Page…

Using the RegisterArrayDeclaration Method

Let's create a simple Web page to demonstrate the RegisterArrayDeclaration method. This page provides a slide show where the client-side JavaScript changes the image. In this example, we will search a specified folder on the server and load the names of all the images in a JavaScript array. We will also provide two buttons, one for showing the next image from the list and another for showing the previous one <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ArrayDeclaration.aspx.cs" Inherits="ArrayDeclaration" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>Untitled Page</title><scripttype="text/javascript">var curPic = 0; function processPrevious() { if (curPic==0) …

How to hide and show data in the DataList?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListHide.aspx.cs" Inherits="DataListHide" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>Untitled Page</title></head><body><formid="form1"runat="server"><div><asp:DataListrunat="server"ID="Datalist1"Font-Size="10pt"Font-Name="Verdana"OnItemCommand="Datalist1_ItemCommand"><ItemTemplate><asp:LinkButtonStyle="text-decoration: none"runat="server"ID="btnDetails"Text="+"CommandName="Show"Font-Name="Verdana"/><b><%# DataBinder.Eval(Container.DataItem, "Id") %></b><br/><asp:La…

How To change the background image of html table placed in gridview's template field.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicDiv2.aspx.cs" Inherits="DynamicDiv2" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>Untitled Page</title></head><body><formid="form1"runat="server"><div><asp:GridViewID="GridView1"AutoGenerateColumns="true"runat="Server"OnRowDataBound="GridView1_RowDataBound1"><Columns><asp:TemplateField><ItemTemplate><tableid="myTable"runat="server"width="200px"height="150px"><tr><td><asp:LabelID="lblName"runat="Server"Text='<%#Eval("Name")%>'></asp:Label></td>&l…

Adding/Using Embedded Resources in .Net Web Application/Class Library (C#/VB.Net/ASP.Net)

In .Net when you don’t want to relay for a file on physical location, then it is very good option to take an advantage of Embedded resources. Using embedded resources you can add any file type in the assembly/DLL/EXE when they get compiled. And whenever you want to use it, load it from the assembly, the files get stored in the metadata of Assembly. Here is an example of how to use Embedded Resources. 1. Open Microsoft Visual Studio and create new project for C# Windows Application, here I have created Windows Application named “EmbeddedTest”, even you can use “Class Library” 2. To add a file in the project Right click on the project name in Solution Explorer, select “Add” >> “Existing Item…” 3. Now we have added a file to the project, so it doesn’t mean that it will automatically embedded in the Assembly, for that we need to change the files “Build Action” property to “Embedded Resource”, and compiler will include this file in metatdata. Here I have added Image file(blue.jpg…

How To Use Out Parameter

<%@ Page Language="C#" %><scriptrunat="server">void Page_Load() { int a = 1; int b; Increment(a, out b); lblMessage.Text = b.ToString(); } void Increment(int Number, outint Result) { Result = Number + 1; } </script><htmlxmlns="http://www.w3.org/1999/xhtml"><head><title>Demonstration of using out Parameters</title></head><body><formid="form1"runat="server"><asp:Labelid="lblMessage"runat="server"></asp:Label></form></body></html>

Working with postback of dropdown list inside Gridview

Many a times there are circumstances where by we need to use dropdown list inside a Gridview and also handle the index changed event of the dropdown list. The easy example of this kind of requirement would be when we nee to fill another dropdown list in the same row from the value selected in the first dropdown list. We all know that the dropdown list does not support command name property so you cannot handle the event in the row command event. A simple solution to the problem is to use the namingcontainer in the selectedindexchanged event and get the reference of the parent row view. After that we can do what we want from the row view. Here is an example in the code. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewDropDown.aspx.cs" Inherits="GridViewDropDown" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns=…

How To Create your Own Custom Events

In this post i will show how to extend EventArgs class. 1) Create your custom event args class derived from System.EventArgs 2) Declare your delegate with two parameters, object source and YourEventArgsClass e. 3) Create a protected virtual method that fires the event for you (its protected and virtual so that deriving class can see the method and also override it if they wish to change how and when the event is fired) using System; publicclass MyEventArgs : System.EventArgs { privatebool m_OldValue; privatebool m_NewValue; public MyEventArgs(bool oldVal, bool newVal) { this.m_OldValue = oldVal; this.m_NewValue = newVal; } publicbool OldValue { get { returnthis.m_OldValue; } } publicbool NewValue { get { returnthis.m_NewValue; } } } publicclass MyClass { publicdelegatevoid ValueChangedHandler(object source, MyEventArgs e); publicevent ValueChangedHandler ValueChanged; privatebool m_MyBool = false; public…

UserControl and Event

I will create a new and very simple UserControl, to illustrate how to create events. It won't have a real life purpose, but is only meant to show you how to use events in a UserControl. First, we create a new, simple EventUserControl, with this code in it: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="EventUserControl.ascx.cs" Inherits="EventUserControl" %> Page title: <asp:TextBoxrunat="server"ID="txtPageTitle"/><asp:Buttonrunat="server"ID="btnUpdatePageTitle"OnClick="btnUpdatePageTitle_Click"Text="Update"/>All just text and server controls that we know. In the CodeBehind, it looks a bit like this: using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlCo…