Skip to main content

Posts

Showing posts from July, 2008

Making a small Popup picture on mouse over event

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

<!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">
function get_(div_)
{
div_=div_.id+"1";
document.getElementById(div_).style.display="block";
}
function get_1(div_)
{
div_=div_.id+"1";
document.getElementById(div_).style.display="none";
}
</script>

</head>
<body>
<formid="form1"runat="server">
<div>
<asp:GridViewID="GridView1"runat="Server"AutoGenerateColumns="False">
<Columns>
<asp:BoundFieldDataField="Id"HeaderText="Id"/&…

Enum With String Values In C#

In this post i will show how to assign string value to enum(In C# you cannot have an enum that has string values)
Step1
First I created the new custom attribute class, the source is below:
Step2
Then I created a new which I will use to get the string value for an enums value:



using System;
using System.Data;
using System.Configuration;
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;
using System.IO;
using System.Reflection;
publicclass StringValueAttribute : Attribute
{
publicstring StringValue;

public StringValueAttribute(stringvalue)
{
this.StringValue = value;
}
}
publicstaticclass Util
{
publicstaticstring GetStringValue(Enum value)
{
// Get the type
Type type = value.GetType();

// Get fieldinfo for this type
FieldInfo fieldInfo = type.GetField(value.ToString());
// Get the stringvalue attributes
StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
typeof(Stri…

using arrayList as DataSource for Gridview

Let us suppose that you have four controls (TextBox ,DropDownList ,ListBox ) and your requirement is that when the user enters texts or make a selection all these items/values gets stored inside an ArrayList and then this Array List is used as the Data Source for a Gridview control.


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

<!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:TextBoxID="TextBox1"runat="server"/><br/>
<asp:DropDownListID="DropDownList1"runat="server">
<asp:ListItem>A</asp:ListItem>
<…

Tips

how to resolve the error Oneor more errors encountered while loading the designer. The errors are listed below. Some errors can be fixed by rebuilding your project, while others may require code changes. for more details check out this link

Binding a Listcontrol(Dropdownlist) to Enumeration Values

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

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
publicvoid AddEnumToList(Type GetSystemType, ListControl List)
{
// Populates the specified list with the
// names and values of the passed system type
string[] strNames;
System.Array arrValues;
int intCount;
strNames = Enum.GetNames(GetSystemType);
arrValues = Enum.GetValues(GetSystemType);
List.Items.Clear();
for (intCount = strNames.GetLowerBound(0); intCount < strNames.GetUpperBound(0); intCount++)
{
List.Items.Add(new ListItem(strNames[intCount].ToString(), arrValues.GetValue(intCount).ToStri…

The .NET Replacement for App.Path

A lot of confusion surrounds how to find out the startup path of your application —the .NET equivalent of the App.Path property we had in Visual Basic 6. I’ve personally written my own elongated routines, when in fact the solution is incredibly simple.If you want to find out the application path of your Windows application, just reference the StartupPath property of the Application object, as so:Dim strPath As String = Application.StartupPath Note that the returned path doesn’t include a trailing slash.If you’re developing a class library or similar project, however, you might stumble upon a slight problem. You see, not all projects support the Application object. In these cases, you can use the System.Reflection class to analyze the executing assembly and return its location. A little like this:Dim strPath As String = System.Reflection.Assembly.GetExecutingAssembly().Location A bit more in depth, but still pretty darn simple

How To Post Data From ASP.net To Another URL

using System;
using System.Net;
using System.Web;
using System.Collections.Specialized;


publicclass RemotePost
{
NameValueCollection Inputs = new NameValueCollection();
publicstring Url = "";
publicstring Method = "post";
publicstring FormName = "form1";
publicvoid Add(string name, stringvalue)
{
Inputs.Add(name, value);
}
publicvoid Post()
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write("<html><head>");
HttpContext.Current.Response.Write(string.Format("</head><body onload=\"document.{0}.submit()\">", FormName));
HttpContext.Current.Response.Write(string.Format("<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >", FormName, Method, Url));
int i = 0;
while (i < Inputs.Keys.Count)
{
HttpContext.Current.Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">"…

How To Convert HTML to Text, Easily

Whether you want to convert an HTML page into pure text so you can parse out that special piece of information, or you simply want to load a page from the Net into your own word processing package, this mini function could come in handy.It’s called StripTags and accepts an HTML string. Using a regular expression, it identifies all <tags>, removes them, and returns the modified string. Here’s the code:

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

<!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:TextBoxID="TextBox1"r…

How to View one record per page in ASP.NET?

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

<!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:TextBoxID="txtProductName"runat="server"OnDataBinding="txtDataBind"></asp:TextBox>
<asp:TextBoxID="txtProductid"runat="server"></asp:TextBox>
<asp:ButtonID="btnPrevious"runat="server"Text="Previous"OnClick="PrevBtn"></asp:Button>
<asp:ButtonID="btnNext"runat="server"Text="Next"OnClick="NextBtn"></asp:Butto…

Filling typed dataset programmatically

Filling a typed dataset works just like filling an untyped dataset. You create a data adapter, assign the SelectCommand, and call Fill(). The only real trick is to get the table and field names right. A DataAdapeter will generate table names for each distinct result set generated by the SelectCommand. The names will be "Table", "Table1", "Table2", etc. If you call the overloaded Fill() method which takes a table name parameter, the generated names will follow the same pattern, except your table name will be used instead of "Table". Once the data adapter has generated the table names, it will look for matching table names in your data set. The data is then mapped to the matching column names in the matching tables. As long as the SelectCommand returns the proper column names, the data will populate as you would expect. Some things to beware of:* You will *not* get errors if the table or column names are wrong. If the Data Adap…

How To Clear Browser Session In Asp.net

In this post i will show how to clear Browser history in asp.net.just write down following code in Logout button. protectedvoid LogOut() { Session.Abandon(); string nextpage = "Logoutt.aspx"; Response.Write("<script language=javascript>"); Response.Write("{"); Response.Write(" var Backlen=history.length;"); Response.Write(" history.go(-Backlen);"); Response.Write(" window.location.href='" + nextpage + "'; "); Response.Write("}"); Response.Write("</script>"); }

How to easily insert row in GridView with SqlDataSource?

Asp.Net 2.0 GridView control is very powerful control. Developers at Microsoft really did a great job when designing it. Its like Swiss Army knife for selecting, editing, sorting, paging and displaying data. Combined with various DataSource controls (ObjectDataSource, SqlDataSource) it helps you execute most of your Read, Update and Delete operations without writing a single line of code. Off course, nothing is perfect. For example, creating new data is is one common task that is not so simple to accomplish with GridView. Since there is InsertCommand property in DataSource controls that we can set to insert data, there is no reason not to use it with GridView to insert new data, just like we use DeleteCommand to delete data. For some strange reason there is no built-in way to do this, but there are always workarounds check out this link

Passing Date from Calendar Pop-Up to Parent Window

This section will help us building a simple DatePicker control using Calendar control and Javascript. Create two aspx pages, ParentPage.aspx and Calendar.aspx. Calendar.aspx will contain a Calendar control and it should be opened as a popup window. When the user selects a date in the Calendar popup it will populate the date selected into a textbox in the ParentPage.aspx page.
ParentPage.aspx

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

<!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">
function PopupPicker(ctl)
{
var PopupWindow=null;
PopupWindow=window.open('Calender.aspx?ctl='+ctl,'','width=250,hei…

Working With PopUp In ASP.net

Whenever we build any web applications it can have requirements to work with popup windows. For example: To pick a date for a date field in an aspx page, we can place a calendar control on a popup so that user can select a date from the calendar instead of typing himself, a simple date picker control. This article will help you understand some of tasks that we more often do with popup windows.

Refreshing a parent page from a popup ASP.Net page

There are situations where we will be required to populate the information’s entered on the popups to the parent page and refresh the parent page so that it can be accessed in the server side. The below example will help us in achieving it.

The below example will populate the entered name in the child.aspx(popup) to the parent(Parent.aspx) and submit the page to the server.
Parent.aspx

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

<!DOCTYPEhtmlPUBLIC"-…

The null coalescing operator: ??

This is a new feature of c# 2.0. The null coalescing operator is a short cut for checking if a value is null and if so returning the value of the second operand. Kind of like an IIF. The syntax is as follows string newValue = someValue ?? "default";The first operand someValue must be a nullable type. The above code will set the value of newValue to someValue unless it's null, then it'll set it to "default". You could also use this inline: Console.WriteLine("The value is " + (someValue ?? "null")); another use return (bool)(ViewState["IsPaged"] ?? true);

How To Restrict the number of rows in a dataview when binding to a GridView

In this post i will show how to restrict the number of items bound to an asp:GridView. Basically I never wanted more than 10 items to be displayed.



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

<!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 S…