Display text only once per session in .net - asp.net

I have a text to be displayed to user only once per session i.e. only the first time they try to open a page. If they try to open again they are not supposed to see that text.
If they close the browser and open it again they should be able to see the text again.
I wanted to use session, but I am not sure how to use it.
I tried to use cookie which worked, but what if cookie is disabled, I guess this wont work. so I decided to go with Sessions.

Define OnSessionStart Event In Global.asax...like this...
void Session_OnStart(object sender, EventArgs e)
{
Session["showmessage"]="Show";
}
On pageLoad or Event where you want to check... Check Session...if you want to show your text in label do like this...
protected void Page_Load(object sender, EventArgs e)
{
if(Session["showmessage"].ToString()=="Show")
{
Label1.Text="Message";//i Supposed you wana Show Message in Label.You Can Write your Code to Show Message wherever you wnat show.
Session["showmessage"]=Not Show";//To Display Message Only One Time.
}
Note-: It will Display Message Every Time Your Session Expired.

Related

asp.net - RadioButton "CheckedChanged" event does not work when it is unchecked

I have two RadioButtons on my Web Form and here is the code:
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToLongTimeString());
}
When I check RadioButton2, time appears on the page but when I check RadioButton1(RadioButton2 is unchecked now) it does not update the shown time and removes it.
the event function must be called when it checks and unchecks.I don't understand what is wrong here and why the time disappears
(As it is obvious, I know about GroupName property and AutoPostBack. My problem is something else)
You can check property of each control and check AutoPostBack=True
and also if checked="true"
I tried it on different ways and opened my pages with different browsers and I figured out a few things. the most important of them which is related to my problem:
When a control Calls back, the page refreshes and that is why the time disappears. It sends the status of all controls using post method to itself and they would be the same way as they were. But the time is not written on a control and it would not be saved.
As I said if it was for example on a Label, the text would be kept:
Label1.Text = DateTime.Now.ToLongTimeString();
It answers the first question but I still think that the event function must be called when the RadioButton is being unchecked.

asp.net dynamic user control button click issue

I made researching about this subject I could not find proper answer.
In my default.aspx page, I have a treeview. Codes are in default.aspx like below:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
Control ucont;
if (TreeView1.SelectedNode.Value == "Yeni Dönem")
{
ucont = LoadControl("usercontrols/yenidonem.ascx");
PlaceHolder1.Controls.Add(ucont);
}
else
{
ucont = LoadControl("usercontrols/tabloktar.ascx");
PlaceHolder1.Controls.Add(ucont);
}
}
I load user controls dnynmicaly. User controls are have button control. I can not fire user control's button click when I load it dynamcally. How can I solve this ?
Thanks.
First of all, I would not recommend adding control dynamically later than in Page_Load event. Other things to remember is that You should add it on each page load and assign unique ID value the control that does not change between postbacks.
In this case, the easiest way would be to always add both controls to the page and show appropriate one using Visibility property.
If that's not suitable for You, try to move the code from TreeView1_SelectedNodeChanged to the Page_Load event and load appropriate control on each postback until it should be changed to another one.
I haven't tested this, so if You have any issues when using thise answer, let me know in the comments and I'll try to help.

how to do notification message in ASP.NET Web Forms

I have in Site.Master:
<% if(Session["msg"]!=null) Response.Write(Session["msg"].ToString()); %>
I have also on submit form method:
protected void Send_Click(object sender, EventArgs e)
{
Session["msg"] = "Thx for email.";
Response.Redirect("~/Default.aspx");
}
But now when I refresh page or go to another page I still see "Thx for email." but user should see it only once.
You can clear out the Session["msg"] on Page_load (outside of the if(!isPostback))
Or you can create a label on the master page, access that through the child pages to put the message in there, and clear that one on Page load, this gets you away from using Session. Using a Label you can also set the cssClass, allowing bolding, color changes (red for errors, green for success, etc).
If you just want a plan message you could aways go with a literal control, less over head.
This is because Session variable having their value throghout the session.
Session["msg"]
will always have same value on all the pages in a session.
If you want that value should only be used for the page where you redirect then you can use querystring.
protected void Send_Click(object sender, EventArgs e)
{
Session["msg"] = "Thx for email.";
Response.Redirect("~/Default.aspx?msg='true'");
}
then on SiteMaster
<% if(Request.QueryString["msg"]!=null) Response.Write(Session["msg"].ToString()); %>
You have to set Session["msg"] = null after you show the message. Session lives in server at defualt of 20 min. If you do not set it null it will appeear
Try setting the Session["msg"] to null once it is printed on the page.

My code says a checkbox isn't checked when it is... ASP.NET

I have some checkboxes on a page. I get them using FindControl() in an UpdatePanel after pressing a button trigger, but the checked value is wrong. How can I get the correct checked value?
If you have any code that sets the values of the checkboxes on your page, make sure it isn't executing on postbacks, like this:
protected void Page_Load(object sender, EventArgs e) {
// Only set the checkboxes on GETs, not on POSTs
if (! this.IsPostBack) {
this.EmailMeUpdatesCheckbox.Value = false;
}
}
Actions triggered within UpdatePanels still go through the page lifecycle (which is why you have access to all your Page's state), so it may be clearing the user's selections before getting to the code in which you examine the checkbox values.

how to go to previous screen

I am using two kinds of buttons, on button takes the user to the next screen. Here I am using a session variable to take the user to the next screen:
protected void Buttondocumentdetails_Click(object sender, EventArgs e)
{
Session["Narration"] = TextBox2.Text;
Response.Redirect("~/TJFAQ0001.aspx", false);
}
This is working properly -- after clicking this button the user is sent to the next screen, which is is "TJFAQ0001.aspx". On this page I am using button to take the user to the previous screen:
protected void previous_Click(object sender, EventArgs e)
{
Session["Narration"] =null;
Response.Redirect("TJFAQOO1.aspx", false);
}
This is working, but all the data has been cleared. I want the same data when I click previous button. How can I do this?
First of all you can use Server.Transfer instead of Response.Redirect.
You can pass the page field data in a context variable and then pass it to the next page. When redirected to the previous page pass this back and do the necessary processing and then fill the data back to the controls.
Do not use Session variable in this context. You can add a context variable when using Server.Transfer.
You have to repopulate the data in TJFAQOO1.aspx page when it gets loaded on click of previous button as it is a new get request and postback kind of facility is not available.

Resources