Getting values posted by a form on target page ASP.NET - c#

I am new to asp.net and learning it to my own. I just want to submit a form on another asp.net page and want to retrieve all the posted values on that page! I have tried the following code (I am just testing and learning asp.net to my own so this code may have some blunders).
My Default.aspx page (submitting the form with values):
<body>
<form id="form1" runat="server" action="formtarget.aspx" method="post" onsubmit="return Validate()">
<div>
<asp:Label ID="namelab" Text="Your Name" runat="server"></asp:Label>
<asp:TextBox ID="namebox" runat="server"></asp:TextBox>
</div>
<div>
<asp:Label ID="agelab" Text="Your Age" runat="server"></asp:Label>
<asp:TextBox ID="agebox" runat="server"></asp:TextBox>
</div>
<div>
<asp:Button ID="submitbutton" Text="Submit" runat="server"/>
</div>
</form>
</body>
The formtarget.aspx
<body>
<form id="form1" runat="server">
<div>
You Entered The Following Details!<br />
Your Name: <asp:Label ID="namelab" runat="server"></asp:Label><br />
Your Age: <asp:Label ID="agelab" runat="server"></asp:Label>
</div>
</form>
</body>
The formtarget.aspx.cs (here i want to access the posted values by Default.aspx page form)
public partial class formtarget : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String name = Request.QueryString["namebox"];
String age = Request.QueryString["agebox"];
namelab.Text = name;
agelab.Text = age;
}
}
The code is working fine for me but the page formtarget.aspx is not showing up any values.
I know that I can use the Default.aspx.cs to get my form values but I am just learning how can I post my form to another page.
Thanks

Request.QueryString is used to access parameters passed using GET; to access parameters passed via POST you should use Request.Form:
public partial class formtarget : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String name = Request.Form["namebox"];
String age = Request.Form["agebox"];
namelab.Text = name;
agelab.Text = age;
}
}

Related

How to set a paragraph text dynamically in ASP.NET?

I am creating a website in asp.net. My website has an admin page. The admin will use this page to daily update website's content. This content will get reflected to the main website. I have learned from the following link that how we can pass values from one page to another-
How to pass values across the pages in ASP.net without using Session
I am using Application variable.
Admin.aspx
<form runat="server">
<div>
<asp:TextBox id="DailyMsgID" name = "DailyMsgName" runat="server"></asp:TextBox>
<asp:Button id="b1" Text="Submit" runat="server" OnClick="b1_Click" />
<asp:Label ID="Label_DailyMsgId" name="Label_DailyMsgName" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
Admin.aspx.cs
protected void b1_Click(object sender, EventArgs e)
{
Label_DailyMsgId.Text = DailyMsgID.Text;
Application["DailyMessage"] = Label_DailyMsgId.Text;
}
Home.aspx
<!-- Page content-->
<div id="div1" class="container-fluid">
<h1 id="myhead" class="mt-4">Welcome to the Official Website of ABC</h1>
<p id="DailyMessage"></p>
</div>
To set the paragraph, I want to do something like below. But it is not recognising the paragraph Id.
Home.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
DailyMessage.Text = Application["DailyMessage"].ToString();
}
How do I set the paragraph?
Both Admin and Home page are under the same solution.
This issue got resolved. I was missing
runat="server"
Here is the updated code -
Home.aspx
<p id="DailyMessage" runat="server"></p>
Home.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
DailyMessage.InnerText = Application["DailyMessage"].ToString();
}

how to display the first form data in second page using hidden field? [duplicate]

This question already has answers here:
How To Pass Dynamically created value into another page..?
(1 answer)
cross page posting
(1 answer)
Cross-page posting. Is it a good pratice to use PreviousPage in Asp.net?
(1 answer)
Transferring DropDownList values from one page to the next
(3 answers)
Closed 2 years ago.
I m working with hidden field
I want to display the first page data in second page using hiddenfield
first.aspx
<body>
<form id="form1" runat="server">
Name:<asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />
Address:<asp:TextBox ID="txtaddress" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Next" OnClick="Button1_Click" style="height: 26px" />
<asp:HiddenField ID="hdnname" runat="server" />
<asp:HiddenField ID="hdaddress" runat="server" />
</form>
</body>
first.aspx.cs
public partial class DemoHidden : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
hdnname.Value = txtname.Text;
hdaddress.Value = txtaddress.Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
second.aspx
<body>
<form id="form1" runat="server">
nameget:<asp:Label ID="lblname" runat="server" Text="Label"></asp:Label><br />
addressget:<asp:Label ID="lbladdress" runat="server" Text="Label"></asp:Label>
</form>
</body>
second.aspx.cs
public partial class Second : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//lblname.Text=txtname //here I want to display the name and addresss
}
}
how to display the name and address when user fill up the first webform and display data in second webform page using hiddenfield?
help?

Dynamically Create a specific div with its children in C#

I'm sorry in advance if this seems to be a beginners question and wasted some of your time, but it does really bother me and I would really appreciate it if someone could help.
I'm simply creating a "zone" if you will, where the user inputs a word/sentence which are tasks, and then can check if the task was completed or not and/or if the word/sentence needs to be changed.
So in the end, what I need is a textbox lets say where the user inputs a specific number supposed x, and generates x times this div.
Here is the code (unfortunately I could not post pics since I do not have enough reputation..)
ASP.NET code
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GMode.tiz.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>whatevz</title>
<link href="StyleSheet1.css" rel="stylesheet" />
<script src="../jquery-1.11.3.min.js"></script>
<script src="../functions.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div class="singleContainer">
<div class="lblPlace">
<asp:Label ID="lbl" runat="server" Text="Input Task"></asp:Label>
<asp:TextBox ID="hi" CssClass="txtBox" runat="server"></asp:TextBox>
</div>
<asp:Button ID="save" runat="server" CssClass="UniversalBtn" OnClick="btnCheck_Click" />
<asp:Button ID="cancel" runat="server" CssClass="UniversalBtn" OnClick="btnCancel_Click" />
<asp:Button ID="edit" runat="server" CssClass="UniversalBtn" />
<asp:Button ID="submit" runat="server" CssClass="BtnSubmit" OnClick="btnSubmit_Click" Text="DONE"/>
</div>
</form>
</body>
</html>
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace GMode.tiz
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnCheck_Click(object sender, EventArgs e)
{
lbl.CssClass = "done";
}
protected void btnCancel_Click(object sender, EventArgs e)
{
lbl.CssClass = "undone";
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (hi.Text == "")
{
lbl.Text = "No Value";
}
else
{
lbl.Text = hi.Text;
}
}
}
}
So basically I want to dynamically create <div class="singleContainer"> with everything inside it x times (x defined by the user).
I'm going about this more on the fact like it's C++. When you can dynamically create an array with a specific number x.
Please tell me if you need more of the code. There are still the css and js files that I did not post.
Thank you,
Jack
I think that for this the best option is to use AngularJS, you can do something like this:
<html>
<head>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-resource.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-route.min.js"></script>
<script>
var app = angular.module('app', [])
app.controller('PostsCtrl', function ($scope) {
$scope.change = function (){
$scope.listDivs = [];
for(var i=0;i<$scope.createDiv;i++) {
$scope.listDivs.push(i);
}
}
})
</script>
</head>
<body ng-app='app'>
<div ng-controller='PostsCtrl' class='container'>
<input ng-model='createDiv' ng-change="change()"/>
<ul class='list-group'>
<li ng-repeat="post in listDivs" class='list-group-item'>
<div class="singleContainer">
<div class="lblPlace">
<asp:Label ID="lbl" runat="server" Text="Input Task"> </asp:Label>
<asp:TextBox ID="hi" CssClass="txtBox" runat="server"> </asp:TextBox>
</div>
<asp:Button ID="save" runat="server" CssClass="UniversalBtn" OnClick="btnCheck_Click" />
<asp:Button ID="cancel" runat="server" CssClass="UniversalBtn" OnClick="btnCancel_Click" />
<asp:Button ID="edit" runat="server" CssClass="UniversalBtn" />
<asp:Button ID="submit" runat="server" CssClass="BtnSubmit" OnClick="btnSubmit_Click" Text="DONE"/>
</div>
</li>
</ul>
</div>
</body>
</html>
Put your div into a usercontrol (ascx file).
In your main page add a placeholder:
<asp:PlaceHolder runat="server" ID="PlaceHolder1" />
in code behind you can add your control as often as you want:
yourControl= LoadControl("~/Controls/yourControl.ascx");
PlaceHolder1.Controls.Add(yourControl);

How can I prevent C# from redrawing a <div> I used jQuery to show

Scenario: I have a modal-style div that will be shown when the user clicks a button. At the time the div is shown, I need to get some data from the back-end to fill in some fields. Additionally, I'd like to use the jQuery method I use for all my modal windows (fades in the modal div, displays a background div as well as enabling the use of ESC key or "click offs" to close the modal).
It looks something like this:
<asp:ScriptManager ID="sc" runat="server" />
<asp:UpdatePanel ID="updForm" runat="server">
<ContentTemplate>
<h4>Testing jQuery calls combined with code behind actions</h4>
<div class="pad-content">
<asp:LinkButton ID="lnkShowIt" runat="server" OnClick="lnkShowIt_Click" Text="Load Form" OnClientClick="showForm()" />
<asp:Panel ID="pnlPopup" ClientIDMode="Static" runat="server" CssClass="box-modal" style="width:500px;display:none;z-index:1001">
<div class="header">Edit Estimate X</div>
<div class="content">
<div class="window">
<h5>Test Form</h5>
<asp:TextBox ID="tbxTime" runat="server" />
<br />
<asp:TextBox ID="tbxText" runat="server" Width="150px" />
<br />
<asp:LinkButton ID="lnkValidate" runat="server" CssClass="link-button-blue" Text="Validate" OnClick="lnkValidate_Click" />
</div>
</div>
</asp:Panel>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<div id="backgroundPopup"></div>
So ... lnkShowIt calls both the jQuery (which will show pnlPopup) as well as the C# (which will populate tbxTime).
jQuery method actually just calls another method from a common js library I have that does the modal window stuff - I don't think that actual code is the problem but here is the simple function used for this page:
<script type="text/javascript" language="javascript">
function showForm() {
loadPopup('#pnlPopup');
}
</script>
Code behind methods look like this:
protected void lnkShowIt_Click(object sender, EventArgs e)
{
tbxTime.Text = System.DateTime.Now.Second.ToString();
}
protected void lnkValidate_Click(object sender, EventArgs e)
{
if (tbxTime.Text == tbxText.Text)
{
Response.Redirect("DynamicBoxesWithJQuery.aspx?mode=success");
}
else
{
tbxText.Style["border"] = "1px solid red";
}
}
I'm able to generate some level of success by doing the following but it seems like just a major hack and I have to assume there's a better approach:
protected void lnkShowIt_Click(object sender, EventArgs e)
{
tbxTime.Text = System.DateTime.Now.Second.ToString();
ScriptManager.RegisterStartupScript(this, this.GetType(), "OpenEditor", "<script type='text/javascript'>loadPopup('#pnlPopup');</script>", false);
}
protected void lnkValidate_Click(object sender, EventArgs e)
{
if (tbxTime.Text == tbxText.Text)
{
Response.Redirect("DynamicBoxesWithJQuery.aspx?mode=success");
}
else
{
tbxText.Style["border"] = "1px solid red";
ScriptManager.RegisterStartupScript(this, this.GetType(), "OpenEditor", "<script type='text/javascript'>loadPopup('#pnlPopup');</script>", false);
}
}
It seems like it should be easier than this, but the way the UpdatePanel keeps redrawing (and thus resetting the display:none on pnlPopup) is really causing me fits.
Thanks in advance
Solution I just found: putting the LinkButton in its own UpdatePanel and then the form in its own UpdatePanel and making sure the div that is the actual popup box is not in an UpdatePanel at all.
<h4>Testing jQuery calls combined with code behind actions</h4>
<div class="pad-content">
<asp:UpdatePanel ID="updLink" runat="server">
<ContentTemplate>
<asp:LinkButton ID="lnkShowIt" runat="server" OnClick="lnkShowIt_Click" Text="Load Form" OnClientClick="showForm()" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Panel ID="pnlPopup" ClientIDMode="Static" runat="server" CssClass="box-modal" style="width:500px;display:none;z-index:1001">
<div class="header">Edit Estimate X</div>
<div class="content">
<asp:UpdatePanel ID="updForm" runat="server">
<ContentTemplate>
<div class="window"style="min-width:500px;">
<h5>Here is a Test Form</h5>
<label>Time:</label>
<asp:TextBox ID="tbxTime" runat="server" />
<br />
<asp:Label ID="lblText" AssociatedControlID="tbxText" runat="server" ViewStateMode="Disabled">Text:</asp:Label>
<asp:TextBox ID="tbxText" runat="server" Width="150px" />
<br />
<asp:LinkButton ID="lnkValidate" runat="server" CssClass="link-button-blue" Text="Validate" OnClick="lnkValidate_Click" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</asp:Panel>
</div>
Seems to do the trick without any Script Registers from the codebehind

iFrame in UpdatePanel not updating

I'm trying to update the source of an iFrame when a button is clicked on the page. However, nothing seems to be happening...
.ASPX:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<div class="accountInfo">
<p class="submitButton">
<asp:Button ID="ValidateButton" runat="server" Text="Validate" OnClick="SubmitSponsor" />
</p>
</div>
<iframe runat="server" id="PayPalFrame" height="150px" width="100%" frameborder="0"></iframe>
</ContentTemplate>
</asp:UpdatePanel>
Code Behind:
protected void SubmitSponsor(object sender, EventArgs e)
{
var driver = new Driver();
var isvalid = driver.IsAppValid(URL.Text, APILINK, Connstring);
if (isvalid)
{
PayPalFrame.Attributes.Add("src", "http://www.google.com");
URLInvalid.Visible = false;
}
}
I've also tried:
PayPalFrame.Attributes["src"] = "http://www.google.com"; and still nothing.
I have tested your code, and it works in a sample project:
ASPX:
<form id="form1" runat="server">
<div>
<asp:ScriptManager runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<div class="accountInfo">
<p class="submitButton">
<asp:Button ID="ValidateButton" runat="server" Text="Validate" OnClick="SubmitSponsor" />
</p>
</div>
<iframe runat="server" id="PayPalFrame" height="150px" width="100%" frameborder="0">
</iframe>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
Codebehind:
public partial class UpdatePanelIFrameTester : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SubmitSponsor(object sender, EventArgs e)
{
bool isvalid = true;
if (isvalid)
{
PayPalFrame.Attributes.Add("src", "http://www.ironworks.com");
}
}
}
I would inspect your code and ensure nothing is causing your page to refresh/perform a full postback (I changed the URL as google will not allow you to view it in an iframe under standard conditions).
I believe you have to remove the src attribute and then re-add it, for this to work. Not sure why this is, but I remember doing it the last time I worked with update panels. Perhaps it's because the src attribute always already exists?
Anyway, here's some sample code: how to change the src attribute in iframe.

Categories