I've been doing some really clever stuff (I think) in ASP.Net c#, so much so that the simple stuff is more difficult (if that makes sense)
I have this snippet of code in my page
<form id="form1" runat="server">
<asp:HiddenField runat="server" ID="hdnConfirm" value="Hello World" />
<asp:LinkButton runat="server" PostBackUrl="/confirm.aspx" Text="Confirm">
</asp:LinkButton>
</form>
I have this snippet of code in confirm.aspx.
if !(IsPostback)
{
lblConfirm.Text = Request.Form["hdnConfirm"]
}
I was expecting this to be nice and simple but when i click the button and go to page "confirm.aspx" the Request.Form has no values. What have I missed ?
[TESTING]
I ran a test on a brand new web forms project in VS2013. Dot.Net 4.5.1 This does not work. PreviouPage is always null. Whether surrounded by (!IsPostBack) or not. Doesn't matter if the submitting control is a Button, LinkButton or Hyperlink. Request.Form["hdn"] is also null. I have restarted my computer just in case and still no joy. I am missing something really simple I am sure of it but I can't see what
This is the first page nothing in the code behind
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton runat="server" PostBackUrl="~/WebForm2.aspx">click</asp:LinkButton>
<asp:HiddenField runat="server" ID="hdn" Value="3" />
</div>
</form>
</body>
</html>
This is the second page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication2.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string s = ((HiddenField)this.PreviousPage.FindControl("hdn")).Value;
}
}
}
On confirm.aspx use PreviousPage.FindControl instead :
HiddenField hdnFieldName = this.PreviousPage.FindControl("hdnConfirm") as HiddenField;
string hiddenValue = string.Empty;
if (hdnFieldName != null)
{
hiddenValue = hdnFieldName.Value;
}
Here is good example to get you started.
What's going on Here:
By default with VS 2013 and Asp.Net v4.5.1 , there is a feature FriendlyUrls which is enabled.
The FriendlyUrls feature does kind of Asp.Net routing , thus changing URLs from localhost/webform1.aspx to localhost/webfrom1
PostbackUrl property will not work in combination with Asp.Net Routing. When ASP.NET routing is in use the PreviousPage URL is the final routed URL. The runtime will check for a file represented by the Routed URL now, which will be webform1 and NOT webfrom1.aspx. Since there is NO such file as webform1, so it will always set the PreviousPage to null.
Possible Solutions:
So, now you know the issue at hand.
1.) Either don't use the Routing system of Asp.Net Friendly Urls , in this case, therefore try adding the <%# PreviousPageType VirtualPath="~/WebForm1.aspx"%> to webform2.aspx page and check.
2.) OR if you keep the FriendlyURLs and hence the Routing system, make changes in your code to read the Form values using some other alternatives.
Related
I have a web site in windows host = https://blobloblo/WebForm5.aspx
When run that link on FireFox developer & take a look at Console logs,
I will see this error :
Error: ASP.NET Ajax client-side framework failed to
Because of this error many parts of this web site do n't work correctly.
Yesterdays every thing was ok.
But today they closed the host for some hours & did some updates such as plesk's update.
After running the host again this error appeared.
What should i tell them to fix this problem?
WebForm5 ASPX :
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="Virtual_Visa_Cards.WebForm5" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" Height="26px" OnClick="Button1_Click" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
WebForm5 C# :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Virtual_Visa_Cards
{
public partial class WebForm5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "This is Ajax Call = CallBack";
}
}
}
Related Topic :
ASP.NET Ajax client-side framework failed to load. when put the ScriptManager on a blank page
I test all answers in the topic upper WITH NO RESULT.
In plesk i went to Web Application Firewall & put Web application firewall mode to off.
I worked one day to solve this issue.
I have googled and have search this forum, i can not find solution to my problem. As stated by the title in my webforms project using vs 2015 both these mentioned controls are causing a full page post back after uploading is done. In my other projects this works fine. But in this particular one i cant figure it out. Is there just a common thing I am over looking, I have created fresh pages with out using the master-page I still get the same results - full post back. I did not post my code because i am using the controls as any other programmer would.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="pic1.aspx.cs"
Inherits="SSWebAppV1._1.pic1" %>
<%# Register Assembly="AjaxControlToolkit"
Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server"
Width="500px" AllowedFileTypes="jpg,jpeg,png" MaximumNumberOfFiles="4"
OnUploadComplete="AjaxFileUpload1_UploadComplete" />
</div>
</form>
</body>
</html>
****code-behind***
protected void AjaxFileUpload1_UploadComplete(object sender,
AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
string fileName = Path.GetFileName(e.FileName);
AjaxFileUpload1.SaveAs(Server.MapPath("~/Eimages/" + fileName));
}
I am working on a project in C# .NET that allows me only one form in the .aspx file.
<form id="form1" runat="server" action="#1">
How can I change the form action through the C# code in a method?
I have tried this:
protected void Button1_Click(object sender, EventArgs e)
{
form1.Action = "#2";
}
but it didn't work. Thanks in advance...
Based on the comments to you question. asp:Panel Controls could help you out.
A very rough example
ASPX:
<form id="form1" runat="server">
<asp:Panel id="Form1" runat="server">
<!-- Form 1 Stuff -- >
<asp:Button ID="Button1" runat="server" Text="Submit"
OnClick="Button1_Click" />
</panel>
<asp:Panel id="Form2" runat="server" Visible="false">
<!-- Form 2 Stuff -- >
<asp:Button ID="Button2" runat="server" Text="Submit"
OnClick="Button2_Click" />
</panel>
</form>
C#
protected void Button1_Click(object sender, EventArgs e)
{
//HIde "Form"1
Form1.Visible = false;
//Show "Form"2
Form2.Visible = true;
//Do other stuff
}
protected void Button2_Click(object sender, EventArgs e)
{
//Do Final Processig
}
Also look at the DefaultButton property of the Panel
(Moving this to the top because it's an answer to the newly understood question. It's not how to change the form action, but how to have multiple forms.)
If you want a server form on a page that already has a server form then perhaps that second "form" should be a User Control. That way it sits inside the host page's server form but doesn't require its own form. And it's self-contained, able to contain whatever logic it needs when handling a postback.
Here's an example of a simple User Control. You can create one from Add > New Item > Web > Web Forms User Control.
<%# Control Language="C#" AutoEventWireup="true"
CodeBehind="OtherForm.ascx.cs" Inherits="WebApplication1.OtherForm" %>
<label for="<% = OtherFormTextInput.ClientID %>">
This is some other form on the same page
</label>
<asp:TextBox runat="server" ID="OtherFormTextInput"></asp:TextBox>
<asp:Button runat="server" ID="Submit" Text="Submit this other form"/>
It looks like an .aspx page but it has no form. It can still have its own code behind which can interact with with the other server controls it contains, just like an .aspx page would.
Then you add that control to your "main" page:
<%# Page Language="C#" AutoEventWireup="true"
CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!-- Register the user control -->
<%# Register TagPrefix="uc" TagName="other" Src="~/OtherForm.ascx" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<!-- This form has its own stuff, but also contains the "other" form. -->
<uc:other runat="server" ID="TheOtherForm"></uc:other>
</form>
</body>
</html>
I recommend this over using panels (which still works) because if you're putting two forms on one page, it's likely that you might at some point want to move the secondary form to another page or re-use it. This approach keeps it completely self-contained. Its code-behind isn't in the same file as the the .aspx page. You can place this on as many pages as you want.
Original "literal" answer which addresses the question as originally understood.
The runat="server" form exists entirely for the purpose of allowing ASP.NET to interact with the page and its server controls during postbacks. It's central to the way webforms works. If you change the action then technically what you have isn't a webforms page any more.
That's fine (I don't even like webforms) but it can lead to some weird behavior. If you have controls that trigger postbacks then normally they'd be handled on the same page and your user would just see a (hopefully) fast refresh. Now they might get sent to another page.
What if you just removed that form entirely and added your own form instead? Then your .aspx page will just behave more like an .html page.
Having added all the disclaimers about why not to do it, you can change the action using JavaScript. Here's a sample:
<%# Page Language="C#" AutoEventWireup="true"
CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" ClientIDMode="Static">
<asp:Button runat="server" text="Causes postback"/>
<asp:CheckBox runat="server" AutoPostBack="True"/>
</form>
<script>
document.getElementById("form1").action = "http://stackoverflow.com";
</script>
</body>
</html>
Some browsers might not allow changing the form's action.
I put the checkbox there just for fun (I must be really bored) to show the odd side effects it could have, that you might click on a checkbox and get redirected to a different page.
You can write a response.write() in your Asp.net side that print some javascript or jQuery code! As #Scott Hannen wrote some javascript like this :
Response.Write("<script>document.getElementById('YOURFORMID').action = 'YOUR URL';</script>");
or with jQuery
Response.Write("<script>$('#YOUR FORM ID').attr('action', 'YOUR URL');</script>");
btw if you have access to .html or .js files u can directly put this jQuery code without any C# code!
My page contains a user control and inherits a master page. Both have a button. I have put it in a form tag. I am just trying to do an event bubbling from master page as well as user control. Now it doesn't load because of multiple form tags error. How to do it?
Masterpage :
<%# Master Language="C#" AutoEventWireup="True" CodeBehind="SampleSite.master.cs" Inherits="EventBubbling.SampleSite" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
<asp:Button ID="Button1" runat="server" Text="MasterPageButton" />
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
User control :
<%# Control Language="C#" AutoEventWireup="True" CodeBehind="SampleUserControl.ascx.cs"
Inherits="EventBubbling.Controls.SampleUserControl" EnableViewState="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnUC" runat="server" OnClick="btnUC_Click" Text="UserControlButton" />
</div>
</form>
</body>
</html>
User control.cs page :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace EventBubbling.Controls
{
public partial class SampleUserControl : System.Web.UI.UserControl
{
public event EventHandler buttonClick;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUC_Click(object sender, EventArgs e)
{
buttonClick(sender, e);
}
}
}
Main Page :
<%# Page Language="C#" AutoEventWireup="True" MasterPageFile="~/SampleSite.Master"
CodeBehind="Default.aspx.cs" Inherits="EventBubbling._Default" %>
<%# Register Src="~/Controls/SampleUserControl.ascx" TagPrefix="uc" TagName="SampleUserControl" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<div>
<uc:SampleUserControl ID="UC1" runat="server" />
</div>
</asp:Content>
.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace EventBubbling
{
public partial class _Default : System.Web.UI.Page
{
Button btn = new Button();
protected void Page_Load(object sender, EventArgs e)
{
UC1.buttonClick += new EventHandler(UC1_buttonClick);
btn = this.Master.FindControl("Button1") as Button;
btn.Click += new EventHandler(btn_Click);
}
void btn_Click(object sender, EventArgs e)
{
Response.Write("EventBubbling from MasterPage");
}
void UC1_buttonClick(object sender, EventArgs e)
{
Response.Write("EventBubbling from User control");
}
}
}
My goal is to learn event bubbling but I get some basic error with this code I posted.
your user control should not have a < form> tag, this is taken care of by the MasterPage all pages / controls are then rendered inside this master control < form> tag.
If you want to have your user control with its own form you will have to change to a different MasterPage and to be honest I am not sure how that would even work.
Keep Id of form different every where you are using form tag.
every where its <form id="form1" runat="server">.
Make it as Id=1 , Id=2 ,etc.
In master page keep form Id=1 , in user control page make it as 2 and in main aspx make it as 3.
Or Remove form tag from usercontrol page.
Since you have placed controls in master page, you need to keep the <form> tag thereon.
The error message: A page can have only one server-side Form tag. should actually be taken as: A page can have only one visible server-side Form tag.
if you want you can have more than one form tags.
Read here one Great tricks of Asp.net: http://blogs.msdn.com/b/kaevans/archive/2005/10/19/482778.aspx . Definitely not of much practical use.
Else, You need to correct the Markup of your userControl. Remove tags like: head, html, form.
Your final markup of UserControl will be:
<%# Control Language="C#" AutoEventWireup="True" CodeBehind="SampleUserControl.ascx.cs"
Inherits="EventBubbling.Controls.SampleUserControl" EnableViewState="false" %>
<div>
<asp:Button ID="btnUC" runat="server" OnClick="btnUC_Click" Text="UserControlButton" />
</div>
I hope you get the idea what all needs to be removed.
All such pages or usercontrols that are going to be a part of this master page must not contain any <form> tag.
I am trying to pass a value from a JScript function to an ASP.NET Control but I am not able to make it work. What am I doing wrong here:
aspx.cs
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication5._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function saveMyData() {
var myData = "TestData";
var hiddenInput = document.getElementById('<%=HiddenField1.ClientID %>');
hiddenInput.Value = myData;
}
</script>
</head>
<body onload="javascript:saveMyData();">
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="HiddenField1" runat="server" />
<br><input type="submit" value="Press Me!"><br> <--New Code
</div>
</form>
</body>
</html>
Code Behind:
using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace WebApplication5
{
public partial class _Default : Page
{
protected HtmlInputHidden HiddenField;
protected void Page_Load(object sender, System.EventArgs e)
{
string value = HiddenField1.Value;
}
}
}
**UPDATE**
I added in a Submit button but I am still not able to assign the myData Value to the Hidden Control, any thoughts?
All you're missing is a postback. You will need the page to post back before it is able to read the newly inserted value into HiddenField1. The javascript is adding the value to HiddenField1 after Page_Load.
What are you trying to do? From what it looks like, you try to read out the value you set via Javascript (on the client side), but you never actually returned the data to the server.
You wont be able to read it without a PostBack, because the javascript does not get executed on the server side.
Add a button, make a Postback and see if you can read out the data then.