Background: I've been asked to work on a legacy asp.net application for which there is no documentation and the original programmers are not available for consultation. I'm primarily a Perl programmer, primarily on *nix systems, so while I've no major problems with the actual logic of the system, I'm getting tied in knots by some of the "bare bones" stuff underneath.
Outline: I'm trying to extend an existing aspx form to increase its functionality, the form is part of a system for purchasing telephone numbers. At the moment, the form when first called shows a list of locations and telephone area codes in an HTML drop-down list, and expects the user to select one. When a value is selected (or selected and the select button clicked, if JS is disabled) and the form submitted, the page in postback mode then provides an appropriate redirect to a purchase page, with various values set in the query string. I want to extend the functionality by adding a second row of controls, on the same form, comprising a text box for users to enter an area code, and a "Search" button. The idea is that when a user does this, the system will then do a DB search for that area code, and if a match is found, generate the appropriate redirect to the page selling that area. All the code is (and will be) in C#.
The problem: Having found a lot of useful info here on Stack Overflow on extracting form variables from asp.net forms, I tried to retrieve my new variables using Request.form["variable_name"]. But it soon became clear the values weren't coming through.
Here's the aspx page code for the new bit of the form:
<tr>
<td align='left'><b>STD code:</b></td>
<td align='left'>
<asp:TextBox id="STD_Code_Search"
AutoPostBack="True"
Columns="7"
Text=""
TextMode="SingleLine"
Wrap="False"
runat="server"/>
</td>
<td><asp:Button ID="Button2" runat="server" Text="Search" /></td>
</tr>
I expected to be able to pull those variables back via
Button2_Clicked = Request.Form["Button2"]
STD_Code_Search = Request.Form["STD_Code_Search"]
However nothing ever came through. Based on more searches, I found this code to display all the variables coming into the form:
foreach(string key in Request.Form.Keys)
{
LOG.WriteLine(key + ": " + Request.Form[key] + "<br/>");
}
The LOG object is a StreamWriter going to a text file, which seems to work fine.
However the data in the file looks like this:
ctl00$ctl00$ctl00$ContentPlaceHolder1$Main$Range_Data$STD_Code_Search: 01952<br/>
ctl00$ctl00$ctl00$ContentPlaceHolder1$Main$Range_Data$Button2: Search<br/>
Fair enough, I then tried to use the following to retreive the data in C#
STD_Search = Request.Form["ctl00$ctl00$ctl00$ContentPlaceHolder1$Main$Range_Data$STD_Code_Search"];
That does actually work and if I send STD_Search to the StreamWriter LOG I can see the entered value in my text file.
However if I then try and read a second value (in this case I'm trying to catch the value of Button2, so I can trigger specific behaviour if that button, rather than the original one, is clicked), using this code:
STD_Search = Request.Form["ctl00$ctl00$ctl00$ContentPlaceHolder1$Main$Range_Data$STD_Code_Search"];
Button2_Clicked = Request.Form["ctl00$ctl00$ctl00$ContentPlaceHolder1$Main$Range_Data$Button2"];
Then while the asp.net processes all appear to complete correctly (no errors or page crashes), the writing of the text file stops dead and no data at all is written.
Questions:
I'm sure I shouldn't have to include all the placeholder stuff (reading on here I gather that's part of the underlying framework and asp should in fact just present me with the named variables, the same as a Perl or PHP system would do with a plain HTML form), any pointers as to why that might be happening and if there's anything else I should be doing to process / extract the data?
Why does it all drop dead when I try and read in a second variable, despite working (for some large values of working) when I only read in one?
Are there any recommended books on asp.net and C#? I have Sitepoint's "Build your own asp.net 4 website using C# & VB", which has helped a bit but isn't so useful for trying to take someone else's code apart and re-build it.
Prior research: I've done extensive searching on this, however most of the answers I've seen so far have been related to people using asp.net to process data submitted from external sites, etc, or by scripts using post directly. All the sites I've looked at for tutorials on asp.net appear to say I should just use Request.Form["variable_name"], so there appears to be something else going on here.
You should be using code-behind to do all this stuff. It will make it easier.
string searchText = STD_Code_Search.Text;
And to to know if a button was clicked, you would do
<asp:Button ID="Button2" runat="server" Text="Search" OnClick="Button2_Click" />
And have it processed
protected void Button2_Click(object sender, EventArgs e)
{
//button2 was clicked
}
Asp.Net is an event-driven framework, not like PHP or JSP. Pick a good book on Asp.Net and read. Most of the things you were trying to do have been handled for you by Asp.Net.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With