I am trying to make a news site, I want to make the news title be moving and once the user puts the mouse over a title it will stop and if click on it it will open this news and displays the title, details and the news photo.
I made a Master page called Site1.Master and I made two content pages WebForm2 which display the moving news title and sends the id using the variable x to WebForm3. However, once I click on any news it doesn't show me anything.
WebForm2.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="newsSite.WebForm2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div id="n" runat="server">
</div>
</asp:Content>
WebForm2.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace newsSite
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("server=;uid=sa;pwd=;database=news");
cn.Open();
SqlCommand cm = new SqlCommand("select id,title from articles", cn);
SqlDataReader dr = cm.ExecuteReader();
string strTitle = "";
while (dr.Read())
strTitle = " <a herf=WebForm3.aspx?x=" dr["id"] ">" dr["title"] "</a> ";
n.InnerHtml = "<marquee id=news onm ouseover=news.stop(); onm ouseout=news.start();>" strTitle "</marquee>";
}
}
}
WebForm3.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="newsSite.WebForm3" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div id="art" runat="server">
</div>
</asp:Content>
WebForm3.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace newsSite
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("server=;uid=sa;pwd=;database=news");
cn.Open();
SqlCommand cm = new SqlCommand("select * from articles where id=" Request.Params["x"], cn);
SqlDataReader dr = cm.ExecuteReader();
while (dr.Read())
{
art.InnerHtml = "<b>" dr["Title"] "</br><br>";
art.InnerHtml = dr["Details"] "<br>";
art.InnerHtml = "<img src=images/" dr["photo"] " /><br>";
}
}
}
}
CodePudding user response:
you can send values from form to another , or page to another using session variables.
you can find more details about other methods on this link
CodePudding user response:
This can be achieved using various state management techniques like View State, Session State, Application State, and Postback features
CodePudding user response:
This is a great question (and a good one).
And why use ViewState over session()?
Well, ok, first up we have use of Session(). This persists per each user, is global to each user, and is 100% server side (so, its considered secure).
Keep in mind that if you not using SQL server manged sessions, then such values can get blown out quite easy. However, session[] is a great way to pass values.
but, what is the number one got-ya? Well, since session is global, say we on a house listing, and I click on a row (say to buy a house). Well, we now jump to a new page, an session() can have that PK id of that house. but, if we right click, and open a new tab? Well, now you can have two tabs open, each showing a different house, but our session() is set to the last one we opened. We now have the possible to buy the wrong house!!!
So, session() is per user, but NOT per page. (and golly, wish someone would have spent 30 seconds to inclue the above information!!!).
So, session() good for global values and passing values.
However, for row and datbase pk id things? Then on page load you GRAB and adopt a conding standard that you transfer that value(s) to view state on page load. That way, you get per page..
And that brings us to ViewState. Viewstate can't really be used to pass values, but viewstate is fantastic for holding or persiting values for the current page. So, ViewState is per page - and thus your code will not get messed up if someone happens to right click, or have two browsers running.
next up? The old tested tried and true?
Use parameters in the URL. I think they look ugly, but even this site (SO) can and does use parameters in the URL (often called query parameters).
Now, you CAN use the URL to pass values but for sensitive information, or database PK values? (very bad idea). I recall a story in the VERY early days of internet, and some credit card system used URL's with an id. If you typed in a different ID, you could see other peoples credit card information!!!
But, even google, this site and many STILL use parameters. They do this, since this can reduce the server side loads and memory (the information is in the URL client side).
However, there is ALSO a VERY nice option and LITTLE used called Page.Previous in .net.
This meanns that you can actually referance, grab, get or use ANY thing from the previous page. However, TWO things are required for this to work:
The button you used requires use of the post-back URL.
And you MUST pick up the information on the first page load IsPostBack = false.
However, this is oh so often missed, and being able to pick up any value and any control from the previous page without having to use ANY of the other methods is often overlooked. As noted, you have to get your values on first page load, but all of the values from Page.PrevousPage can be had this way. And as noted, previousPage ONLY works if the button used has a Postbank URL.
So I will often setup a bunch of values in a simple class. I persist it using ViewState. To pass ot another page, I shove into session(), and on first page load, that class now moves to viewstate (to allow and deal with that right click, and multiple pages/browsers open).