Access HeaderTemplate and FooterTemplate Controls

How to access controls in  HeaderTemplate and FooterTemplate

We are using ASP .NET, C# here in this example.

Lets take this repeater as an example. 

<asp:Repeater ID="rpt" runat="server">
    <HeaderTemplate>
       <asp:CheckBox ID="chk1" runat="server" />
    </HeaderTemplate>
    <ItemTemplate>
       HELLO
    </ItemTemplate>
    <FooterTemplate>
      
<asp:CheckBox ID="chk2" runat="server" />
    </FooterTemplate>
</asp:Repeater>

 

You can access the header and footer item controls on ItemCreated or even ItemDataBound. But not when you iterate through the repeater items like this:

foreach(RepeaterItem ri in rpt.Items)

{
    if (ri.ItemType == ListItemType.Header)
    {
        CheckBox chk1 = (CheckBox)ri.FindControl("chk1");

       chk1.Checked=true;
    }
    if (ri.ItemType == ListItemType.Footer)
    {
        CheckBox chk2 = (CheckBox)ri.FindControl("chk2");

       chk2.Checked=true;
    }
}

 

This will simply not work.

Without going through the explanation why it does not work, let see how we can get it work.

If you have a footer and header template defined in the repeater, header is item with index 0 and footer is the last item. Bearing that in mind, we can simply access the header and footer controls as follows:

CheckBox chk1 = (CheckBox)rpt.Controls[0].FindControl("chk1");
chk1.Checked = true;
CheckBox chk2 = (CheckBox)rpt.Controls[rpt.Controls.Count - 1].FindControl("chk2");
chk2.Checked = true;

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: WebCosmo
Posted on: 7/17/2008 at 11:08 AM
Categories: Programming / Coding
E-mail |  Stumble it! |  Propeller it! |  Digg it! |  del.icio.us |  Technorati
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Programmer's Forums

Here is a small list of programmers forums; incase you need them:

http://www.vbforums.com
http://www.vbcity.com/forums
http://www.programmingtalk.com
http://www.codecomments.com
http://www.codeguru.com/forum
http://www.daniweb.com/forums
http://forums.devshed.com
http://www.gidforums.com
http://forums.microsoft.com/msdn/default.aspx?siteid=1
http://www.developerfusion.co.uk/forums
http://www.gamedev.net/community/forums
http://www.codeproject.com/script/Forums/view.aspx

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: WebCosmo
Posted on: 5/2/2008 at 5:46 AM
Categories: Programming / Coding
E-mail |  Stumble it! |  Propeller it! |  Digg it! |  del.icio.us |  Technorati
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Reindexing Microsoft SQL Databases to Optimize Performance

Generally the indexing is done automatically in MS SQL database. If you need to reindex the database for any reason, you can use the tips here.

First of all backup the databse before starting the reindexing. So anything goes wrong, you can get your DB back.

We will use DBCC DBREINDEX, a helpful database maintenance command available for defragmenting indexes in Microsoft SQL Server. DBCC DBREINDEX can be used to rebuild one or more indexes for a specific table.

DBCC DBREINDEX locks the tables as it is operating on, tables would be unavailable to users when this command runs.

Primary Key or Unique constraints are preserved automatically during the rebuild. DBCC DBREINDEX completely rebuilds the indexes, so it restores the page density levels to the original fill factor (default). However, if you prefer, you can choose another target value for the page density. Running DBCC DBREINDEX is similar to using Transact-SQL statements to drop and re-create the indexes manually.

To reindex all the indexes in a Microsoft SQL database, follow these steps:

1. On the Start menu, point to Programs, point to Microsoft SQL Server, and then click Query Analyzer.

2. In the Connect to SQL Server dialog box, click OK.

3. On the Query menu, click Change Database.

4. In the Select Database of <ServerName> dialog box, click the Microsoft CRM database that you want to work on, and then click OK.

5. In the Query window, type the following commands:

DECLARE @TableName varchar(255)
DECLARE TableCursor CURSOR FOR
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'base table'
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'Reindexing ' + @TableName
DBCC DBREINDEX(@TableName,' ',90)
FETCH NEXT FROM TableCursor INTO @TableName
END
CLOSE TableCursor
DEALLOCATE TableCursor

6. Click the Execute Query button on the toolbar, and the results will show in the results pane.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: WebCosmo
Posted on: 3/11/2008 at 5:37 AM
Categories: Programming / Coding
E-mail |  Stumble it! |  Propeller it! |  Digg it! |  del.icio.us |  Technorati
Post Information: Permalink | Comments (1) | Post RSSRSS comment feed

Solution: Css Hover Not Working in FireFox

You may have seen this weired problem; css hover for font not working in FireFox. However it works fine in IE.

I will give you an example. Lets say this is my document code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<style type="text/css">
.default { font-size: 12px; font-family: Verdana, Arial; color: #656565; }
a.default:link { text-decoration: none; color: #656565; }
a.default:hover { text-decoration: none; color: #017812; }
a.default:active { text-decoration: none; color: #656565; }
a.default:visited { text-decoration: none; color: #656565; }
</style>
</head>
<body>
<div class="default">
<a href="index.html" class="default">products</a>
</div>
</body>
</html>

Funny enough this doesn't work with Firefox.

Solution?
Make sure to put the hover style comes after the active and visited style.
So the refined code that works is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<style type="text/css">
.default { font-size: 12px; font-family: Verdana, Arial; color: #656565; }
a.default:link { text-decoration: none; color: #656565; }
a.default:active { text-decoration: none; color: #656565; }
a.default:visited { text-decoration: none; color: #656565; }
a.default:hover { text-decoration: none; color: #017812; }
</style>
</head>
<body>
<div class="default">
<a href="index.html" class="default">products</a>
</div>
</body>
</html>

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: WebCosmo
Posted on: 2/21/2008 at 3:43 PM
Categories: Programming / Coding
E-mail |  Stumble it! |  Propeller it! |  Digg it! |  del.icio.us |  Technorati
Post Information: Permalink | Comments (1) | Post RSSRSS comment feed

Microsoft Access Error: The field is too small to accept the amount of data you attempted to add

ERROR: The field is too small to accept the amount of data you attempted to add.

I generally work with Microsoft SQL database. This time I got into a situation when I have to work with Microsoft Access. I did very little work with Access before, so I was concerned at the begining.  Later as I started working I found it's not that difficult, lot of syntax matches with MS SQL with some differences of course.

Anyways when I was working with a situation when I had to insert big text into the Access table I got the error "The field is too small to accept the amount of data you attempted to add. " After looking at the helps I found its max limit is 255 characters. You can change the max limit with Field Size property. In my case this field size would not work, since I have to insert big chunk of text. Then I found if you change the field type as Memo it can take upto 63,999 characters which works for me. One problem is Memo data types can not be indexed for performance. If you ever got into this error, you know the solution now.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: WebCosmo
Posted on: 1/10/2008 at 7:37 AM
Tags:
Categories: Programming / Coding
E-mail |  Stumble it! |  Propeller it! |  Digg it! |  del.icio.us |  Technorati
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Solution for generic GDI Error

A generic error occurred in GDI+

I know how frustrating it can be debugging this problem. One case when it occurs is when you try to save an image created from a Stream e.g. using Bitmap Save function.

Few things to consider overcoming this error:

- Make sure you allow the write permission on the folder where image is being saved.

- Release resources after saving the image. For example if you create a Bitmap object from a stream dispose resource by bmp.Dispose() or similar syntax

I will suggest using a try catch finally block like this (I am using C# here):

Bitmap bmp = null; 

try

{

     bmp = new Bitmap(myPostedFile.InputStream, false);

    //save image, do things here 

}

catch

{

}

finally

{

    if (bmp != null)
    {
        bmp.Dispose();
    } 

Was it Enough to Solve this Error?

In my case it wasn't able to solve the problem.

Solution 

Redirect after every the save reloading the page again as fresh. This way it will make sure all the resources being hold will be released and reloaded. Sounds stupid right? In my case it solved the problem. 

Example C# Redirect Code: Response.Redirect("MyPage.aspx",false); 

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: WebCosmo
Posted on: 12/24/2007 at 6:12 AM
Categories: Programming / Coding
E-mail |  Stumble it! |  Propeller it! |  Digg it! |  del.icio.us |  Technorati
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Create High Quality Thumbnail - Resize Image Dynamically - ASP .Net C# Code

This article will help you create high quality thumbnail from an image as well as resize image dynamically.

GetThumbnailImage method of class Bitmap produces a thumbnail image from the file specified. Life is so easy with it. Not always though. Sometimes the thumbnail produced has low quality - pixelated and blurred.

Why it Happens? Why My Images are Pixelated and Blurred?

Image formats like jpeg may store the thumbnail inside the same file. If we use System.Drawing.Bitmap method GetThumbnailImage, method checks if there’s a thumbnail image stored into the file and, if the thumb is found, it returns that thumbnail version scaled to the width and height you requested. If the thumbnail version of the image is smaller then the size you requested to produce, thats when problem occurs. The thumbnails produced become pixelated as we know stretching an image to a larger once reduces the Image Quality.

Solution:

Solution is really simple. We will load the source image to a System.Drawing.Image object, scale it to the desired width and height preserving good quality and save it to a new file.

Step by Step Procedure to Create a Good Quality Thumbnail:

For this example lets assume these variables

String src="c:/myImages/a.jpg";   //absolute location of source image

String dest="c:/myImages/a_th.jpg";    //absolute location of the new image created(thumbnail)

int thumbWidth=132;    //width of the image (thumbnail) to produce

The code snippest shown here is in ASP .NET C#.

You have to use these namespaces

using System.Drawing; 

using System.Drawing.Drawing2D;

Following are the Steps along with example code:

- Get the source image to a System.Drawing.Image object
System.Drawing.Image image = System.Drawing.Image.FromFile(src); 


-  Create a System.Drawing.Bitmap with the desired width and height of the thumbnail. 
int srcWidth=image.Width;
int srcHeight=image.Height; 

//we will get the sizeratio in decimal so that we dont get exception in case width is less then height.

Decimal sizeRatio = ((Decimal)srcHeight/srcWidth);
int thumbHeight=Decimal.ToInt32(sizeRatio*thumbWidth);
Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);


- Create a System.Drawing.Graphics object from the Bitmap which we will use to draw the high quality scaled image
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);


- Set the System.Drawing.Graphics object property SmoothingMode to HighQuality
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;


- Set the System.Drawing.Graphics object property CompositingQuality to HighQuality
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;


- Set the System.Drawing.Graphics object property InterpolationMode to High
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;


- Draw the original image into the target Graphics object scaling to the desired width and height
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);


- Save to destination file
bmp.Save(dest);


- dispose / release  resources
bmp.Dispose();
image.Dispose();           

Lets help each other:

I get emails often asking for help with the code. I always love to do that. Unfortunately sometimes I get too busy. I have a full-time job, maintain two sites, and married. If you have problems with this code, please post on the comment. Anybody interested helping others may reply on the comments as well.

Resources:

http://www.WebCosmo.com 100% Free Classified Advertising 

Free Advertising on our website www.WebCosmo.com  for jobs, apartments, personals, pets and much more in more then 30 countries.

Currently rated 4.7 by 7 people

  • Currently 4.714286/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Manik
Posted on: 10/19/2007 at 6:37 AM
Tags:
Categories: Programming / Coding
E-mail |  Stumble it! |  Propeller it! |  Digg it! |  del.icio.us |  Technorati
Post Information: Permalink | Comments (18) | Post RSSRSS comment feed