A special text on a web site - c#

Assume that there is a web site which includes 3 different pages.
I want to show a text one of the pages randomly, with is formatted with css.
For instance the pages are below:
hello-world.aspx
hi-sun.aspx
good-night-moon.aspx
* When John enters to the site, the text will appear on hi-sun.aspx,
* When Elmander enters to the site, the text will appear on hello-world.aspx
And when one enters the page which includes a special text, even if come again, it shouldn't appear.
Psedue Code:
if(Session["first"] == "1")
{
//show the text in a random page
}
else
{
//text.visible = false
}
in the if block
how can I supply the text in a random page. (it shouldn't appear in every page, should appear only one page)
How can I do? Are there any suggestions?
Thank you.

I don't fully understand what you want to do, but I think it's something like this:
You have a couple of different sites (3), and you have a text (welcome or something) you only want to show once. But after typing in your url the user should see one of the sites randomly.
For the first (if you don't want the user to log in) you can either save some flag in the Session object or create a cookie for the user (saying he has seen the text) and check this every time you want to show it.
The session will on the server but will be lost so the user might see the same message later again if he revisits your site. But while staying he will see it only once.
The second is on the client. If he accepts the cookie he will never see the message again if not he might see it everytime because you cannot know. Maybe you want some combination of these both.
For the second you will have to send a redirect if you don't want to get fancy with a deep dive into System.Web.
In the case above you can just do:
if(Session["first"] == null)
{
Session["first"] = true;
//show the text in a random page
}
else
{
text.Visible = false
}
but note that the session will not stay forever for the current user.

Related

ASP.Net passing textfield value to a label on a different web page

I have a page called webForm1, this page contains a textfield, when a user enters a value, I want the value to show up in a label on webForm2, when I do that, I am getting an error:
Label1 is inaccessible due to its protection level
This is what I am doing in webForm1
webForm2 webform = new webForm2();
webform.Label = textBox1.Text;
Response.redirect("~/webForm2.aspx");
but the above is not working, I am new to programming and not familiar with classes and complicated programming, what is the easiest way to get the value of the textbox in the label?
Thank you.
You can't instantiate the page class (webForm2) in your current page. You'll have to pass the value in another way to the second page and then bind the label. As Jason P says, the ASP.NET framework instantiates the webForm2 page for you, you can't do it yourself.
If the data is not sensitive, use the Query String:
Response.Redirect("~/webForm2.aspx?label=" + textBox1.Text);
This will redirect the user to a page with the url of whatever.com/webForm2.aspx?label=whatevervalue. On the second page, you can pull the text from the query string and bind it to the label:
public void Page_Load(object sender, EventArgs e)
{
Label.Text = Request.QueryString["label"].ToString();
}
Unlike WinForms, you don't instantiate the next form like that. Essentially, your first two lines are incorrect for WebForms. The third line is where you want to focus your attention. You redirect the user to the second form, allowing the framework to take care of instantiating it.
This is because WebForms, despite being "forms", is still an HTTP web application and does everything through requests and responses. By issuing a redirect you are telling the client to abandon the current page and make a new request for the specified page.
There are a number of ways to send a value to this next page. You can store it in some persisted medium (such as a database), you can use session state, etc. Probably the simplest approach at the moment would be to include it on the query string:
Response.Redirect("~/webForm2.aspx?label=" + textBox1.Text);
Then in the next page you'd get the string from:
Request.QueryString["label"]
You may want to URL-encode the text value first, I don't know if Redirect() does that for you. Also keep in mind that this isn't a "secure" transfer of data from one page to the next, because the client has full access to modify values in the URL. So if this is in any way sensitive data then you'll want to look into other approaches. (Keep in mind that "sensitive" could be a relative term... The information itself might not be sensitive but you might be doing system-sensitive things with it on the next page, which we can't know from the code posted.)

Page back not working as expected on BinaryWrite

I have a home/main menu page. There's a link to the form. If you run the form with inputs, you get Excel output. If you click 'open' to open that output in the browser, the back-button takes you to the home/main menu.
Which is understandable, since if the form page is X.aspx, the binaryWrite that outputs the Excel doesn't really change that.
The problem is that if there's a postback, the back button takes you to the form page. This is inconsistent. If I run the page or a drop down causes a postback, I really want the back-button to take me to the form field, so that I can run the excel sheet again.
Things I've tried.
I tried adding a location header.
e.ContentFileName = "ELTFile.Output.xls";
Before the response.Writing ...
private void SendFileToBrowser(DownloadFileEventArgs e)
{
if (!e.Cancel && e.FileObject != null)
{
this.Page.Response.Clear();
this.Page.Response.ClearContent();
this.Page.Response.AddHeader("Content-Disposition", string.Format("inline; filename={0}", e.ContentFileName));
this.Page.Response.AddHeader("Content-Length", e.FileObject.LongLength.ToString());
this.Page.Response.AddHeader("Location", e.ContentFileName);
this.Page.Response.ContentType = e.ContentType;
this.Page.Response.BinaryWrite(e.FileObject);
this.Page.Response.End();
}
}
And I tried
Response.Cache.SetCacheability(HttpCacheability.Public);
As well as
Response.Cache.SetCacheability(HttpCacheability.Server);
But basically, depending on whether or not I post back before I click the 'Submit' button to generate the Excel, I get the main menu (don't want) or the form page (want). I want the form page every time. Basically I want to tell the browser "I know it's not really a different page, but treat it that way, will ya please?"
You cannot directly add pages to a browser history.
You could make your application such that the form is always a page ahead of the excel output.
To do this, have a separate page to output the excel.
When you have the inputs to generate the excel, Response.Redirect to the generation page.
This will keep your input form in the browser history.
Note: you could redirect to the same page too if you liked, but that would need you to track it.

Unable to get updated value of session variable and Textbox

I am building a web application in asp.net and c#, I am storing text of textbox in Session variable like this:
Session["data"]=TextBox1.Text;
and on the button click event I am redirecting user to another page.In the second page I am using this variable Session["data"] and in page2 there is a back button where I am again Redirecting to page1 where the textbox1 is present.Now on the button click event where I am redirecting to page2 and event is code like this,
Session["data"]=TextBox1.Text;
Response.Redirect("page2.aspx");
now when I am accessing the value of the Session["data"] it is giving me the previous value.As the content of TextBox1 may get changed,these changes have not been shown.
Please tell me where I am going wrong.
I dont think the code you are using is worng. You please make a break point and check it again that the values are assigning correctly
Or
You just set the session to null and then assign the textbox value to it on the click event.
Read this I think Respone.Redirect might be causing you to lose your session data
Don't redirect after setting a Session variable (or do it right)
A problem I see over and over again on the ASP.NET forums is the
following: In a login page, if the user and password have been
validated, the page developer wants to redirect to the default page.
To do this, he writes the following code:
Session["Login"] = true;
Response.Redirect("~/default.aspx");
Well, this doesn't work. Can you
see why? Yes, it's because of the way Redirect and session variables
work. When you create a new session (that is, the first time you write
to a Session variable), ASP.NET sets a volatile cookie on the client
that contains the session token. On all subsequent requests, and as
long as the server session and the client cookie have not expired,
ASP.NET can look at this cookie and find the right session. Now, what
Redirect does is to send a special header to the client so that it
asks the server for a different page than the one it was waiting for.
Server-side, after sending this header, Redirect ends the response.
This is a very violent thing to do. Response.End actually stops the
execution of the page wherever it is using a ThreadAbortException.
What happens really here is that the session token gets lost in the
battle. There are a few things you can do to solve this problem.
First, in the case of the forms authentication, we already provide a
special redirect method: FormsAuthentication.RedirectFromLoginPage.
This method is great because, well, it works, and also because it will
return the user to the page he was asking for in the first place, and
not always default. This means that the user can bookmark protected
pages on the site, among other things. Another thing you can do is use
the overloaded version of Redirect:
Response.Redirect("~/default.aspx", false);
This does not abort the
thread and thus conserve the session token. Actually, this overload is
used internally by RedirectFromLoginPage. As a matter of facts, I
would advise to always use this overloaded version over the other just
to avoid the nasty effects of the exception. The non-overloaded
version is actually here to stay syntactically compatible with classic
ASP.
I don't really see what you want to do here. You say it works (the Session["data"] contains the text of TextBox1?) but you don't see any changes? What changes did you expect? Please give us some more information about this part of the question:
As the content of TextBox1 may get changed,these changes have not been shown.
Thanks.
Update:
Try clearing or remove the session and then refill it.
Session.Remove("data");
Session.Clear();

show popup box before signout

i have a doubt on how to show a popup???`
if (machineID.Count != 0)
{
checkMachineGrpState(machineID);
}
else
{
FormsAuthentication.SignOut();
Session.Abandon();
Response.Redirect("~/Default.aspx");
}
Ok now what im am doing in else statement is signing out the user and sending him back to the log out page....
I need to how him some pop up message that he is being signed out i cant figure out how to do that...
i tried messagebox but it wont work with servver and client side..
I want to use AJAX but dont know how...
any suggestions.... thanks
There are a couple of different ways you can go about this. Here's a simple example.
Your Default.aspx page will need to display the message to the user when they've logged out, so you might want a way to distinguish when you want to show the message. You could add a query string param to your redirect, like:
Response.Redirect("~/Default.aspx?ShowLogout=true");
Now on your Default.aspx page, you have a number of options. You could simply show a hidden control on the page, or write out some Javascript to show an alert box:
if (!String.IsNullOrEmpty(Request.QueryString["ShowLogout"]))
ClientScript.RegisterStartupScript(this.GetType(), "LogoutMsg", "<script>alert('You have been logged out.');</script>");
This will simply write out a script tag that runs when the user views the page. From here, you can make it more elegant by showing the user a better dialog box. For example, you could use jQuery to create a nice looking dialog box, and call the Javascript function to show it rather than calling alert in my example.

Disable selection box without removing value from post

In my current asp.net-mvc project one of my pages allows the user to select a value in a dropdown box after wich a post request is made that updates several values.
To make sure the delay from the postback doesn't confuse the user into selecting another value (and thus creating another post, creating another delay etc) I set the select's disabled attribute to true.
Disabled inputs aren't submitted to the post call however.
How can I make it visually clear to the user that work is in progress and make it imposible to select a new value without removing the input from the post?
Yes, this annoys me too.
Basically what you need to do is hide the old button and replace it with a disabled one so it looks the same to the user. That way it's still submitted but can't be doubly submitted.
Actually I've found what seems to be a duplicate of this at Problem with disabling submit buttons on form submit.
From your answer, I gather you are already using jQuery. In that case why don't you get the value of the select box, disable it, then post the value yourself?
Bonus : BlockUI is a nice jQuery plugin to, well, block the UI.
None of the answers I found in Cletus' post was entirely what I was looking for.
Here is what I came up with. It's not 100% reusable, but it does what I need and feel free to improve/edit.
$('#productMixSelectorForm').change(function() { $(this).ChangeSelection() });
jQuery.fn.ChangeSelection = function() {
var html = $('<div class="hidden">');
$(this).find('select, input').each(function() {
if ($(this).hasClass('hidden') == false) {
//Clone the original one into the hidden div
html.append($(this).clone());
//Disable the original (visible) one and make it's name unique again
$(this).attr("disabled", true);
var name = $(this).attr("name");
$(this).attr("name", name + "disabledDummy");
}
});
//Add the collection of clones to the form so they get submitted
$(this).append(html);
$(this).submit();
}

Categories