This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Passing data between asp.net pages
how to pass value of TEXTBOX from one page to another page in ASP.NET c# I need, not by using URL string query method. I do need passing from one page to another without passing values by URL.
Use the session state.
Session["TextBoxValue"] = TextBox1.Text;
Then, retrieve it on the other page:
string val = Session["TextBoxValue"];
you can create a form in the first page, this form contains your required parameters and use an action redirect to another page like in this example:
<form method="post" action="yoursecondpage.aspx">
<input ... />
</form>
Another way is using a Server.Transfer("mySecondPage", true), in the second page, on Page_Load event you can cast "PreviousPage as MySecondPage" and get properties .
Related
This question already has answers here:
Get the Value of an asp:HiddenField using jQuery
(8 answers)
Closed 5 years ago.
I assign a value to an ASP hidden field within the page load but can't access that value in my JQuery. I added an alert as a test and it comes up 'undefined'. Am I making a syntactic error?
<asp:HiddenField runat="server" id="hfID"/>
hfID.Value = "Test";
alert($('#hfID').Val());
Asp.Net Webforms mangles the id you give it to make sure it's unique on the page. You can use class as a selector or modify your jQuery selector so that it's looking for an id that ends in hfID.
$("[id$='hfID']")
This question already has answers here:
ASP.NET MVC HTML helper methods for new HTML5 input types
(6 answers)
Closed 7 years ago.
I would like to use the new HTML5 tags with my ViwModel (specifically, I want to use the date picker control and bind it to a DateTime property in my ViewModel), is there any extension method for them?
For example, if I want to bind a text editor to a property in my ViewModel, I can write something like this inside a form:
#Html.EditorFor(m => m.MyTestProperty)
While with HTML5 tags, I only know something like this (as an individual input element):
<input id="myDate" name="myDate" type="date" value="">
The problem with it is I can only get the value from Request.Form["myDate"] and it is only a string value. Can I bind the value of HTML5 element to my ViewModel?
UPDATE:
Thank you for all who pointed out that this is a duplicate question. But I can't seem to get it work with referenced question. Is marking DateTypeAttribute the only thing I need to do and after that the EditorFor extension method would render an input with type date? Because I tried that but nothing happened.
You can use bootstrap datetimepicker for this.
#Html.EditorFor(m => m.MyTestProperty, new {#id="datetimepicker1"})
Then you need below js to activate it. You can just add this inside document.ready().
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker();
});
</script>
This question already has answers here:
Change HtmlForm action in C# ASP.NET 3.5
(6 answers)
Closed 9 years ago.
The form element on my page created with ASP.NET looks as such (if I do "View page source" from a web browser):
< form method="post"
action="Page.aspx?lang=en-US&pp=10&bi=10"
id="ctl01" enctype="multipart/form-data">
How do I access that form element? I need to remove &pp=10&bi=10 part from the action element?
if you just have one form on the page then try this in your javascript
alert(document.forms[0].action);
to change the action just do it like this
var actionPath = document.forms[0].action;
// your logic to remove unwanted string with the help of string functions in JS
document.forms[0].action = actionPath;
In case you know what is the name of your form then access it like this.
document.forms['YOUR_FORM_NAME'].action
or you can access it with the client id as well if you want consiering your form
have runat="server with it
document.getElementById(....)// Client Id of the form control
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Send data from one page to another in C# ASP.Net
Hi I am trying to send data from one page to another in asp.net I found various methods of doing so and am gona try a few to se how they work but I seem to be getting an error on my first attempt.Here is my code:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Go"
PostBackUrl="~/Default2.aspx" />
<br />
I am sending the data from Default.aspx to Default2.aspx.
At the Default2.aspx in the Page_Load method I wrote this to retrieve the data:
if (Page.PreviousPage != null)
{
string txtBox1 = ((TextBox)Page.PreviousPage.FindControl("TextBox1")).Text;
}
From what I read on MSDN this should work but I must be missing something because when I pres the button to send the data and Default2.aspx loads I get an error page that looks like this:
if you use master page for Default.aspx then Page.PreviousPage.FindControl doesn't work. Because when using masterpage, all controls in Default.aspx are placed in ContentPlaceHolder not directy in Form. So you can use below code:
Page.PreviousPage.Form.FindControl("yourContentPlaceHolderID").FindControl("TextBox1");
i assume you use masterpage. Otherwise your first code must work. In addition if you dont use masterpage and textbox is placed in PlaceHolder or Panel, your code won't work as well. FindControl doesnt recursively search in child container controls.
See the link. MSDN
Page.PreviousPage is only available when using Server.Transfer to redirect to a new page. If this is a standard GET or POST, or Response.Redirect then this code will not work.
From your previous page. Use Server.Transfer to be able to access the previous page data.
Sorry, another super basic ASP.NET question. this so embarrassing.
I am reading the article on How to: Pass values between ASP.NET pages
In the second approach, they suggest hooking up a button and directing the user to another page using POST. I don't know how to do this. How do I HTTP POST?
"When the source page uses the HTTP POST action to navigate to the target page, you can retrieve posted values from the Form collection in the target page."
This is how I am sending the user to the new page:
protected void btnSubmitForPost_Click(object sender, EventArgs e)
{
Response.Redirect("GetViaPost.aspx");
}
EDIT
The final solution:
You can use ASP.NET webforms. Do the following: On the first page, create your controls and a button that sends the user to a new page. Handle the click event from this button. As stated below, use Server.Transfer with endResponse=false, instead of Response.Redirect(). When you use Response.Redirect, your post data is cleared out. I did not need to specify action in the form or anything else.
In ASP.NET when you click a button, you're posting the entire page's fields by default (as it's contained within a gigantic <form /> tag last time I checked. You can access these values after clicking the button like this:
string MyPostedValue = Request.Form["MyFormField"];
*Edit as per your update in your question, change Response.Redirect() to Server.Transfer() like this:
protected void btnSubmitForPost_Click(object sender, EventArgs e)
{
Server.Transfer("GetViaPost.aspx", true);
}
Then in your GetViaPost.aspx's page you can get any form/query string variable you passed from your sending page like this:
string MyPostedValue = Request.Form["MyFormField"];
If I'm reading this right, all of these answers are missing the question...
You're looking at posting from one Asp.Net form to another, and one of the methods is what you want to figure out - doing a normal http post. The book or article probably is already telling you about the Server.Transfer as another option if I'm guessing right.
If I'm getting the question right, then the simplest answer is to not use a standard ASP.Net form (with the runat = server attribute) as the starting point, but to use a simple standard html form to post to an asp.net page
<form action = "targetpage.aspx" method="post">
...some form fields here
<input type = "submit">
</form>
If in the codebehind you wire up to the button click event, then click the button. It's a POSTback that happens.
Any controls that you have runat="server" will be accessible by their id (and any values set on them) in the codebehind.
In terms of posting data to other pages, you have a number of options available to you.
The querystring, sessions, cookies and viewstate.
A basic example (with no error handling) given your updated Response.Redirect might be:
int someId = int.Parse(txtBoxOnThePage.Text);
Response.Redirect(string.Format("GetViaPost.aspx?myId={0}", someId));
Then on the GetViaPost page you could pull that out by:
HttpContext.Current.Request.QueryString["myId"]
http://www.asp.net/learn/ is a surprisingly good source of information and tutorials for this kind of learning.
ASP.NET buttons always perform a POST. You can set which page the button posts to using the PostBackUrl property of the button. If you leave this blank, the button will post back to the same page that is resides on.
Check out this article for more information.