html transferring values in button click - asp.net

I have a html application with Textbox and a button. (static application)
I need to transfer the textbox value on the button click,
which will take me to another asp.net application.
(Where I can retrieve that in query string) is that possible?
2 applications are in different servers.

make the method of your form get and and the action of the form the url of the other website you want to hit eg
<form action="http://www.google.co.uk" method="get">
<input type="text" name="test" />
</form>
when submitted will post to http://www.google.co.uk?test=ValueOfInput
EDIT
Just noticed you are using asp.net - this is not static html! As your have an asp button you can add a button click event so your code looks like this:
<input ID="btnsrch" type="button"value="Search" runat="server" onclick="btnsrch_click" />
then in your corresponding cs file you can add the following function:
protected void btnsrch_click (object sender, EventArgs e)
{
Response.Redirect(string.Format("http://www.urltogoto.com?variable={0}", txtsrch.Text));
}

Yes this is possible.
You can give a postback url and can get the value in request object over their.

Related

MVC & ASP.Net Web Form : Viewstate?

I have used ASP.Net Webforms. I am currently learning MVC, and I have read that ASP.NET MVC–generated pages don’t contain any View State data.
Can someone explain what is the substitute for viewstate in MVC?
How come they eliminated viewstate? As controls also have viewstate, what's the alternative in that case?
In webforms, the first page load is distinguished by using IsPostback property and server controls are basically initialized with data or initial values in Page_Load event. These initial settings are persisted on subsequent post back events for the page. As you may already know, all this is accomplished by viewstate which is basically a hidden variable that contains state of all server controls on the page and gets posted back for each postback event.
MVC doesn't have this mechanism and is completely stateless i.e, we need to manually assign values of controls for each request. For ex:
In Web forms
ASPX page:
<asp:TextBox id="txtName" runat="server"/>
<asp:Button id="btnSubmit" Text="submit" onclick = "btnSubmit_click" />
Code behind
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostback)
{
txtName.Text = "test1";
}
}
protected void btnSubmit_click(object sender, EventArgs e)
{
//some code
}
Enter a value and click on submit button, the value gets persisted event after postback due to viewstate.
In MVC
View
#using(Html.BeginForm()){
<input id="Name" type="text" name="Name"/>
<input type="submit" value="submit" />
}
Controller
[HttpPost]
public ActionResult Index(FormCollection values)
{
return View();
}
Enter a value and click on submit button, the value is not persisted across postback since there is no such persistence mechanism. So we need to achieve it manually
View
#using(Html.BeginForm()){
<input id="Name" type="text" name="Name" value="#ViewBag.Name"/>
<input type="submit" value="submit" />
}
Controller
[HttpPost]
public ActionResult Index(FormCollection values)
{
ViewBag.Name = values["name"];
return View();
}
Note: I have used ViewBag only to show an example, ideally we need to pass viewmodel from controller.
Additional Inputs
Apart from its role of persisting state, ViewState can also be used to store values, say for ex: ViewState["test"] = objectoranyothervalue. In MVC, there are other techniques for state management like ViewBag, ViewData and TempData but difference is they don't get posted back to server like ViewState. They are used to just pass data from controller to view.

How to prevent form's action to occur in asp.net

I have a form with action="xyz.aspx:type=new" and some input textboxes and 2 buttons: Submit and Preview. The Submit button updates the database with textbox values and Preview button carries the form textbox values to page "xyz.aspx?type=preview" . The problem is that the functiobalities are achieved but after clicking Preview button, the form's action url gets into action and causes an update in the database which is not wanted. So how do I prevent this from happening? ANy help is appreciated.
If your two buttons are not asp:Buttons, do that first.
<asp:Button Id="Submit" runat="server" OnClick="Submit_OnClick"/>
<asp:Button Id="Preview" runat="server" OnClick="Preview_OnClick"/>
Now, move your code to update the database into Submit_OnClick
protected void btnExecute_Click(object sender, EventArgs e)
{
//update your database
}
Your database should only be updated when Submit is clicked.
If you need your Preview button to post to another page, use the PostBackUrl property.

File upload control in ASP.NET

i am using file upload control in ASP.NET for uploading images.
In my form there are two buttons.one for assigning criteria which redirects to other form and other for submitting form.
After assigning criteria only the user has to use submit button.
My problem is when is upload image and click on AssignCriteria button and return back to original page,the upload control is getting blank.
How to keep that upload image control value in that textbox even if we redirected to other page and come back.
<asp:FileUpload runat="server" ID="uploadStatement" />
<asp:Button runat="server" Text="Upload" OnClick="cmdUpload_Click" />
Next code uploads selected file to Temp folder on the server, in my case - parses it and deletes the file.
protected void cmdUpload_Click(object sender, EventArgs e)
{
var fileName = Server.MapPath(Path.Combine("Temp", String.Concat(Guid.NewGuid().ToString(), Path.GetExtension(uploadStatement.FileName))));
try
{
uploadStatement.SaveAs(fileName);
// parse file
}
finally
{
File.Delete(fileName);
}
}
Since any manipulation with the "input type='file'" element is a serious security threat I am afraid there is no way to do this.
Have you considered using of some AJAX overlay "dialog"?

using sessions in asp.net

I would like the data that i enter in a text box on pageA to be accessable on pageB
eg: User enters their name in text box on page A
page B says Hello (info they entered in text box)
I heard this can be accomplished by using a session but i don't know how.
can someone please tell me how to setup a session and how to store data in it?
Thank you!
Session["valueName"]=value;
or
Session.Add("valueName",Object);
And You can retrieve the value in label (for Example) By
/*if String value */
Label1.Text=Session["valueName"].ToString();
or
Label1.Text=Session.item["valueName"].ToString();
And also You can remove the session by;
/*This will remove what session name by valueName.*/
Session.Remove( "valueName");
/*All Session will be removed.*/
Session.Clear();
// Page A on Submit or some such
Session["Name"] = TextBoxA.Text;
// Page B on Page Load
LabelB.Text = Session["Name"];
Session is enabled by default.
Yes, you could do something like JohnOpincar said, but you don't need to.
You can use cross page postbacks. In ASP.Net 2.0, cross-page post backs allow posting to a different web page, resulting in more intuitive, structured and maintainable code. In this article, you can explore the various options and settings for the cross page postback mechanism.
You can access the controls in the source page using this code in the target page:
protected void Page_Load(object sender, EventArgs e)
{
...
TextBox txtStartDate = (TextBox) PreviousPage.FindControl("txtStartDate ");
...
}
You can use session to do this, but you can also use Cross Page Postbacks if you are ASP.NET 2.0 or greater
http://msdn.microsoft.com/en-us/library/ms178139.aspx
if (Page.PreviousPage != null) {
TextBox SourceTextBox =
(TextBox)Page.PreviousPage.FindControl("TextBox1");
if (SourceTextBox != null) {
Label1.Text = SourceTextBox.Text;
}
}
There is even a simpler way. Use the query string :
In page A :
<form method="get" action="pageB.aspx">
<input type="text" name="personName" />
<!-- ... -->
</form>
In page B :
Hello <%= Request.QueryString["personName"] %> !

Implementing a search page using url parameters in ASP.NET and ASP.NET MVC

Let's say I have a search page called Search.aspx that takes a search string as a url parameter ala Google (e.g. Search.aspx?q=This+is+my+search+string).
Currently, I have an asp:TextBox and an asp:Button on my page. I'm handling the button's OnClick event and redirecting in the codebehind file to Search.aspx?q=
What about with ASP.NET MVC when you don't have a codebehind to redirect with? Would you create a GET form element instead that would post to Search.aspx? Or would you handle the redirect in some other manner (e.g. jQuery event attached to the button)?
You need to understand that MVC doesn't directly reference .aspx pages like WebForms in its URLs. Its main purpose is to separate concerns, that is model (data), controller (logic), and view (presentation).
First, you'd have to create a route matching your URLs, which would now look like this for example : /home/search/This+is+my+search+string
This would call the Search action method of the Home controller, which would get "This is my search string" as an input parameter. This action is responsible for accessing the model and pulling the results probably from a database.
Typically, your search action would then return a ViewResult containing the view placed in the folder /Views/Home/Search.aspx. Here, you can use neither the Postback functionality nor the events of your Web controls like in WebForms, because MVC applications are stateless and not event-driven. It's more like a request/dispatch way of doing things.
Read more about MVC here.
Create a user control called Search.ascx with a form:
<% using (Html.BeginForm ("Search", "Home")) { %>
<input name="search" type="text" size="16" id="search" />
<input type="image" name="search-image" id="search-image" src="search.gif" />
<% } %>
And in your search action all you need is the following:
public class HomeController : Controller
{
public ActionResult Search (string search)
{
throw new Exception (string.Format ("Search: {0}", search));
}
}
In your master page or wherever you can then add
<% Html.RenderPartial ("Search"); %>
You can use a simple javascript in the button's onclick to redirect to the search page:
Search <input type="text" id="go" size="4" /><input type="button" value="<%=Html.Encode(">>") %>" onclick="javascript:window.location='<%=Url.Action("Search", "Home") %>/' + document.getElementById('go').getAttribute('value')" />

Resources