I am working on a Web Application and i want that when the user logged In the UserID should be stored in Session. I know how to create Session.
Session["UserID"] = myvalue;
But i want that to be used on everypage. so i dont want to write code or check for session availability on every page. will Global.asax file help me ?
If i write session in Session_Start() then will that be accessible on all pages ? and if expired the return to login page.
Just require proper guidance. Thanks
I used to write as a separate class to handle session and return it as a property. I can write all the condition in the class.
I highly suggest creating a basepage that is inherited by every page but the login. this way you are only creating the session variable code on one page but that code is accessible by any page that inherits it.
edit with small example:
when your user is authenticated you need to set your session variable
currently your pages probably look something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
what you need to do is create a separate class file, basepage.cs for example that looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public class basepage : System.Web.UI.Page
{
protected int GetItem()
{
return Convert.ToInt32(Session["myvalue"]);
}
}
}
and then your original page would look more like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : basepage
{
protected void Page_Load(object sender, EventArgs e)
{
int whatINeed = GetItem();
}
}
}
as you see instead of your page inheriting the System.Web.UI.Page it is inheriting the basepage (which in turn inherits System.Web.UI.Page).
Related
overflow, im having trouble trying to add a ui framework , for exmple metro framework, to my c# form.
I know that i have to include the reference and include it with
using MetroFramework.Forms;
but im not sure what else to do here and when ever i try and google it, i just get websites that try and sell me a framework package.
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework.Forms;
namespace FrameworkTest
{
public partial class Form1
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
I know this may be a silly question but any help would be much appreciated.
Thanks to Crowcoder I found out that I was supposed to add
MetroForm after Form1 like:
public partial class Form1 : MetroForm {
//Code goes here
}
I'm currently working on a custom user control inheriting from a panel, I'm trying to hook on the Layout event of the Panel to do a certain action. I don't want to override the initial Layout event, I just want to hook on it and add stuffs to do
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SamsonLogiciel
{
public partial class StretchPanel : Panel
{
public StretchPanel()
{
InitializeComponent();
}
}
}
protected override void OnLayout(LayoutEventArgs levent)
{
// your code here
base.OnLayout(levent);
}
Just add this to your StretchPanel class. You can add your own code to "hook" into it, and then you call base.OnLayout so that you don't override the default stuff.
I'm new in programming. I've followed the steps in the book "Pro asp.net 4 in c#2010" to create 2 classes.
Now I try to use this class on a webpage. In the .cs file I added using "DBComponent;" but Visual Studio says "The type or namespace name 'DBComponent' could not be found (are you missing a using directive or an assembly reference?)"
What did I forget?
If i try to add the compiled dll under 'references' i get the error:
The type 'DBComponent.EventDetails' in 'D:_Web\OSWeb\DBComponent\DBComponent\EventDetails.cs' conflicts with the imported type 'DBComponent.EventDetails' in 'D:_Web\OSWeb\DBComponent\DBComponent\bin\Debug\DBComponent.dll'. Using the type defined in 'D:_Web\OSWeb\DBComponent\DBComponent\EventDetails.cs'.
This is what I've done:
Create a new empty website 'OSWeb' in VS
On top of this solution I clicked right and choosed: Add > new Project > Visual C# > Windows > Class library: name: DBComponent and I stored in D:_Web\OSWeb\DBcomponent
List item
I created 2 cs files (EventDB.cs and EventDetails.cs)
my events.cs file
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DBComponent
{
public class EventDB
{
private string connStr;
public EventDB()
{...}
public EventDB(string connectionString)
{...}
public int InsertEvent(EventDetails evd)
{...}
}
}
This is my eventdetails.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DBComponent
{
public class EventDetails
{
private int eventId;
private string eventName1;
public int EventId { get { return eventId; } set { eventId = value; } }
public string EventName1 { get { return eventName1; } set { eventName1 = value; } }
public EventDetails(int eventId, string eventName1)
{
this.eventId = eventId;
this.eventName1 = eventName1;
}
public EventDetails() { }
}
}
Now I create a new webpage (this is the .cs file of this webpage) (events.aspx.cs)
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBComponent; => here is the error
public partial class events : System.Web.UI.Page
{
private EventDB db = new EventDB(); => here is the same error
protected void Page_Load(object sender, EventArgs e)
{
}
}
try the following:
right click on your web project and click in properties, then click en references. remove the reference of your class, close this windows. Verify in the Bin folder from you proyect, yours references (should not be the reference has been removed.). Next, you open again the project properties and add your class library.
(before adding the reference should be sure that the class library compiles correctly.)
sorry for my english
I just want to set the background image of another form to a png file picture resource I added
I have the following in my MAstrategyBldrForm.cs file:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MAStrategyBuilder
{
public partial class MAstrategyBldrForm : Form
{
private CrossPicsForm crossPic;
public MAstrategyBldrForm()
{
InitializeComponent();
crossPic = new CrossPicsForm();
}
....
....
.... later in same file:
private void MAcrossPicButton_Click(object sender, EventArgs e)
{
crossPic.BackgroundImage = (Bitmap) (Properties.Resources.ResourceManager.GetObject("Moving Avgs Cross.png"));
crossPic.Show();
}
....
....
In my CrossPicsForm.cs file I have:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MAStrategyBuilder
{
public partial class CrossPicsForm : Form
{
public CrossPicsForm()
{
InitializeComponent();
}
}
}
Everything compiles but when I click my button control in the mastrategy... form, a CrossPicsForm window is displayed that is blank. Darn!
You won't get an exception when you mis-spell the resource name, ResourceManager simply returns null and that leaves the BackgroundImage property unchanged. You did, it would normally be named "Moving_Avgs_Cross" without the .png extension.
Do favor using the property that was added by the resource designer instead. That ought to resemble:
crossPic.BackgroundImage = Properties.Resources.Moving_Avgs_Cross;
Use IntelliSense to help you fall in the pit of success here.
I have a simple window application I am starting and I was given another file I would like to bring into the new project. How do I bring them both together to work as one file?
New Project
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Other
using System;
using System.Text;
using System.IO;
using System.Net;
namespace CaptchaClient
{
public static class SampleUse
{
public static void SampleSolve()
{
}
}
public class CaptchaSolver
{
}
public enum ResponseState
{
}
}
This screenshots shows the how yo do he procedure
Right click on your project in the Solution Explorer and click Add Existing Item. Then browse to the other class file. Once imported, you might want to change its namespace to the one you are using in your project.