To highlight a particular item in a DropDownList

//The following function describes the code to highlight a particular item in a DropDownList. Here i have assumed that that the

DropDownList name is dlSelectedAction

private void HighLight()

{

foreach (ListItem item in dlSelectAction.Items)

{

if (item.Value.Equals(“the index of the item”))

{

item.Attributes.Add(“class”, “categoryItemStyle”);

//Here you need to create a class in your stylesheet (.css file) that has the name “categoryItemStyle” as shown below.

In that class you can specify the background color of your choice using background-color property.

You can use another class name too.

}

}

}

.categoryItemStyle
{
background-color: red;
}

To get records between current date and adding 15 days in current date by stored procedure

//Here i am giving an example of getting the records of users whose birthdays are between current date and adding 15 days in the current date. I am assuming that the field that contains the birthdate is Bdate. You can give your fieldname insted of it.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[Get_UpcomingBirthdays]

AS
BEGIN
select  your fields,(convert (varchar,(select DateName(month,dateadd(month,month(Bdate),0)-1)),103)+’, ‘+ convert (varchar,day(Bdate),103))
as date,
from TableName

where month(Bdate)=month(getdate())
and day(Bdate)between Day(getdate())
and day(dateadd(day,15,getdate()))

To save an image to a specified path with a randomly generated name

//Here i am assuming that the folder in which you want to store images is “UserPhotos” and  File Uploader control name is    UploadPhoto

string Path = Server.MapPath(“UserPhotos”);

Random r = new Random();

string FileName = r.Next().ToString();

string FileExtention = UploadPhoto.FileName.Substring(UploadPhoto.FileName.LastIndexOf(‘.’));
FileName = FileName + FileExtention;
string FilePath = Path + @”\” + FileName;
UploadPhoto.SaveAs(FilePath);
imgPhoto.ImageUrl = FilePath;

To Get MonthName from a Date Field from a table using Stored Procedure

//Assuming that the field name is Bdate

convert (varchar,(select DateName(month,dateadd(month,month(Bdate),0)-1)),103)

To get Current Month name

There are two ways to get the Current Month Name from the Current Date.

1)  Using Globliziation

System.Globalization.DateTimeFormatInfo d = new System.Globalization.DateTimeFormatInfo();
string month = d.MonthNames[DateTime.Now.Month-1];

2) Using new date object as follow

string MonthName = GetMonthName(DateTime.Now.Month);
static string GetMonthName(int monthNum)
{
return GetMonthName(monthNum, false);
}
static string GetMonthName(int monthNum, bool abbreviate)
{
if (monthNum < 1 || monthNum > 12)
throw new ArgumentOutOfRangeException(”monthNum”);
DateTime date = new DateTime(1, monthNum, 1);
if (abbreviate)
return date.ToString(”MMM”);
else
return date.ToString(”MMMM”);
}

To check whether the user’s b’day is in the current month or not using stored prcedure

//Assuming the table name as Users and fieldname as Bdate

select  * from Users
where month(Bdate)=month(getdate())
and day(Bdate)between Day(getdate())
and day(dateadd(day,30,getdate()))

To create your own captcha control

protected void Button1_Click(object sender, EventArgs e)
{
string code = null;
string fcode = null;
int i;
Random randomclass = new Random();

for (i = 0; i < 5; i++)
{
int k = randomclass.Next(1, 35);
code = getcaptcha(k, 1);
fcode = fcode + code;
}
// here i have used the label name lblCaptcha. You can have your own label in your page.

lblCaptcha.Text = fcode;

}

private string getcaptcha(int i, int j)
{
string gcode = “abcdefghikjlmnopqrstuvwxyz123456789″;

string gencode = null;

gencode = gcode.Substring(i, j);
int div = i / 2;
if (div>=1 && div<=10 )
gencode = gencode.ToUpper();
return gencode;

}

To integrate FCKeditor with asp.net

Downloading FCKEditor

You can download the FCKeditor from its official website, http://www.fckeditor.net. The current available version is 2.6.4.

1. Download the ASP.Net version of FCKeditor called FCKeditor.Net from the link http://www.fckeditor.net/download

2. FCKeditor.Net package is just the asp.net control and you need to download the supporting source files from the above location. You can find this in the top 2 links in the same page. Download the zip file (FCKeditor 2.6.4).

Integrating and using in ASP.Net Page

1.      Create a new ASP.Net website in Visual Studio 2005.

2.      Unzip the ASP.Net FCKeditor zip file. You can add the FCKeditor in visual studio toolbox by Right click on the toolbox > Choose Item > Browse to the location where you have unzipped the control. You can find the dll file in location FCKeditor.Net_2.6.3\bin\Release for both 2.0 and 1.1 version of ASP.Net. I have selected the dll from 2.0 directory. You can see the control added to the toolbox.

You can now drag and drop the control in the aspx page.

Below you can find its aspx code generated. You will also find a Register directive on top of the page for the control.

<FCKeditorV2:FCKeditor runat=”server”>

</FCKeditorV2:FCKeditor>

Executing the page now will give you 404 page not found error. It is because; we have not added the FCKEditor source files into our project that we downloaded in Point (2) of the previous section (Downloading FCKEditor). Unzip the file and copy the fckeditor folder into the solution.

We need to specify the root folder of this source files through the BasePath property of FCKEditor.

<FCKeditorV2:FCKeditor BasePath=”fckeditor/” runat=”server”>

</FCKeditorV2:FCKeditor>

To access the text typed in the editor, we can use the server side property Value.

protected void btnSave_Click(object sender, EventArgs e)

{

Response.Write(rtfComments.Value);

}

Execute the page and you can see the FCKeditor appearing the screen.

To fetch the value of a particular cell from the grid

for (int i = 0; i < YourGridName.rows.count; i++)

{

Your VariableName = ((Label)YourGridName.Rows[i].Cells[Cell Index].Controls[Index]).Text;

}

OR

Your VariableName =((Label)YourGridName.Rows[i].Cells[Cell Index].Controls[Index]).Text;

Example:

string temp = ((Label)GridDetail.Rows[i].Cells[3].Controls[1]).Text;


« Older entries

Follow

Get every new post delivered to your Inbox.