|
Hi,
I've just getting into ASP.NET and am wondering what is the best practice on passing variables between pages. I have done a lot of programming in classic ASP and used querystrings a lot to pass information between 2 pages.
Given that one of ASP.NET's advantages is strongly typed variables it seems a bit nasty to have to resort to using querystrings and then converting values into which ever type is expected e.g.:
string strMode = Request.QueryString["mode"];
if( strMode == "add")
{
if(Request.QueryString["myid"] != null)
{
short nMyID = Convert.ToInt16(Request.QueryString["myid"]);
DetailsFromAddPage(nMyID);
}
}
None of the books I have seen really mention this so i'm thinking that i've missed something!
If someone could give me a pointer in the right direction I would be grateful
Thanks.
|
|
|
|
|
I don't think there is another way short of storing values in Session variables, database or text file.
Now if you can use Server.Transfer() instead of Response.Redirect() you won't even need to pass anything because the page you call will be executed in the same process and I believe that all the variables from the first page will be accessible in the second page.
|
|
|
|
|
I need to attach different (self-created) user controls to my web form at run-time, and I want to specify their position when/after they are loaded.
The only way I have managed to do this so far, is by drawing a control container (like the Placeholder control) at designtime, but I need to do it all at runtime.
Does anyone know how to do this?
Thanks!
Eivind
|
|
|
|
|
Try something like this:
Label lblNew = new Label();
lblNew.Text = "This is a new label";
lblNew.Style["position"] = "absolute";
lblNew.Style["top"] = Convert.ToString(150);
lblNew.Style["left"] = Convert.ToString(200);
Controls.Add(lblNew);
|
|
|
|
|
Thanks, but this metod has a minor drawback:
It doesn't work for controls that needs to be inside a Form tag (i.e. the TextBox Control).
The reason for this, I guess, is that .NET output the control after the tag in the web-page, which I believe is not ideal.
I guess that I somehow should try to get a reference to the Page's Form, but I don't know how... (yes, I'm a newbie!)
Eivind
|
|
|
|
|
Well, if your form has an id you should be able to refer to it using that id. For example:
...
<form id="frmMain" ....>
...
frmMain.Controls.Add(myLabel);
...
Another solution might be is to just place all the controls in the form from the very beginning, but hide the ones you don't need and show them when necessary.
|
|
|
|
|
Thank you Konstantin, this was what I needed to get things right!
By using the FindControl() method, I got a reference to the Form. Here's the code for anyone interested.
Control myUserControl;
myUserControl = Page.LoadControl("Login.ascx"); //my custom user control
Control activeForm;
activeForm = Page.FindControl("myFormId"); //the id of my - element
activeForm.Controls.Add(myUserControl); //add the user control to the form
|
|
|
|
|
Hi how can i validate some form fields without using asp:RequiredFieldValidator or asp:RegularExpressionValidator?
I have this:
asp label tag id="lNome" text="Name:" runat="server" /> | asp textbox tag id="tNome" size="30" runat="SERVER" /> | | |
and the function:
Sub Validate()
if tNome.value = "" then '??? doesnt work!!!
msgbox ("Please Insert a string")
else
msgbox ("OK") ' How Can I submit the form here??
end if
End Sub
Can someonelse help me please???
Thanx in advance
bRunO
|
|
|
|
|
|
I've corrected the post... there is a problem when i write asp:label ...
By the way how can i submit a form in a VBS function??
|
|
|
|
|
Bruno Lemos wrote:
I've corrected the post... there is a problem when i write asp:label
OK. System.Web.UI.WebControls.TextBox asp control does not have a Value property. You have to do tNome.Text on the server side.
Bruno Lemos wrote:
By the way how can i submit a form in a VBS function?
Do you mean how to run a client-side function on submit? This is why you should use Custom Validator - it will take care of it for you - you will be able to specify client-side and server-side validation functions. It is very simple, just look at the link I gave you.
|
|
|
|
|
ok thanx for your help. is it possible this?
validate a field, if its not valid, appears a red label "Incorrect Entry"
if its valid submits the form to another page (page2.aspx)?
thanx again
|
|
|
|
|
You don't submit the form to the other page per say, instead you validate everything in this page and then redirect user to the other page. For example, in your Page_Load or myButton_Click event handler functions on the server side you can do this:
If Page.IsValid Then
Response.Redirect("page2.aspx")
End If
|
|
|
|
|
ok..
sorry being so newbie
one more question please:
when i redirect to page2.aspx is it possible to get the value of the field with Request.Form["tName"] in order to insert it in a Db?
|
|
|
|
|
Not if you redirect. You could store it into a Session variable, but it is probably better to do a Server.Transfer("page2.aspx") instead of Redirect than. If you do the transfer you will be able to do Request.Form("...") in the other page.
|
|
|
|
|
ok thanx for all your help... i'll try some stuff with your tips and if i have more questions i'll post them ok? thanx again
|
|
|
|
|
Hi All !
I know a best domain & hosting services
Domain $15/year , Hosting $1/month at www.clicknhost.com
Kalaji
|
|
|
|
|
What happened to the HTML/CSS Article section? I cannot find it in the left hand bar on the home page. The folders still exist (I checked my first article and it was still there.)
Am I just missing it? Also ASP is not there, though I guess ASP is being put into the .NET section.
Still, what happened to the HTML/CSS Article section?
regards,
Paul Watson
Bluegrass
Cape Town, South Africa
"The greatest thing you will ever learn is to love, and be loved in return" - Moulin Rouge
Martin Marvinski wrote:
Unfortunatly Deep Throat isn't my cup of tea
Do you Sonork? I do! 100.9903 Stormfront
|
|
|
|
|
Everything is sill there as far as I can see. I think you need to select All Topics tab to see it.
What I want to know is what happened to article submission wizard. It says it is temporary unavailable. I have an article to submit and now I am not sure if I should just email it to Chris or wait for wizard to come back. The thing is that I don't know if it is down for a long time now or is it just today or what...
|
|
|
|
|
Konstantin Vasserman wrote:
I think you need to select All Topics tab to see it.
Aahhhh Thanks Konstantin.
Konstantin Vasserman wrote:
What I want to know is what happened to article submission wizard
Me too, was about to submit a second article and found the wizard down. I assume Chris is improving it no end So the wait, not sure how long, should be worth it.
regards,
Paul Watson
Bluegrass
Cape Town, South Africa
"The greatest thing you will ever learn is to love, and be loved in return" - Moulin Rouge
Martin Marvinski wrote:
Unfortunatly Deep Throat isn't my cup of tea
Do you Sonork? I do! 100.9903 Stormfront
|
|
|
|
|
Paul Watson wrote:
Me too, was about to submit a second article and found the wizard down. I assume Chris is improving it no end So the wait, not sure how long, should be worth it.
Well, according to Chris there is a problem with it that they are trying to fix and they hope to get it working soon. So we'll just wait. Maybe I should write another article while waiting - I still have 20 more to go to get my platinum membership status...
|
|
|
|
|
Hai,
Its surprising me, that no one discusses about the traditional . It didnt work. Because the submit button always tries to call the Page_Load function of the same page.
Surprising enough, none of Microsoft's documentation discusses about the action attribute. Wondering whether it is not required since the form itself is running on the server.
Then how would I jump to a different page?
omkamal
|
|
|
|
|
Doesn't Response.Redirect() work anymore?
|
|
|
|
|
But why do I want to use Redirect? Why not posting form values to a different page by calling that page? Like what we used to do in ASP.
omkamal
|
|
|
|
|
In ASP it was a different story: you had one page with HTML form and one page with ASP code to process the data. Now you can do it all in one page without creating a mess out of your code and without having all those extra pages. That is one of the good things about ASP.NET, as far as I concern...
|
|
|
|