page1.aspx
<input type="text" runat=server id="text1"/>
page2.apx
protected void btnCreateMember_Click(object sender, EventArgs e)
{
text1.text="test";
}
How can I change Input on Page1.aspx through Page2.aspx Method btnCreateMember_Click
I think that you are missing some very basic concepts about ASP.NET MVC here. In ASP.NET MVC there is no such thing as runat="server", nor any callbacks. There is no ViewState nor PostBacks. Even if it is based on ASP.NET, ASP.NET MVC is a fundamentally different framework. In ASP.NET MVC you have Models, Controllers and Views. So if you want to send values from one view to another controller action you could use a form with HTML helpers:
<% using (Html.BeginForm("someAction", "someController")) { %>
<%= Html.TextBoxFor(x => x.SomeProperty) %>
<input type="submit" value="OK" />
<% } %>
I would recommend you going through the tutorials here: http://asp.net/mvc to familiarize yourself with the basic notions of MVC.
I see the MVC tags have been removed.
This isn't how webforms work.
If the user is on Page 2 and clicks the "Create Member" button, then you should perform the logic to "create a member" there and redirect as necessary.
If you redirect from Page2 to Page1.aspx, then the page_load event of Page1.aspx should load whatever data it needs and populate any necessary page controls itself.
In other words, each page should be encapsulated to pull, display, and update the data it needs.
Related
I have this at the bottom of my page:
<form id="Login" method="post" runat="server">
<span class="MenutextWhite">You are logged in as
<asp:Label ID="lblUsername" Runat="server"></asp:Label>
- not you? </span>
<asp:LinkButton CssClass="MenutextWhite" ID="btnLogout" Runat="server" Text="Log out"
OnClick="Logout_Click" tip="click here to log out">
</asp:LinkButton> | <a href = "admin.aspx"
class="MenutextWhite">Admin</a>
</form>
this deals with displaying who the user is and giving access to the admin section and logoff button. (I am not using masterpages)
However further up the page, in the main content area, I need another control (freetextbox)
How can I get around this single form only problem?
you can only have one form with a server side tag. however you can have as many html forms (without the runat=server) tags. The runat=server tag allows the webforms page life cycle and view stuff to work.
if you want a more traditional web development experience don't use webforms. Instead use an mvc framework (FUBU, Monorail, MSMVC, etc)
As Jason Meckley says - you can't. But in ASP.Net forms I've never needed it.
You have two buttons for two different parts of the form - so you will have 2 different click handlers so can take two different actions
protected void ptnFormPart1_Click(object sender, EventArgs e)
{
//.. action 1
}
protected void ptnFormPart1_Click(object sender, EventArgs e)
{
//.. action 2
}
Therefore you don't need two different forms with two different actions
Solved!
The 2 Forms that I required were in different DIV's.
as i can only have one form,
I put the tags just inside the - outside all contents.
Now everything works ok.
I am new in developing using asp.net.
I use java(struts2) to do the web application before,it is easy to control this use the jsp tag or struts2 tags:
For example(suppose the "do something" operation need use login):
<s:if test="#session.user!=null">
<span>do something</span>
</s:if>
So if a user did not login,he will never see the menu of "do something".
Now I wonder how to make it in the asp.net?
Is this controled in the xx.aspx.cs?
Thanks.
There are several ways to handle "membership" in asp.net. Microsoft has the Membership Provider as a built in solution. It seems however you have gone with a failrly simple one of your own devising.
You could handle your problem two ways using what you've already got.
In the aspx page you could have:
<% if(Session["user"] != null) { %>
<span>Do Something</span>
<% } %>
Preferably move the logic to the code behind page (.aspx.cs)
In your aspx page have
<span id="thisSpan" runat="server">Do Something</span>
Then in yout code behind page in the onPage Load event
thisSpan.Visible = Session["user"] != null;
on the page products.aspx i created a form:
<form id="send_info_form" method="post" action="send_email.aspx">
<input type="text" name="the_name />
<input type="submit" />
</form>
when i click on submit it's forwarded to the same page (products.aspx) and not to the page i set in action attribute of the form.
It looks like you have a misunderstanding about how ASP.NET's logic works- ASP.NET has a much different paradigm than PHP or ASP does.
It seems like you're taking more of an ASP classic or PHP approach of directly handling the landing pages of forms and POST values, which you no longer have to do. You shouldn't need a separate page to handle the logic of the form submission either; this is all handled by event handlers in the submitting page's codebehind.
Instead of handling input elements directly, you should use ASP.NET's server controls to handle all the inputs for you.
What you should be doing is:
In the Products.aspx page:
E-mail Address: <asp:TextBox runat="server" ID="txtEmail" />
<asp:Button runat="server" ID="btnSubmit" OnClick="btnSubmit_Click" Text="Submit" />
Note that there's no form tag required (besides the one already provided to you when you first make the ASPX page.
Since you're working with an object-oriented language with a business objects representing all of your HTML elements with ASP.NET, you don't have to handle reading from the POST values of the form directly.
In the codebehind for Products.aspx (I'm assuming C#, so Products.aspx.cs), add a method for btnSubmit_Click:
protected void btnSubmit_Click(object sender, EventArgs e) {
string sendEmailTo = txtEmail.Text;
// insert mail sending logic here
}
In ASP.NET, the tag will by default always post itself to the same page. This allows you to handle any events in the codebehind in the ASPX page. It's not clear from your question what exactly you're trying to do. I can think of three possible scenarios:
1) You want to post back to the same page, and toggle visibility of UI elements (other panels, etc.) based on the result of the processing, or redirect the user to a second destination page once processing is complete. This is the most common scenario and the approach I recommend be taken, because it keeps all the logic regarding the processing of the form in one place.
2) You can specify a PostBackUrl to specify another (ASP.NET) page for button controls and whatnot. From there you can do the processing from elements on the first page on the second page using the PreviousPage property. See http://msdn.microsoft.com/en-us/library/ms178139.aspx for more information.
3) If you have a separate page you wish to post to that you don't control that's not ASP.NET-based (i.e., another site's form, or a PHP/ASP3.0 site you run), things get significantly more difficult. ASP.NET puts everything in one giant elements. Since tags cannot reliably be embedded within each other in HTML, you will either have to do a silent POST manually from your codebehind, or use Javascript to quietly submit an ajax request upon submission.
I have a web application that has a page that loads the content from the database. I want to be able to put a form in the dynamic content, but .net doesn't let the inside form perform it's action. Is there a way to allow this or some other way I can get a form on a dynamic content page?
--EDIT--
I think I need to clarify something. This is an aspx page that loads content from the database. As far as I know, the text I pull from the db and stick in the Label is never compiled or processed by the .net wp, thus I can't use the code behind to fix this issue.
This is a common problem, when you want to have a non-postback form to a 3rd party site (like a PayPal button, for example).
The problem occurs because HTML doesn't let you have form within a form, and most ASP.NET pages have a <form runat="server" /> "high up" in the HTML (or in the Master page).
My favorite solution is to hide the "high up" form tag, while still showing all of the content. Then you can feel free to dump any tags you want in the body. If you do this dynamically you can choose on a page-by-page basis which pages have custom forms.
I created a class called GhostForm.cs to handle this. You can read all about it here:
http://jerschneid.blogspot.com/2007/03/hide-form-tag-but-leave-content.html
There can only be one form on the page (the asp form); you have to use that form somehow.
To clarify, there can only be one form processed.
Not with webforms, no. You have to work within the one, full page form by using an event handler connected to a Button to LinkButton. Fortunately, it's pretty easy to do:
foo.aspx:
...
<asp:TextBox id="txtFoo" runat="server" />
<asp:Button id="btnFoo" runat="server" onclick="btnFoo_Click />
...
foo.aspx.cs:
...
protected void btnFoo_Click(object sender, EventArgs e)
{
string s = txtFoo.Text;
// do something with s
}
...
Dino Esposito has an article from MSDN magazine that covers handling multiple forms or "simulating" sub forms in ASP.Net that might just answer all your questions.
http://msdn.microsoft.com/en-us/magazine/cc164151.aspx
Any work around would be hacky and very ugly. By design asp.net uses a form tag to post and get data. This is why they call it a Web Forms Application. Html does not allow nested forms. What you want to do is use a WebRequest in your code behind.
If you are trying something like a paypal button you could simply use something like this.
Markup:
<div id="PayPalButtonContainer" runat="server"></div>
Code Behind:
public static string GetPayPalButtonMarkup()
{
const string markup = #"https://www.paypal.com/cgi-bin/webscr
?cmd=_xclick&business={0}
&item_name=Widget
&amount={1}
¤cy_code=USD";
return markup;
}
PayPalButtonContainer.InnerHtml = string.format(GetPayPalButtonMarkup,"YOUR PAYPAL USER NAME", "YOUR PRICE VALUE");
you either have to deal with the postback by adding a server side click event handler to what you want to be the "sub forms" submit button (this is how web formas deals with multiple submit type buutons on the same page) or do soemthing clever with AJAX if you dont want a full post back
I've run across this issue before. One workaround that I have done is to place my code that I want my action to be done upon inside of an asp:Panel. With the panel you can set the attribute of "DefaultButton" to a button inside of the panel, and clicking the button (or pressing "enter") will fire that button's click event. I've found this quite handy when wanting to submit a "form" by pressing enter when I have a master page that contains the only allowable asp:Form.
Hope this helps.
When I first came across this problem, I found the simplest solution for me was to simple COPY and PASTE the Master page and give it a slightly different name, something like:
SiteNameMasterPage 'Default page with FORM tag
SiteNameMasterPageNF 'No Form tag
And then depending on wether I wanted a FORM tag or or not, simply change the masterpage link at the top of my CONTENT-PAGES, like this
<%# Page Title="" Language="VB" MasterPageFile="~/SiteName.master" %>
<%# MasterType VirtualPath="~/SiteName.master" %>
<!-- This masterpage has the default FORM tag -->
or
<%# Page Title="" Language="VB" MasterPageFile="~/SiteNameNF.master" %>
<%# MasterType VirtualPath="~/SiteNameNF.master" %>
<!-- This masterpage does NOT have the default FORM tag -->
and then in the content page, wherever I want to place my form I can include the <form> tag
Previously, I was passing information to a Silverlight control inside of the Page_Load method. i.e.
protected void Page_Load(object sender, EventArgs e)
{
MainContainer.InitParameters = "info=" + CurrentUserID.ToString();
}
In MVC, without the concept of a code-behind, I've passed the user ID as part of the model, but the following code doesn't seem to be working:
<asp:Silverlight id="MainContainer"
InitParameters="info=<%= Model.CurrentUserID %>" runat="server"
Source="~/ClientBin/SilverlightControls.xap" ... />
If you look at the code that gets rendered to the browser, it is escaping the MVC tags, so it gets sent like this:
value="info=<%= Model.CurrentUserID %>"
If I try hardcoding InitParameters="info=1", it works.
Don't use the <asp:Silverlight runat="server" /> control, it's a webforms control and doesn't really belong in the MVC world. You need to create the <object> tag yourself - maybe create your own Html helper extension method to create it.
Have a look at this blog post:
Integrating Silverlight and ASP.NET MVC
HTH,
Charles