Find a texbox control or label control on a master page from an inner page

I noticed that this one seems to catch a good few people out - including myself when I was starting out with Master Pages. If you have an aspx page that sits in your Master Page you might have a need of populating a label or a textbox (or other control) with some value. To do this you can simply use the following code to tell .net to find your control on your master page. In the example below I am looking for a Literal control but you can easily change this to TextBox or Label or whatever you want:

Literal ControlIDToFind = (Literal)this.Master.FindControl("ControlIDToFind");

Just change the ControlIDToFind to the name of your ID you want to grab and then you can use it just like you would a normal .net control on your child page. So using the example above, if I wanted to output text into my ControlIDToFind literal which is in my MasterPage I would use the following code in my Child Page:

ControlIDToFind .Text = "some text about how great you are!";

 It's only a small 2 line thing but you'd be surprised how often this catches people out. Below is a small list of items you can find using the above tips, hope it helps!

//find a dropdown in your masterpage and get it's selected value
DropDownList CountryList= (DropDownList)this.Master.FindControl("CountryList");
string SelectedCountryList = CountryList.SelectedValue;
//find a textbox in your masterpage and get it's value
TextBox EmailAddress = (TextBox)this.Master.FindControl("EmailAddress ");
string EmailAddress = EmailAddress.Text;
//find a label in your masterpage and get it's value
Label EmailAddress = (Label)this.Master.FindControl("EmailAddress");
string EmailAddress = EmailAddress.Text;

Jan27

Hide a div element and it's contents using c#

This can be a nice little trick to do if you want to hide options on a user after an update or after so many products are selected, etc. Simply wrap the area you want to hide in a div element and put a runat="server" value into it's tag. Example:

 

<div id="GridDiv" runat="server">
gridview would go in here....
</div>

 

You can now easily set the div to hidden using a bit of CSS that can be set using C# whenever your condition is met. For example:

GridDiv.Attributes["style"] = "display:none;";
//OR you could just type
GridDiv.visible = false;

That's all there is to it but you can see how powerful this can be as it allows you to dynamically set the css class or styles you might want in your applications.

Jan18

Little people fending for themselves

Friday Funny 16 Jan 09

Friday Funny - 16 Jan 09

This website has some amazing shots of little toy people trying to make their way in London. It's impressive to think of the effort each photo must take. One of my favourites is the picture of the musician on the street using an iPod earphone as an amp for his guitar, genius!

http://little-people.blogspot.com/

How to export your tables and all of the content into a T-SQL file from SQL Server

Export database and all its contents into a TSQL file

The SQL Database publishing wizard is a great tool for exporting out your sql database tables, all its content and any stored procedures that you have into a T-SQL file. No more fiddling with text files or excel sheets.More...

Jan12

goodbye 2008, hello 2009!

snowboarding in Bulgaria

I'm just back from a week of snowboarding in Pamporovo in Bulgaria. It was always something that I wanted to do and it is definitely something I will do again but right now my body is in shut down mode. It was the toughest thing I have ever put my body through. I would highly recommend you try it if you haven't yet - if only for the drinking games after a day of boarding ;) I've posted a few pictures up on my flickr page.

More...

Jan04

JCB stunt driver

Friday Funny 19 Dec 08

Every since I was small I've wanted to mess around with a JCB and after watching this video makes me want to do it more now!

Friday Funny - 12 Dec 08

It's got cats in the video. What's not to like!!

Fix for FireFox 3 cookie session issue

Fix Firefox 3 session cookie issue

I had an issue with FireFox 3 recently where none of my cookies were being stored after I closed the browser down. For example if I logged into iGoogle and closed the browser FireFox would not remember that I had logged in already and would ask me to log in again. I ignored this initially but as the days moved on it began to bug me more and more.

I tried to download and install the latest version of FireFox but the version I was running was the most up to date. I installed it again anyway to see if that might fix the issue but it didn't. I took a look online and it seems to be an issue affecting a lot of people. Well, for me anyways, it turns out that to fix this issue was quite painless. You need to go to the FireFox profiles folder and remove a file called cookies.sqlite. In Vista this is located in C:\Users\<<PC NAME>>\AppData\Roaming\Mozilla\Firefox\Profiles\<<Session Folder ID>>. If you can't see the AppData folder you might need to enable hidden folders. You can do this by going to Windows Explorer and selecting Organize ->Folder and Search Options -> Folder Options -> View (tab) -> Show hidden files and folders.

Remember to close FireFox when trying to remove the cookies.sqlite file otherwise you will get a permission denied error as FireFox uses this when it is open. With the file removed, open FireFox back up and try to log onto a site like iGoogle. When you close your browser and start it back up again it should work perfectly for you.

Dec08

Friday Funny 05 Dec

Friday Funny for 03 Dec 08

Limit the text returned from a field using SQL

In most databases today there are columns that contain thousands of characters of text. In some cases this information needs to be returned to an end user on a busy section of your site, for example a search feature.

If someone does a search on your site usually you will be displaying only a portion of that content when displaying the search results. You could very easily return the full content of all your columns to the end user and use your code to stip off the excess data but a much faster way is to strip off what you don't need in SQL before you pass back to your code. One way of doing this is to use the SubString method in SQL Server:

SUBSTRING(ColumnName, start position, end position)

Example below returns the value within PostDetails from the start position up to 150 characters long and calls this new 'field' PostSummary

SUBSTRING(PostDetails,1,150) as PostSummary

Full example:

Select PosterName, PostDate, SUBSTRING(PostDetails,1,150) as PostSummary
From Posts
Where <<search conditions entered from search page>>

You can easily call PostSummary just like you call PosterName or PostDate in your code view.

Dec02