I have to create a cookie and then retrieve the cookie that was stored.
How do I go about doing this? The cookie that is being stored, has to have information that I typed in on my Hobbies page that is being displayed on the Summary page.
I have to save the cookies, then redirect to Summary2 and call the cookie that was stored when i hit a button. Here is the code i have so far that i believe correlates with what I am asking
protected void btnDisplay_Click(object sender, EventArgs e)
{
string vacations = Session["Vacations"] as string;
string hobbies = Session["Hobbies"] as string;
string classes = Session["Classes"] as string;
lblDisplay.Text = "Your favorite vacations spots are: " + vacations + "<br />" +
"Your hobbies are: " + hobbies + "<br />" +
"Your IT Classes are: " + classes;
}
protected void btnRedirect_Click(object sender, EventArgs e)
{
Response.Cookies["Hobbies"].Value = hobbies;
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(30);
HttpCookie aCookie = new HttpCookie("lastVisit");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
Response.Redirect("Summary2.aspx", true);
}
and here is what i have for retreiving the cookies on my Summary2 page...
protected void btnDisplay_Click(object sender, EventArgs e)
{
if (Request.Cookies["userName"] != null)
lblDisplay.Text = Server.HtmlEncode(Request.Cookies["userName"].Value);
if (Request.Cookies["userName"] != null)
{
HttpCookie aCookie = Request.Cookies["userName"];
lblDisplay.Text = Server.HtmlEncode(aCookie.Value);
}
}
I mainly do not know how to store the information that was entered in my Hobbies page. Any help with this will be greatly appreciated. Thank you!
put the "Cookie" in your Site.Master and you can access it anywhere.
I figured out what I was doing wrong. I had the cookies method in the wrong button command and in my Summary2 i had it calling the wrong cookie name.
Related
I have cookies and viewstate in below control .
This ajax Control is used to upload multiple file images.
protected void OnUploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
int userid = 25;
DAL_Cart objdalcart = new DAL_Cart();
if (Viewstate["Imagestringname"] == null)
{
objdalcart.InsertTempImage(userid, ImageName, 1);
}
else
{
objdalcart.InsertTempImage(userid, ImageName, 0);
}
Response.Cookies["JewelleryUserCookiesUserId"].Value = Convert.ToString(userid);
Response.Cookies["JewelleryUserCookiesUserId"].Expires = DateTime.Now.AddYears(1);
Viewstate["Imagestringname"] = ImageName + ",";
}
The issue is when I try to retrive view state value or Cookies value on different click event of button in same page I am not able to retrive the value
protected void lnkcheckout_Click(object sender, EventArgs e)
{
if (Request.Cookies["JewelleryUserCookiesUserId"] == null || Request.Cookies["JewelleryUserCookiesUserId"].Value == "")
{
}
if (Viewstate["Imagestringname"] != null)
{}
}
For both the case it is going in if condition. for viewstate I have placed Enableviewstate=true on master page .Any idea why?
Review
Want ajax multiple file upload on my button click event
var c = new HttpCookie("JewelleryUserCookiesUserId");
c.Value = Convert.ToString(userid);
c.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(c);
Just note: this is insecure. the client can manipualte the cookie...
I have the following code from the site master (where the login form is)
protected void btnLogin_Click(object sender, EventArgs e)
{
string dt = DateTime.Now.ToString();
Response.Cookies["LastLogin"].Value = dt.ToString();
Response.Cookies["LastLogin"].Expires = DateTime.Now.AddDays(365);
}
So basically I save the current datetime in a cookie when a users logs in.
Then in the profile review page, I wrote the following:
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack)
{
if (Request.Cookies["LastLogin"] != null)
{
lblMessage.Text = Request.Cookies["LastLogin"].Value;
}
}}
The thing is that it works (it displays the date and time), but not for the previous log in but for the actual login, obviously, but thats why I'm asking - How can I solve this without having to do anything with database? How can I not "override" the previous value but also save the new one?
Using one cookie LastLogin to save the last login information which can used by rest of your application and CurrentLogin to save current login timestamp.
protected void btnLogin_Click(object sender, EventArgs e)
{
string dt = DateTime.Now.ToString();
if (Response.Cookies["CurrentLogin"] != null)
{
HttpCookie oldLoginCookie = new HttpCookie("LastLogin")
{
Expires = Response.Cookies["CurrentLogin"].Expires,
Value = Response.Cookies["CurrentLogin"].Value
};
Response.SetCookie(oldLoginCookie);
}
HttpCookie loginCookie = new HttpCookie("CurrentLogin")
{
Expires = DateTime.Now.AddDays(365),
Value = dt.ToString()
};
Response.Cookies.Add(loginCookie);
}
Also using SetCookie() instead of Cookies.Add() to avoid multiple cookies from being added as advised here. Your Page_Load methods should work as-is as long as you make the above changes.
When doing the login, you can store the previous date (Response.Cookies["LastLogin"].Value, before overwriting it) into a new cookie - then check this new cookie in your page load.
You do need to check that the LastLogin is not empty, in case this is the very first login.
Something like this:
protected void btnLogin_Click(object sender, EventArgs e)
{
if(Request.Cookies["LastLogin"] != null)
{
Request.Cookies["PrevLogin"].Value = Request.Cookies["LastLogin"].Value;
Request.Cookies["PrevLogin"].Expires = DateTime.Now.AddDays(365);
}
string dt = DateTime.Now.ToString();
Response.Cookies["LastLogin"].Value = dt.ToString();
Response.Cookies["LastLogin"].Expires = DateTime.Now.AddDays(365);
}
I am having the following problem: I want to call a method whenever a specific li is clicked. Problem is that the li is dynamically created in a literal.text string, where I do import things from my database.
Whenever I try to call a method it does not work. I want to call a method whenever the user clicks on each li and get that li information inside my method (haven't wrote the method code yet, because I can't get it called.)
Thoughts?
protected void Page_Load(object sender, EventArgs e)
{
string conString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source="
+ Server.MapPath("~/ebookstoredb.mdb");
using (OleDbConnection con = new OleDbConnection(conString))
{
con.Open();
string query = "SELECT * FROM CATEGORY";
using (OleDbCommand cmd = new OleDbCommand(query, con))
{
OleDbDataReader reader = cmd.ExecuteReader();
String msg = "";
while (reader.Read())
{
lit1.Text += "<ul>" + "<li runat=\"server\" OnClick=\"ProductsInfo\">" + reader["ID"]
+ "," + reader["Name"]
+ "</li>"
+ "</ul>";
}
reader.Close();
}
con.Close();
}
}
protected void ProductsInfo(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
Unfortunately, you cannot create server-side events on a Literal Control.
However, you can add client-side javascript functionality to post back a request.
In your aspx using:
<div>
<asp:Literal runat="server" ID="lit1"></asp:Literal>
</div>
Your aspx.cs should contain:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
lit1.Text += "<ul>" + "<li \' onclick=\'javascript: __doPostBack(\"getProduct\", \"1\");\'>"
+ "Product " + "1"
+ "</li>"
+ "</ul>";
}
if (Request.Form["__EVENTTARGET"] != null && Request.Form["__EVENTTARGET"] == "getProduct")
{
getProduct_Click(null, null);
}
}
private void getProduct_Click(object sender, System.EventArgs e)
{
Response.Write("You Clicked on " + Request.Form["__EVENTARGUMENT"]);
}
This will set up each li to pass their own value to the event argument hidden control and perform a postback to the server.
You can then check if the event target is the required one and call a method with the value that was posted back.
Just change the sample lit1 text above to iterate through your data.
I've never used literals before, but from reading about them... with a literal displaying just static html, you will need to have each li call a javascript function that will then do a postback to the method you want. So build your li like this:
lit1.Text += "<ul><li><a onclick=\"CallProductInfo(" + reader["ID"]+ ")\">" + reader["ID"]
+ "," + reader["Name"]
+ "</a></li></ul>";
Then you have to have a javascript function that does the actual postback to your server side code or redirects to the products info page with the passed id. That is, in your page (not the code behind), have a script something like this for the postback...
<script>
function CallProductInfo(id)
{
__doPostBack('ProductInfoId', id);
}
</script>
In you code behind, in your page load event handler, you'd have something like this:
if (Request["__EVENTTARGET"] == "ProductInfoId")
{
ProductInfo(Convert.ToInt64(Request["__EVENTARGUMENT"]));
}
Ok so guys i found out what was the problem. Our mr.genious instructor told us today that we can use a datagrid..Sigh.
Thanks everyone for your replies!
I have to pass two query strings that i've entered and display them on another page.
Here is the code where i attempt to pass them.
protected void btnDisplay_Click(object sender, EventArgs e)
{
string vacations = Session["Vacations"] as string;
string hobbies = Session["Hobbies"] as string;
string classes = Session["Classes"] as string;
lblDisplay.Text = "Your favorite vacations spots are: " + vacations + "<br />" +
"Your hobbies are: " + hobbies + "<br />" +
"Your IT Classes are: " + classes;
}
protected void btnRedirect_Click(object sender, EventArgs e)
{
string vacations = Request.QueryString["vacations"];
Response.Redirect("Summary2.aspx?vacations=" + vacations);
}
Here is where I attempt to retrieve and display them.
protected void btnDisplay_Click(object sender, EventArgs e)
{
lblDisplay.Text = Request.QueryString["vacations"];
}
I can't figure out what I am doing wrong. When i hit the Display button on my 2nd page, nothing shows up. Which I am assuming I am not passing the information correctly.
PS The information that is trying to be passed is the session states on the stop of my code. I only need to send the vacations and the classes through the query string.
you need to take the vacations value from session. you're reading it from the query string on the first page.
protected void btnRedirect_Click(object sender, EventArgs e)
{
string vacations = Session["vacations"] as string; // this line
string classes = Session["vacations"] as string;
Response.Redirect("Summary2.aspx?vacations=" + vacations + "&classes=" + classes);
}
I think you want to send two parameters. (Your question is a little confused)
I hope this can help you.
So, In your webForm 1, add one button and write this small code:
Session["Vacations"] = "sample 1";
Session["variable2"] = "variable 2";
string vacations = Session["Vacations"] as string;
string variable2 = Session["variable2"] as string;
string myquery = vacations + "/" + variable2;
Response.Redirect("WebForm2.aspx?myquery=" + myquery);
In your webForm 2, In the "load" event add this code:
string data = Request.QueryString["myquery"];
string[] words = data.Split('/');
foreach (string word in words)
{
Response.Write(word);
}
It is a way to pass two parameters between two web pages.
In my page when I call searchBtn_Click the selectedvalue will be carried into the variable ind only if the selection hasnt changed. So if a User selects Automotive, then clicks the search button, and then they change the selection to Government, it will refresh the page and display Automotive, am I missing something in the postback or doing something wrong here?
protected void Page_Load(object sender, EventArgs e)
{
string industry = "";
if (Request.QueryString["ind"] != null)
{
industry = Request.QueryString["ind"].ToString();
if (industry != "")
{
indLabel.Text = "Industry: " + industry;
IndustryDropDownList.SelectedValue = industry;
}
}
}
protected void searchBtn_Click(object sender, EventArgs e)
{
string ind = IndustryDropDownList.SelectedValue;
Response.Redirect("Default.aspx?ind=" + ind);
}
Simply replace your code with this code
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
string industry = "";
if (Request.QueryString["ind"] != null)
{
industry = Request.QueryString["ind"].ToString();
if (industry != "")
{
indLabel.Text = "Industry: " + industry;
IndustryDropDownList.SelectedValue = industry;
}
}
}
}
You don't need to use Redirect and QueryString.
Use SelectedValue at Page_PreRender (In your sample clear Page_Load completely).
you better try this in search button click
but remember your dropdowndlist's value-member==display-member to do this.. i had the same problem and this is how i solved it.
string ind = IndustryDropDownList.Text.Tostring().Trim();
Response.Redirect("Default.aspx?ind=" + ind);
i knw this is not the best way but it did work for me..
You're not leveraging the ViewState of asp.net forms (good mentality for MVC 3 though). But since you are using asp.net, you should change your code to this:
The logic in your page load is not necessary, unless you want the user to set the industry as the enter the page. Since I assumed you do, I left some logic in there. It checks for postback because it doesn't need to execute after the initial page load.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack() && Request.QueryString["ind"] != null)
{
SetIndustry(Request.QueryString["ind"].ToString());
}
}
protected void SetIndustry(String industry)
{
indLabel.Text = "Industry: " + industry;
IndustryDropDownList.SelectedValue = industry;
}
You don't have to redirect the page, since Page_Load will be called every time the page posts back. With .NET, your controls remember their last values automatically.
protected void searchBtn_Click(object sender, EventArgs e)
{
SetIndustry(IndustryDropDownList.SelectedValue);
}