in windows application , when i want to call a customized form , i ueses this method :
result frm = new result();
frm.firstparameter = "first parameter";
frm.secondparameter = "second parameter";
frm.showdialog();
but in web application , i do not know how to handle it .
here is my webapplication source code :
in WebForm1.aspx.cs :
protected void sumbitbtn_Click(object sender, EventArgs e)
{
result frm = new result();
frm.firstparameter = "firstparameter";
frm.secondparameter = "secondparameter";
// frm.showpage() ???
// Response.Redirect("~/result.aspx");
}
in result.aspx.cs :
public partial class result : System.Web.UI.Page
{
private string Firstparameter = string.Empty;
public string firstparameter
{
get{return Firstparameter;}
set { Firstparameter = value; }
}
private string Secondtparameter = string.Empty;
public string secondparameter
{
get{return Secondparameter;}
set { Secondparameter = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
firstlbl.Text=Firstparameter;
secondlbl.Text=Secondtparameter;
}
}
ASP.NET does not work that way. If you want to open one page after the other - you need to send a redirect request to do so. Of course you can also open popup, or make server transfer, but in your case redirect seems to be the best thing to do. Also note that parameters are not just passed into constructor or property - you need to attach them to the request, for example with query string.
So, in WebForm1.aspx.cs:
protected void sumbitbtn_Click(object sender, EventArgs e)
{
string url = string.Format("~/result.aspx?fp={0}&sp={1}", "firstparameter", "secondparameter");
Response.Redirect(url);
}
In result.aspx.cs :
public partial class result : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
firstlbl.Text = Request["fp"];
secondlbl.Text = Request["sp"];
}
}
Note that this code leaves a lot of things out (parameter null handling for one), it just shows the point.
Related
EDIT: made variables not-static.
I have a variable in code-behind class and want to access it from another.
Here's the C# code for the "Signup" page:
public partial class Webform_Signup : System.Web.UI.Page
{
private string user;
public string User
{
get { return user; }
set { user = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if(HttpContext.Current.Request["submit"] != null)
{
user = HttpContext.Current.Request["user"];
Response.Redirect("Login.aspx");
}
}
}
And here's the code for another code-behind class in an .aspx class "Login":
public partial class Webform_Login : System.Web.UI.Page
{
public string loguser = "";
protected void Page_Load(object sender, EventArgs e)
{
if(HttpContext.Current.Request["logsubmit"] != null)
{
loguser = HttpContext.Current.Request["loguser"];
if (loguser == Webform_signup.User)
{
Response.Redirect("Start");
}
}
}
}
The problem is that when I try to access Webform_signup.user, it displays an error saying:
The name Webform_Signup does not exist in the current context.
Anyone has an idea of how to fix it?
The dirt simplest way (and probably not the best way) is to just stick it in a session variable and read it from there instead.
public partial class Webform_Signup : System.Web.UI.Page
{
private string user;
public string User
{
get { return user; }
set { user = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if(HttpContext.Current.Request["submit"] != null)
{
user = HttpContext.Current.Request["user"];
HttpContext.Current.Session["user"] = user; //Save to session
Response.Redirect("Login.aspx");
}
}
}
public partial class Webform_Login : System.Web.UI.Page
{
public string loguser = "";
protected void Page_Load(object sender, EventArgs e)
{
if(HttpContext.Current.Request["logsubmit"] != null)
{
loguser = HttpContext.Current.Request["loguser"];
var user = HttpContext.Current.Session["user"]; //Read from session
if (loguser == user)
{
Response.Redirect("Start");
}
}
}
}
This is assuming of course that you have session state enabled.
You need to keep in mind lifecycles. You're ignoring them, and the results would be disastrous in this case.
Any time you declare a variable as static, there will only be one instance of that variable in your application. That means no matter what user is using your app, the Webform_Signup.User field will be shared. That could result in users accessing other users' information! That's really, really bad.
Also, page classes should not be referring to other page classes. A page only exists when a user is accessing that page.
If you want to login a user, then perhaps you should research Forms Authentication. Or better yet, learn a modern framework like ASP.NET Core MVC instead of Web Forms, which is a deadend platform.
I am new to ASP.NET and I am trying to set session variable. I have one form (SelectPlayer.aspx) where I am trying to set the session but when I try to see the result on second page it does not show me any value. Below is my code.
SelectPlayer.aspx
public partial class SelectPlayer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["player1"] == null)
{
lblSelectPlayer.Text = "Select Player 1";
}
}
protected void btnSelect_Click(object sender, EventArgs e)
{
Session["player1"] = "PlayerSession";
Response.Redirect("Score.aspx");
}
}
Score.aspx
protected void Page_Load(object sender, EventArgs e)
{
if (Session["player1"] == null)
{
Response.Redirect("SelectPlayer.aspx");
}
}
Change Response.Redirect("Score.aspx"); to Response.Redirect("Score.aspx", true);
The second parameter of a redirect indicates that you want to end the response. I've found in the past that setting a Session directly before doing a redirect without this second parameter can cause issues.
I am building a web forms app in Visual Studio and have a number of button functions to allow the user to add, alter and delete from my database.
And all the functions work the way they are intended up until a user enters the wrong information or doesn't enter anything
Here is the error I get
I have done some research myself but I am still not sure how to fix it, And would love some help
Here is the code in question
public partial class DeleteCharacters : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private MarvelModelContext context = new MarvelModelContext();
protected void btnDeleteId_Click(object sender, EventArgs e)
{
int id = System.Convert.ToInt32(txtId.Text);
string Sname = SNameT.Text;
Character CRead = context.Character.FirstOrDefault(c => c.CharacterId == id && c.Superhero == Sname);
context.Character.Remove(CRead);//Error Pops up here
context.SaveChanges();
//Clear Feilds
txtId.Text = "";
SNameT.Text = "";
}
}
As #MKS suggested you must check for null if you are using FirstOrDefault. This returns null if it can't find a match in your lambda expression. So, the updated method should look like this.
public partial class DeleteCharacters : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private MarvelModelContext context = new MarvelModelContext();
protected void btnDeleteId_Click(object sender, EventArgs e)
{
int id = System.Convert.ToInt32(txtId.Text);
string Sname = SNameT.Text;
Character CRead = context.Character.FirstOrDefault(c => c.CharacterId == id && c.Superhero == Sname);
if(CRead != null) // added this condition
{
context.Character.Remove(CRead);
context.SaveChanges();
//Clear Feilds
txtId.Text = "";
SNameT.Text = "";
}
}
}
I have a problem with my code. Its working fine and there is no error but a logical one i think so. I have used a method PassValue(int id) to get value from another from. I have tested it and the forms are exchanging the values correctly but the problem comes when I use the value which i have received from other form as a "textbox.text" or a "label.text"
Here is my code:
namespace MyProgram
{
public partial class UserProfile : Form
{
public string empidstr;
public UserProfile()
{
InitializeComponent();
}
public void PassValue(int id)
{
string idstring = Convert.ToString(id);
// empidlabel.Text = idstring;
empidstr = idstring;
}
private void button2_Click(object sender, EventArgs e)
{
empidlabel.Text = empidstr;
}
private void UserProfile_Load(object sender, EventArgs e)
{
}
}}
hallo iam getting this error message when the page is load,
Type 'messageBox' in Assembly 'App_Code.e9hyffkh, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
iam new in asp.net , and i can find what iam going worng!
heres my code:
public partial class home : System.Web.UI.Page
{
public string val = "";
public string data = "";
protected void Page_Load(object sender, EventArgs e)
{
val = "";
if (!IsPostBack)
{
ViewState["Messages"] = new List<messageBox>();
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
var messages = (List<messageBox>)ViewState["Messages"];
if (text1.Text == "")
{
val = "נא הכנס שם";
}
else
{
messages.Add(text1.Text);
val = "נוסף בהצלחה";
}
ListBox1.DataSource = messages;
ListBox1.DataBind();
ViewState["Messages"] = messages;
data = messages.Count.ToString();
text1.Text = "";
}
Add [Serializable] attribute on top of your messageBox class.
For reference: SerializableAttribute Class
Add [Serializable] attrubute to your class not sure what your class looks like but see below for an example
[Serializable]
public class messageBox
{
//other code / Fields related to the class goes below
}