I have a HTML webform (NOT asp.net webform) that submits its form to an aspx script.
On the aspx script, I'd like to simply forward the form submission to a different form processing script. (after checking just one or two things using Request.Form["variable"])
What is the simplest way to forward the original html page's form submission?
Currently:
html1 -> aspx1 -> html2
Desired:
html1 -> aspx1 -> aspx2 -> html2
I would attempt to solve this problem in the following fashion
Create a Repeater
Set the Request.Form as the datasource of the repeater. If this does not work, i would convert Request.Form into a suitable datastructure, such as a Dictionary or Datatable for binding to the Repeater
Each repeater item would have an input tag, and would receive the appropriate name/id and value. I would not use a server-control input tag. I would emit the string in a more organic fashion.
I would then post to the second aspx page.
The purpose of the repeater is to build an equivalent Form NameValueCollection for processing on the second aspx page.
References
Cross Page Postback
Posting to another page
Binding a Dictionary to a Repeater
Binding Dictionary to DropDownList - (Note, "Key", "Value")
If it is only about simplest way then it is using session variable.
Just save your form values to the session and then you can access it anywhere in your application during the particular session.
So I would have my aspx1 page something like this
// ASPX1 page's Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form["variable"] == "SomeValue")//some condition
{
// save these values to session so that they will
// be available when I will be in aspx2 page
Session["FormValues"] = Request.Form;
Response.Redirect("ASPX2.aspx"); // your aspx2 page's link
}
}
And aspx2 page something like this
// ASPX2 page's Page_Load
protected void Page_Load(object sender, EventArgs e)
{
// form values from aspx1 page
NameValueCollection formValuesCollection =
(NameValueCollection)Session["FormValues"];
string variableValue = formValuesCollection["variable"];
// some processing using form values from aspx1 page
Response.Redirect("HTML2.html");
}
Related
I am using different user controls with Modalpopupextender (within page, repeater etc.) and it took some time to figure all the tricks how to get this two togeather.
I have now ended up with the following problem
When I pass a param into user control
UserControl.ParamID = 1;
I am able to receive it in user control, but at the strange step of page cycle.
When I try to do
protected void Page_PreRender(object sender, EventArgs e)
{
throw new Exception(ParamID);
}
I get null for ParamID!?
But when I do
<% throw new Exception(ParamID); %>
from html, I get a correct ParamID value.
I tryied to store ParamID as ViewState, as a HiddeField, and it is always doing the same error.
Also, when I use this user control wihout mpe, just in regular aspx page, it all works just fine.
What is the catch?
My .NET skills aren't great, but I'm stumped by a little issue and I can't find anything that explains this in a way I understand. Basically I'm trying to pre-populate a form with current values from a CMS, and have those values able to be updated when the form is submitted. It's essentially just an 'edit' facility for part of a website.
I have a usercontrol which contains some form inputs like this:
<label for="">Raised by</label>
<asp:TextBox ID="RaisedBy" runat="server"></asp:TextBox>
I then have a code-behind page which pulls values from a CMS and populates this form with the values already stored for this record, like this:
protected void Page_Load(object sender, EventArgs e)
{
// ...Some logic here gets the node from the CMS and I can pull property values from it. This part works fine.
string raisedBy = node.GetProperty("raisedBy").ToString();
// Populate form input with value from CMS. This works.
RaisedBy.Text = raisedBy;
}
So this is fine. But when the form is submitted it calls the following code:
protected void edit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
// Get edited value of input field
string RaisedByVal = RaisedBy.Text;
// Do some logic here to set up the CMS to be able to save the property - this works although it uses the original value of the form not the modified value if the user has changed it.
pageToEdit.getProperty("raisedBy").Value = RaisedByVal;
}
}
The problem is that the original form values are being saved back to the system rather than the modified values if the user has edited them.
Can anyone suggest what I'm doing wrong and how to get the modified values to be used rather than the original values?
Many thanks.
You have to check whether it is Postback or not in Page_Load() method:
So if you don't do this then on edit button click it will 1st call the Page_Load() and will again reset the original value. Later it will call the Edit click method and you will still see the original data.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// ...Some logic here gets the node from the CMS and I can pull
property values from it. This part works fine.
string raisedBy = node.GetProperty("raisedBy").ToString();
// Populate form input with value from CMS. This works.
RaisedBy.Text = raisedBy;
}
}
Typically I found the answer right after posting! :)
I needed to wrap the 'pre-populate form values' logic inside a:
if (!IsPostBack)
{
}
block.
I'm developing a C# ASP.NET application under VS2010. I want to generate a dynamic table by writing HTML text into a literal, including dynamically inserted buttons on my table. I know how to create dynamic ASP.NET controls, but I have a small problem with dynamic HTML controls. I've created an HTML button but I don't know how to give it a server-side click function.
Can I create dynamic ASP.NET controls and insert them in my literal? How can I get a dynamically created button to trigger a server-side event?
I've used OnServerClick property for defining my server side function but it has no effect, how can I use this value?
If you want post back on your button click, you will have to add __doPostBack function on it's onclick event, for this you can add a button like
StringBuilder dynamicHtml = new StringBuilder();
dynamicHtml.Append("Your Html Code");
dynamicHtml.Append("<input type='button' id='btn1' name='btn1' onclick='__doPostBack(\'btn1\',\'\')' value='Click Here' />");
when you click on it, it will post back, and you can check Request.Form["__EVENTTARGET"] to find who post back the page.
By checking Request.Form["__EVENTTARGET"] in Page_Load event handler, you can call your any server side method like
protected void Page_Load(object sender, EventArgs e)
{
var eventTarget = Request.Form["__EVENTTARGET"].ToString();
if(eventTarget == "btn1")
{
CallMethod1();
}
}
private void CallMethod1()
{
//Code which you want to run
}
I have a problem regarding to asp.net lifecylce hierarchy.
Basically, I have one user control which has a GridView in it. And this GridView is dynamically generated based on a public property on the control (named Parameter in the simplified code below).
Everything is fine when I insert this control in an aspx page, set its Parameter property and call DataBind(Parameter) on it. GridView is generated and populated on the UI.
Problem occurs when I post-back the page. I have to regenerate the GridView structure so that the data in the ViewState of the control can be used to populate the GridView. So that I can achieve its content. But as long as the GridView structure is generated dynamically and it is based on the Parameter property set on it, this is not possible. Because the OnInit of the user control is called before the OnInit of the page and that is why the Parameter property is set after the generation of the GridView structure. As a result I get an empty Gridview in the end.
Here is the code in a simplified way.
Can you give me some recommendation how to overcome this?
Can I explicitly force asp.NET to re-load the ViewState of a gridview?
Page HomePage.aspx has an OnInit event handler in such a way, where it sets a property of the user control ctlMyUSerControl
protected override void OnInit(EventArgs e)
{
ctlMyUserControl.Parameter = new Parameter()
name="Orhan",
surname= "Pamuk"};
}
And in ctlMyUserControl's OnInit I have
protected override void OnInit(EventArgs e)
{
if (Page.IsPostBack && Parameter !=null && SomeGridViewRowsExistOnUI)
{
// Generate dynamic columns based on Parameter property
// So that gridview can be populated
// with the post-backed data which
// should contain the ViewState of the gridview
GenerateGridViewColumns(Parameter);
}
base.OnInit(e);
}
I have sold it guys.
What I have done is regenerating my GridView columns on the container page of the user control.
So, On the OnInit of the page I regenerate my columns and it is still before the call of LoadViewState() method.
protected override void OnInit(EventArgs e)
{
Parameter parameter = new Parameter()
name="Orhan",
surname= "Pamuk"};
ctlMyUserControl.GenerateGridViewColumns(parameter);
}
I have a page and clicked on the button there it will open a new page containing some text boxes, user fill all the text boxes and clicked the button now first page open again and the question is : How can I get the vales of text boxes on the current page using both server-side and client-side
There is a restrictions to use of :
- Cross-paging
- Cookies
- Sessions
- Query strings
Server-side approach:
An alternate approach is to use Server.Transfer method.
On the current page:
protected void Transfer_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
Server.Transfer("destination.aspx");
}
}
On the destination page:
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
TextBox textBox = PreviousPage.FindControl("Parameter")
as TextBox;
if (textBox != null)
{
string parameter = textBox.Text;
Parameter.Text = parameter;
}
}
}
But Server.Transfer does come with disadvantages. The most serious is that the URL in the browser does not change. The browser still believes it has posted back and received content for the first web form, so history and book-marking suffer.
Client-side approach:
In real world I don't recommend to use this solution, this is only a workaround.
In two words: use window.name property on the client side. This property is available across page reloads it is a sort of session.
For more information see:
What’s in a window.name?
window.name Transport
Hope, this helps.
If you are using the PostBackUrl property of the button on the submitting page, you can access the controls for the "previous page" from the action page by using the following:
Dim txtBox as TextBox
txtBox = CType(Page.PreviousPage.FindControl("MyTextBox"), TextBox)
Then you'll have access programmatically to all of the properties and data for that control.
Use the cache if you can't use the Session, Querystring, Cookies and Cross Page posting.
If it is simple/primitive data, then you can go for the ViewState. Or Cache is a better option.
One more option is to have a Page level public variables, set the required values and redirect to the page for further processing.(This is not a good approach to follow)