C# Web Application Page Load with Method call - c#

I have a web application with two pages at the moment, The first page the user needs to complete some details, with a button that needs to open the second page with a list of buildings and the user needs to choose one of to populate a list of textboxes in the first form.
My question is, from a button on the first page. (CreateSurvey.aspx) I want to open the second page (Search_Property.aspx)
I have come across syntax like
Search_Property Search = new Search_Property();
Search.open();
But the .open does not exist in this context.
Could someone help me please.
Regards
Rob

In Web forms, you open pages by redirecting to the new page. So, in your button click event, you would use:
Response.Redirect("Search_Property.aspx"); or
Server.Transfer("Search_Property.aspx");
And of course there's the option to modify your button markup to post directly to Search_property.aspx as shown here:
<asp:Button ID="searchButton" runat="server" Text="Search" PostBackUrl="~/Search_Property.aspx" />

If you don't want to navigate to another page and then back again, there are several options. You can create HTML "dialogs" in your CreateSurvey.aspx page and show/hide them as necessary. You can also use an IFRAME on the CreateSurvey.aspx page to display the Search_Property.aspx page and then use some Ajax (HttpXmlRequest) to update the CreateSurvey.aspx page.

Related

How would I let 1 .aspx page use another .aspx page's code?

I have 2 aspx files in my project. The first.aspx page has some content on it and when I click on a button, it will launch a frame (second.aspx that only has code to show a calendar) on the same page.
Now once that calendar(second.aspx) loads on first.aspx, I want to click a link on the calendar that will .show() a hidden DIV on the first.aspx page.
How do I access code cross pages? In other words, how can I write some code in second.aspx that will affect first.aspx.
What you're asking for is not really possible. You're probably approaching it the wrong way. What you should do is turn your calendar page into a user control so that it can be used seamlessly in first.aspx.
Here is how to get started with user controls in asp.net:
After you turn it into a user control there are different approaches to getting access to the properties of the user control from your page. Here is one approach using the FindControl method.
Hope that helps.
The easiest solution would be to show and hide your div with jquery. Simple give your div a class like:
<div class="myCalendarDiv" style="display:none" />
And your Button should look like this:
<asp:Button id="myButton" OnClientClick="return ShowCalendar();" runat="server" />
<script type="text/javascript">
function ShowCalendar() {
$(".myCalendarDiv").show();
return false;
}
</script>
Another way would be instead of creating a seprate webpage for the calendar, as proposed you can use a jquery dialog, or make a usercontrol and embedd it on the same page.
(Posted on behalf of the OP).
So since I was dealing with an Iframe, I found out that you can target the parent window which would be first.aspx.
I used "window.parent.MYFUNCTION();" to call my JavaScript function on first.aspx and show the div.

Reload a Web Page Using C#

I have a web page that prompts for user input via DropDownLists in some table cells. When a selection is made the selection replaces the DropDownList so that the action can only be performed once. In the case that a change needs to be made I want to be able to click a button that reloads the page from scratch. I have Googled and Googled but I have not managed to find a way to do this.
Any advice is appreciated.
Regards.
Put a link on the page with the text "Reload" and the url the url of the page. It's perfectly valid to have a page with a link to itself.
If you don't like the link idea, use a standard Button and in the click event, use Response.Redirect to redirect to the current page.
You can set an OnClick for your button that resets each DropDownList's SelectedIndex to 0 instead of reloading the page from scratch. Alternatively, you can set a Response.Redirect([the page's url]) into the OnClick as is suggested here.

How to share html component amount different page

In ASP.NET, I have a master page, and five other pages which are under master page.
In all five pages, I need to implement a button, after clicking it, will generate different download results.
My original implementation is, in all five page, will all have a "Button", so in code behind will have corresponding event method to generate the download.
But my approach, will have duplicated html compoents.
Is there a way I can define the download button in my master page, when clicking button in different page, event method from different page will be invoked?
Yes you can, on master page you can have a Download Button and on it click check the URL, which will help you to identify the request has come from which page. Then you can write code on the basis of current page being displayed.
You can use the SHTML instead of HTML and include it every where that you need it.
I don't know what exactly You want to achieve, but maybe it would be better to have one aspx with the button and load the correct ascx file to this page according to some criteria? Each ascx can derive from some interface with some method, f.e. Click(), and each time the button on aspx is clicked, the Click() method from currently loaded ascx can be called.

How do I HTTP POST from an ASP.NET button?

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.

Passing master page form data to another webform

I have a master page with one form on it. It is a search form which must always be visible. When the button of that form is clicked I want the form data to be sent to search.aspx. The problem is, I don't know how. I cannot set the form action to search.aspx, because all my other pages which use the master form will go to search.aspx. This I don't want.
Hope someone can help me out :)
Thnx.
In order to pass the values of the control "txtSearch", when Server.Transfer is executed, you could do many things, including passing it via a querystring variable or setting up a session variable, and then check either of those in the Page_Load event of Search.aspx, and if it's populated, call the event that is fired when the user would hit the submit button on the Search.aspx page.
Also, if the Search.aspx file is using the same masterpage, then you can use this.Master.FindControl("txtSearch") to get the control (it you look a the source of the file after it is generated in the browser, you'll notice that controls in the master page aren't really called by their ID, rather that have something appended to them (i.e. it would now possibly be called "ctl00_txtSearch")
You could create your search form in a separate form, and get it to use GET instead of POST.
Either that, or have the master form handle the search button click and use Server.Transfer to go to the search form.
You can have multiple forms in one page I believe. So one form (your search form) would have its action set to search.aspx and the other would be set for the page itself.
ASP.NET webform pages only have one form (which would generally be included on the master page). You can set the postback url for the search button to your search page..
<asp:Button ID="btnSearch" runat="server" Text="Search" PostBackUrl="~/search.aspx" />
..or just redirect to it from the handler in your master page like this:
protected void btnSearch_Click(object sender, EventArgs e)
{
Response.Redirect(#"~/search.aspx?q=" + Server.UrlEncode(txtSearch.Text));
}
..or use Server.Transfer as suggested by David Kemp.
Note: If you use Request.Query[#"q"] on your search page to get your query, you don't need to use Server.UrlDecode() - it's done for you.
I would:
Add some code to the master page code-behind to detect the source of the POST.
Once I have the source of the POST (e.g. the Search box). I would then pass its query to the Search form.
I used a similar process with having a HTML login form on the master page.
I posted a question and subsequent solution here - check it out:
Form Elements in ASP.NET Master Pages and Content Pages
Once I got my head round it, it seemed a pretty simple and reasonably elegant solution.
The benefit of this is that you have complete control over how the data is sent to the search form. And you don't need to enable transfer of form data to the search form and all that nasty stuff, just create a new GET request to the search page and let it do what it is supposed to do :)
Hope this helps.
NOTE:
You can only have one form with runat="server" on an ASPX page. Additional forms MUST be HTML FORMS.
Because your search form is in the master page, you can probably structure it to contain 2 forms. Place the search form tags with the action set to "search.aspx" outside of the tag that is used by the rest of the site.
<body>
<form action="search.aspx>
<!--search box and submit button-->
</form>
<form runat="server">
<!--rest of page inc placeholder-->
</form>
</body>
If the structure of the page will not enable this, you can set the submit button's PosbackUrl to point to "search.aspx". In this case, "search.aspx" would need to be coded to look in the PreviousPage property for the form data, or use Request.Form to access the input.

Categories

Resources