Wednesday, November 21, 2007

Getting Information From input Tags On A Page In .NET Using Request.Form.AllKeys & Request.Form.Get

While creating a web-based software program recently I came across a small problem. This program dynamically creates a number of textboxes inside of rows that reside inside of a table server control. The rows along with the cells & textboxes are all dynamically created based on user input. Once the textboxes have been created, the user is able to enter information into them. Then the software has to read that information and process it.

This sounds like a simple process right? Well it is a simple process, however, I ran into one problem. When you dynamically create text boxes they are rendered as input tags in the html and not as .net server controls with this tag . This creates an issue when you need to loop through the controls and get the information from the textboxes.

Now you may be thinking, what is she talking about? I can just loop through the Table’s row collection and get a reference to the textbox controls that way. Well I’m here to tell you that won’t work. Sorry L. You may be thinking that you can get to the textboxes via the form’s control collection. Well I’m here to tell you that won’t work either. So what’s a girl, or a guy, to do? Well ladies and gentleman, the answer is really simple.

All you have to do is use Request.Form.AllKeys and Request.Form.Get and you will be able to get a reference to those pesky controls all day long. Here is code you can use. In my case, I named those textboxes with a generic name and just incremented the numbers. So if a user requested 120 boxes, my textbox names would range from uTxt1 to uTxt120. That way I could loop through and use StartsWith(“uTxt”). Here is my code:

int i = 0;
string sValue = "";

foreach (string s in Request.Form.AllKeys)
//this lets me know that I have reached one of the controls that
//I dynamically created earlier
if (s.StartsWith("uTxt"))
sValue = Request.Form.Get(i);


That’s it! Now you can reference any of the controls at any time without any issues.
By the way, if you are using placeholder controls , Request.Form.AllKeys and Request.Form.Get really come in handy since placeholders render the asp server controls used for input as input controls and not server controls.



Server controls are being rendered as html input controls in the final page and you need to access the values of these controls in the code-behind at runtime.

You can't get a reference by looping the control collections of the aspx page because all of the values return null and the controls values you need are being held by html controls and not server controls.

Use Request.Form.AllKeys in combination with Request.Form.Get to get to the particular control and value you need. You can loop through the AllKeys collection using a foreach loop and then reference the item you need using Get.

Tuesday, November 20, 2007

Where Is IsNumeric In C#? Right Where It Should Be!

When I first switched from programming in Visual Basic to programming in C#, I felt a little neglected. There were a few basic functions that were not available to me in C# that were available in VB. One of these wonderful functions is IsNumeric. We all know that we can’t trust end users to enter the correct thing in our wonderful textboxes on our forms. Therefore, we have to check. In VB it was as simple as calling IsNumeric and popping in whatever I wanted to check. In C#, I had to write more than the 9 letters of IsNumeric to do the same thing. If you are a newbie to C# and you have experience in VB, I’m sure you have looked around for that function in vain.

There were some happy smiles around the Internet when C# programmers thought that it was coming in the next version of dot NET. Oh well. You can wipe those happy smiles off of your faces. As you may or may not know, it just ain’t happenin’ folks! (Please don’t email me to correct my English. I say what I want. I’m a woman) So since it’s not already in C#, and it’s not coming to C#, what are we going to do? There are so many solutions floating around the Internet that I’m sure you can find one quickly, however, I say if you can’t beat’em join’em.

Here is where References come in handy. It’s not really earth shattering, but I’m sure that someone somewhere may find it useful. So put away your RegEx stuff and stop writing your own class and let dot NET do all of the work for you. I’m so lazy right?

At the top of your code-behind in your using section, add a reference to Microsoft.VisualBasic. It should look like this:

using Microsoft.VisualBasic;

Now in your code, you can reference IsNumeric like this:

bool bYourVariable = true;

//cast the variable to string to avoid an error if it really is a string
//the error you get if you don’t cast it first is that the string
//isn’t formatted properly
string whateverYouAreCheckingGoesHere.ToString();

bYourVariable = Information.IsNumeric(whateverYouAreCheckingGoesHere);

That’s it! Now you can laugh in the faces of our VB programming brothers and sisters all around the world and tell them that you have found the IsNumeric in C#. You have put the eggs back in the cake and you can bake! Tell them that C# rocks. Then go back to doing whatever it was that you were doing!


Using the IsNumeric function in C#

C# does not have a built in IsNumeric function

Create a reference to Microsoft.VisualBasic and then use Information.IsNumeric(whateverYouAreCheckingGoesHere)

Saturday, November 17, 2007

Are your strongly typed datasets not generating Delete & Insert statements & stored procedures?

This is something very simple you may or may not have come across in your day-to-day dot net programming life. There are times when we use those entirely beautiful, full of great things, code-saving datasets in to make our lives easier. However, you may have noticed that there are times when the strongly typed dataset does not generate all of the required stored procedures even though you specified them when you set up the table adapter.

The reason is simple and the fix is simple too. The strongly typed dataset looks at your table to see if a primary key is defined. If your primary key is defined, then your delete and insert functions will be created. If you have been very bad and deserve a spanking and HAVE NOT defined a primary key, you will not see your Delete and Insert statements or stored procedures.

Now wasn't that simple?




Error: Delete & Insert stored procedures and statements are not generated in a strongly typed dataset
Cause: A Primary key is undefined on your table.

Fix: Define a primary key on your table and then define your dataset.

Comments anyone?

'the form' is undefined - Why can't things ever be simple?

Ok. There are times when I want to go to Microsoft and their infinitly great team of extremely tech savvy and wise developers and get down on my knees and ask why they are trying to make me miserable. I could whine. I could bitch. I could complain. My guess is that no one would care. It may be that the Microsoft team isn't even the source of my problem. But we all need someone to blame so those guys & gals will do just fine.

Here is my latest little issue. I am working on a website that is going to be fantabulous (Please don't freakin' email me! I know it's not a REAL word! However, I'm a woman and I can say whatever I damn well please!). The site is called ShowOffYou and its a site for social site backgrounds, layouts and graphics. The exact address is I'll keep you updated about it's "grand opening." Anyway let me get back to my story.

I am using master pages on this site. Code reuse - I just love it. Everything was going along smoothly and I was in a great mood. I created a .js file to hold all of my wonderful javascript functions and I added this file to my master page. I added it to my master page's head section using the following notation:

Everything was going along well until I tested my document. All of a sudden, I start getting these wonderful javascript errors. Now I could leave them, but I hate to give regular people out there a reason to think that I'm less than perfect :-). Here is the error.

Just in case you are lazy or just don't feel like clicking on the image above, I will tell you what it says. It says 'the form' sys is undefined. 'the form' is undefined??? Oh come on! :-(

Ok. I looked at the source code via the browser. It seemed fine. I looked at my web.config file. It looked just great. Ok. So what now??????
WELL, it appears that .Net does not like the way I did a particular thing. That THING is that I didn't use the end script tag. I CANNOT BELIEVE IT!!! This javascript error was caused because I didn't use an end script tag! What? Ok. What-Ev-Ver (Please don't email me to correct me. I know that whatever is not spelled What-Ev-Ver - but again, I can say What-Ev-Ver I want How-Ev-Ver I want.)

Here is the correct way the script should have been written.

That's it! If you get that dreaded 'theform' is undefined error, just make sure that your script tag has an end script. There may be additional reasons for this 'theform' is undefined error, however, this is a simple fix to look at first.

I hope someone benefits from my 'theform' is undefined ramblings.
Have lots of dotnet fun people!!




Error: Javascript error displayed to end users in their browser - 'theform' is undefined

Cause: Defining the script tag without an ending script tag in the head section of your document.

Fix: Change the way you define the script tag. Make sure that there is a beginning tag and a full end tag. See the images above.

Comments anyone?