We’d like to remind Forumites to please avoid political debate on the Forum.
This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.
📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
The Forum now has a brand new text editor, adding a bunch of handy features to use when creating posts. Read more in our how-to guide
ASP.Net newbie needs help
Astaroth
Posts: 5,444 Forumite
in Techie Stuff
I have a page which contains a form which will have a variable number of textboxes for the user to complete. The text boxes are called (for arguements sake) Textbox1, Textbox2, Textbox3 etc
I was intending to create a Do.... Loop using a counter and variable to create the textbox name and then use this variable in the request.form command - see below as an example of what I am trying to say.
The problem that I am having is that the request.form(textboxname) is not returning an results, though if I put a test piece of script in of request.form("textbox1") it does return the anticipated value.
How do I get round this problem?
Many thanks
I was intending to create a Do.... Loop using a counter and variable to create the textbox name and then use this variable in the request.form command - see below as an example of what I am trying to say.
Dim loopcount As Integer = 1
Dim item_IDs As String = ""
Dim textboxname As String
Do
textboxname = "textbox" & loopcount
If Request.Form(textboxname) < 1 Then Exit Do
item_IDs = item_IDs & "," & Request.Form(textboxname)
loopcount = loopcount + 1
Loop
The problem that I am having is that the request.form(textboxname) is not returning an results, though if I put a test piece of script in of request.form("textbox1") it does return the anticipated value.
How do I get round this problem?
Many thanks
All posts made are simply my own opinions and are neither professional advice nor the opinions of my employers
No Advertising or Links in Signatures by Site Rules - MSE Forum Team 2
No Advertising or Links in Signatures by Site Rules - MSE Forum Team 2
0
Comments
-
I don't really use VB.Net, but using C# you could try something like:
int i = 0; foreach (System.Web.UI.Control c in Page.Controls) !! if (c is TextBox) !! TextBox data = c as TextBox; if(c.ID == string.Format("myTextBox{0}", i)) !! // do something i++; } } }
It'll depend where you have the TextBoxes sat. The above example assumes they aren't nested within any other .Net objects (such as a PlaceHolder), and that they are sat directly on the page.
Or you could use theTextBox t = Page.FindControl("myTextBoxId") as TextBox if(t != null) !! // do something }
Dynamic forms can be a nightmare. Another alternative, if possible, would be to put all the TextBoxes on the page, and turn then visible/hidden depending upon whether they are needed. If it's truly dynamic though, then it can be quite a pain."Boonowa tweepi, ha, ha."0 -
ASP.Net is object orientated so you should be refering to your textboxes as objects, not as properties of the Request.Form object as was the case in Classic ASP.
The following code is based upon having 3 TextBox controls on a page with a Button control for postback.Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Page.IsPostBack Then Dim intLoop As Integer = 1 Dim strObjName As String = "" Dim objTextbox As TextBox Do strObjName = "TextBox" & intLoop objTextbox = CType(Me.FindControl(strObjName), TextBox) If Not objTextbox Is Nothing Then Response.Write(objTextbox.Text) Else Exit Do End If intLoop = intLoop + 1 Loop End If End Sub0 -
For some reason I'm unable to edit the post I created!!
This will only work if your textboxes are consecutively named. You'd probably be better iterating through the Controls collection on the page and looking for textboxes.0 -
Sweet, cheers, didn't know there was a CODE entry when doing posts. Updated mine to look nicer."Boonowa tweepi, ha, ha."0
-
mparter wrote:ASP.Net is object orientated so you should be refering to your textboxes as objects, not as properties of the Request.Form object as was the case in Classic ASP.
The following code is based upon having 3 TextBox controls on a page with a Button control for postback.
Many thanks.... as I say... am still very much struggling to get my head round object orientated scripting (and failing)
The text boxes are of an unknown number but are consecutively named.... I have tried to use the more built in functions to do what I want but a combination of lack of knowledge and using MySQL as the db is meaning that I am struggling with some parts so having to use slight cellotape and glue methods of getting it working and will then look to streamline my code once I am more proficient at .netAll posts made are simply my own opinions and are neither professional advice nor the opinions of my employers
No Advertising or Links in Signatures by Site Rules - MSE Forum Team 20 -
I'd maybe have a look at C#. I always find OOP much easier with it.
Also, if you're using MySql, maybe have a look at SubSonic (http://www.codeplex.com/Wiki/View.aspx?ProjectName=actionpack). It's a simple code generator, just point it at your database and it'll do the rest. Very much like Ruby On Rails, but for .Net. Watch the video at the link above and you'll see how easy it is to use. Should make data access much easier for you.
FXCop (http://www.gotdotnet.com/Team/FxCop/) is very good tool for checking over your code. Basically it'll raise alerts if it's not correctly formatted or named, which will help you write more consistent code.
And lastly, make sure you're using something like Visual Studio to build the project in. It makes things so much easier."Boonowa tweepi, ha, ha."0 -
Currently using the Visual Web Developer
Will have a look at subsonic - have heard people talk about ruby on rails but never have known what it is/ havent had chance to look into it.
The simple wording of VB.Net reminds me much more of the old days of BBC Basic programming and of cause from classic ASP where as C# just looks too much like javascript/ perl that used to scare me
All posts made are simply my own opinions and are neither professional advice nor the opinions of my employers
No Advertising or Links in Signatures by Site Rules - MSE Forum Team 20 -
D'oh
Would appear that I spoke too soon.... the code works perfectly when I have a fixed number of input boxes which I can therefore give IDs of textbox1 textbox2 etc to but doesnt work with my textboxs that are within a gridview (using MySQL I cannot use the gridviews native database update/ couldnt make it look as I wanted even without the actual update working).... dont get any error codes but it simply doesnt pick up the values of the text box.
The previous way that I did it was to use HTML textboxes with the ID being set dynamically with the row number at the end of the "column name". Obviously cant use your script to gather the inputs as they are not controls and cannot see how to put a variable into the request.form() - as per my original query.
How can I either.... a) get the values of the controls out of the gridview or b) sequentially ID textbox controls in a table? c) any other way to do this given the mysql limitation?All posts made are simply my own opinions and are neither professional advice nor the opinions of my employers
No Advertising or Links in Signatures by Site Rules - MSE Forum Team 20 -
You'll need to loop through the GridView. I don't really use them (too heavy) but I think it's probably made up of items called something like GridViewItems. So,
int i = 0; foreach(GridViewItem item in myGridView.Items) !! TextBox t = item.FindControl("TextBox" + i) as TextBox; if(t != null) !! string theValue = t.Text.Trim(); i++; } }
...or at least that's the rough logic when using a Repeater. It shouldn't be too different if it's not right."Boonowa tweepi, ha, ha."0
This discussion has been closed.
Confirm your email address to Create Threads and Reply
Categories
- All Categories
- 353.5K Banking & Borrowing
- 254.2K Reduce Debt & Boost Income
- 455K Spending & Discounts
- 246.6K Work, Benefits & Business
- 602.9K Mortgages, Homes & Bills
- 178.1K Life & Family
- 260.6K Travel & Transport
- 1.5M Hobbies & Leisure
- 16K Discuss & Feedback
- 37.7K Read-Only Boards