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();
}
Related
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?
I have a master page and an user control on it.
I have a two buttons in the user control for create or remove session, and a label that shows a session text, when click the buttons nothing happens and user control doesn't update I should refresh the page,
Would you please anybody help me to fix this issue ?
This is my master page markup:
<form runat="server">
<div>
<!--previous codes-->
<nav class="navigation">
<div class="wrapper">
<controller:menu runat="server" ID="menu" />
<controller:user runat="server" ID="user" />
</div>
</nav>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
<!--Next Codes-->
</div>
</form>
and this is my user control
<ul class="nav signup">
<li class="no-drop-down">
<asp:Label ID="_signupbutton" runat="server" CssClass="sr">Test</asp:Label>
<div class="signup-dropdown">
<asp:PlaceHolder ID="_defaultuser" runat="server" Visible="false">
<div class="notloggeduser">
<asp:LinkButton ID="LinkButton1" runat="server" CssClass="usercontroller"
OnClick="LinkButton1_Click" Text="????">
</asp:LinkButton>
</div>
</asp:PlaceHolder>
<asp:PlaceHolder ID="_signedup" runat="server" Visible="false">
<div class="defaultuser">
<ul>
<li>
<asp:LinkButton ID="_userlogout" runat="server" CssClass="usercontroller" OnClick="_userlogout_Click">
<i class="fa"></i>????
</asp:LinkButton>
</li>
</ul>
</div>
</asp:PlaceHolder>
</div>
</li>
</ul>
and this is user control codebehind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["signup"] == null)
{
_signupbutton.Text = "??? ??? / ????";
_defaultuser.Visible = true;
}
else
{
_signupbutton.Text = "<i class=\"fa\"></i> " + Session["signup"].ToString();
_signedup.Visible = true;
}
}
}
protected void _userlogout_Click(object sender, EventArgs e)
{
Session.Remove("signup");
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Session.Add("signup", "????? ??????????");
}
Your order of events is a bit messed up here.
Page_Load will execute before the event handlers for the click events. You need to perform the setup of the button state from the event handlers, as if you do it in page load your session object wont have been updated yet.
You will need to do a few things here (this isn't tested but should show you the logic):
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) // This will run when the page is loaded but not from a post back / button click etc (e.g. from your page refresh)
{
this.SetButtonState();
}
else
{
// use this for things you want to happen on postback only
}
}
private void SetButtonState()
{
if (Session["signup"] == null)
{
_signupbutton.Text = "??? ??? / ????";
_defaultuser.Visible = true;
}
else
{
_signupbutton.Text = "<i class=\"fa\"></i> " + Session["signup"].ToString();
_signedup.Visible = true;
}
}
protected void _userlogout_Click(object sender, EventArgs e)
{
Session.Remove("signup");
// Update your button state
this.SetButtonState();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Session.Add("signup", "????? ??????????");
// Update your button state
this.SetButtonState();
}
I've had a look at lots of posts and I feel like I'm going crazy as nothing I've tried seems to work. I simply want to click a button and retrieve the value of a textbox.
I'm using web forms with a site.master so not sure if this could be affecting the problem, but most of the solutions I've seen don't appear to be using a site.master.
I'm not binding the textbox, initially I just wanted to create a contact form.
<%# Page Title="About" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="About.aspx.cs" Inherits="Blackburn_Pulse.About" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<script>$("#menuAbout").addClass("navi-active");</script>
<div class="contentBody">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="tb_Test" EnableViewState="true" CausesValidation="false" runat="server"></asp:TextBox>
<asp:LinkButton ID="btn_Test" runat="server" OnClick="btn_Test_Click" Text="send test" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn_Test" EventName="click" />
</Triggers>
</asp:UpdatePanel>
</div>
</asp:Content>
public partial class About : Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Page.Master.EnableViewState = true;
if (IsPostBack)
{
var test_var = tb_Test.Text; //returning empty
}
}
protected void btn_Test_Click(object sender, EventArgs e)
{
var test_var = tb_Test.Text; //returning empty
}
}
UPDATE:
site.master.cs
public partial class SiteMaster : MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindCategories();
}
}
protected void BindCategories()
{
Categories Categories = new Categories();
rpt_categories.DataSource = Categories.Get_Categories();
rpt_categories.DataBind();
}
protected void lb_newsCat_Command1(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "naviTo":
//Redirect user to selected category
Response.Redirect("~/" + e.CommandArgument.ToString());
break;
}
}
}
I'm converting a PHP website to an ASP.net website. Turns out I had another HTML form tag on a search box that I haven't started working on yet.
Unfortunately the debugger doesn't throw an error if you have another form tag so anybody else who's just spent hours of their lives searching, double check you don't have more than 1 form tag!
In my site.master.aspx file, I had a search box as follows:
<form method="post" action="?">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-primary my-2 my-sm-0" type="submit">Search</button>
</form>
Once I took the extra form tag out, everything worked fine.
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;
}
}
I'm trying to code a page where you can post a comment without reloading the whole page. The comments are displayed using a Repeater control. The template looks like this:
<asp:UpdatePanel runat="server" ID="commentsUpdatePanel" UpdateMode="Conditional">
<ContentTemplate>
<!-- Comments block -->
<div class="wrapper bloc content">
<h3><img src="img/comments.png" alt="Comments" /> Comments</h3>
<p><asp:Label ID="viewImageNoComments" runat="server" /></p>
<asp:Repeater ID="viewImageCommentsRepeater" runat="server">
<HeaderTemplate>
<div class="float_box marge wrapper comments">
</HeaderTemplate>
<ItemTemplate>
<div class="grid_25">
<span class="user"><%#Eval("username")%></span><br />
<span style="font-size:x-small; color:#666"><%#Eval("datetime") %></span>
</div>
<div class="grid_75">
<p align="justify"><%#Eval("com_text") %></p>
</div>
</ItemTemplate>
<FooterTemplate>
</div>
</FooterTemplate>
</asp:Repeater>
</div>
<!-- Post comment block -->
<div class="wrapper bloc content">
<h3><a id="post_comment" name="post_comment"><img src="img/comment_edit.png" alt="Comments" /></a> Post
a comment</h3>
<p class="description">Please be polite.</p>
<p>
<asp:Label ID="postCommentFeedback" runat="server" />
</p>
<table border="0">
<tr>
<td valign="top">
<asp:TextBox id="postCommentContent" runat="server" TextMode="MultiLine"
MaxLength="600" Columns="50" Rows="15" Width="400px" />
</td>
<td valign="top">
<span style="font-size:x-small">BBCode is enabled. Usage :<br />
<b>bold</b> : [b]bold[/b]<br />
<i>italic</i> : [i]italic[/i]<br />
<span class="style1">underline</span> : [u]underline[/u]<br />
Link : [url=http://...]Link name[/url]<br />
Quote : [quote=username]blah blah blah[/quote]</span>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="postCommentButton" runat="server" Text="Submit"
onclick="postCommentButton_Click" />
</td>
</tr>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
The postCommentButton_Click() function works just fine - clicking "Submit" will make the post. However, I need to completely reload the page in order to see new comments - the post the user just made will not show until then. I Databind the Repeater in Page_Load() after a (!isPostBack) check.
The postCommentButton_Click() function looks like this:
protected void postCommentButton_Click(object sender, EventArgs e)
{
// We check if user is authenticated
if (User.Identity.IsAuthenticated)
{
// Attempt to run query
if (Wb.Posts.DoPost(postCommentContent.Text, Request.QueryString["imageid"].ToString(), User.Identity.Name, Request.UserHostAddress))
{
postCommentFeedback.Text = "Your post was sucessful.";
postCommentContent.Text = "";
}
else
{
postCommentFeedback.Text = "There was a problem with your post.<br />";
}
}
// CAPTCHA handling if user is not authenticated
else
{
// CAPTCHA
}
}
In my case, we do see postCommentFeedback.Text refreshed, but, again, not the content of the repeater which should have one more post.
What is it I'm missing?
You should DataBind in the Page_Load within a !IsPostBack as you are. You should ALSO databind in your Click event.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
this.DataBind();
}
}
protected void MyButton_Click(object sender, EventArgs e)
{
//Code to do stuff here...
//Re DataBind
this.DataBind();
}
public override void DataBind()
{
//Databinding logic here
}
Instead of making your datasource a MySqlDataReader, have your reader populate a BindingList or something like that. Keep that in session and do your databind every non-postback and click. When your user posts, you can either add it to the list and wait for something to tell it to save that, but it makes more sense in the context of posting comments to save their post to your db and redo your datapull and stomp over your BindingList and re-Databind.
Also, personal peeve: I dislike <%#Eval....%>. Code in your page is usually a bad sign. Try using the Repeater.ItemDataBound Event
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx
It sounds to me like the quick fix is to bind on page load regardless of postback. Alternatively, you could rebind from within postCommentButton_Click.
protected void Timer1_Tick(object sender, EventArgs e)
{
Repeater1.DataBind();
/*This is all I did for it to work.*/
}
protected void Buttontextbox_Click(object sender, EventArgs e)
{
this.DataBind();
/*Leave sql connection to your database above "this.databind"*/
}
try putting the update panel between tags and if you have already done that then check if the closing of div tags is proper