Read Integer From Sessions - c#

I am attempting to save the global variable Label1.Text to sessions and read it back in on page_load. I passed the integer through here:
Session["points"] = TotalPoints.Label1;
And am attempting to read it back here. What am I doing wrong?
Label1.Text = (int)Session["points"];
All code is below:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = (string)Session["points"];
}
public class TotalPoints
{
public static int Label1;
}
public void Validations()
{
if (TextBox1.Text == "20")
{
Image5.Visible = true;
Image6.Visible = false;
TotalPoints.Label1 += 1;
}
else
{
Image5.Visible = false;
Image6.Visible = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
Validations();
}
protected void newWindow(object sender, EventArgs e)
{
int next = new Random().Next( 3 ) + 1;
Response.Redirect(string.Format( "Question{0}.aspx", next ));
Session["points"] = TotalPoints.Label1;
}
</script>
<html>
<form id="form1" runat="server">
<asp:Image ID="Image1" runat="server" Height="60px" ImageUrl="http://www.ixl.com/qgen_res/sets_of_objects/popsicles_100.gif" Width="107px" />
<asp:Image ID="Image2" runat="server" Height="60px" ImageUrl="http://www.ixl.com/qgen_res/sets_of_objects/popsicles_100.gif" Width="107px" />
<asp:Image ID="Image3" runat="server" Height="60px" ImageUrl="http://www.ixl.com/qgen_res/sets_of_objects/popsicles_100.gif" Width="107px" />
<asp:Image ID="Image4" runat="server" Height="60px" ImageUrl="http://www.ixl.com/qgen_res/sets_of_objects/popsicles_100.gif" Width="107px" />
<br />
How many popsicles are there? Count by fives. <br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="button1_Click"/>
<asp:Image ID="Image5" runat="server" Height="30px" ImageUrl="http://www.lookseeedit.com/resources/tick.jpg" Width="30px" Visible="False" />
<asp:Image ID="Image6" runat="server" Height="30px" ImageUrl="http://star-w.kir.jp/grp/9/red-cross-wrong-i19.svg" Width="30px" Visible="False" />
<asp:Label ID="Label1" runat="server" /> </asp:Label>
<asp:Label ID="Label2" runat="server" Text="/10"></asp:Label>
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="Next Question" OnClick="newWindow"/>
</form>
</html>

No, you're not doing it right.
Response.Redirect occurs before Session["points"] is set inside of the newWindow function.
To start, you should rearrange those lines, but you should also call that function from somewhere too.

Related

How to show corresponding value of a calculated field through html query string to another page

I'm working on a shopping cart functionality which allows users to add items to a shopping cart(via a add to cart button.) I have a specials page where members get discounted price for items. i have successfully passed the id(database) and sales price (calculated value) from the specials page to a add to cart page.
I couldnt post images because i dont have reputations so here is what the url looks like:
http://localhost:51231/LegoBits/Shopping/ShoppingCartItem.aspx?Id=1&SalesPrice=0.89
You can see the url is getting the values Id and SalesPrice from another page, but , i'm having trouble showing the salesPrice value in datalist of current page.
here is my code for the shoppingCartItem.aspx
<asp:DataList ID="DataList1" runat="server" HorizontalAlign="Center" DataKeyField="Id" ItemStyle-HorizontalAlign="Left" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Thumbnail", "{0}") %>' />
<asp:Label ID="PictureURLLabel" runat="server" Text='<%# Eval ("Thumbnail") %>' Visible="false"></asp:Label>
<br />
<strong style="text-align: left;">Name: </strong>
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
<br />
<strong style="text-align: left;">Model: </strong>
<asp:Label ID="ModelLabel" runat="server" Text='<%# Eval("Model") %>'></asp:Label>
<br />
<strong style="text-align: left;">Ages: </strong>
<asp:Label ID="AgesLabel" runat="server" Text='<%# Eval("Ages") %>'></asp:Label>
<br />
<strong style="text-align: left;">Price: </strong>
$
<asp:Label ID="PriceLabel" runat="server"></asp:Label>
<br />
<br />
<asp:ImageButton ImageUrl="../Images/AddToCart.jpg" ID="Button1" runat="server" OnClick="Button1_Click" />
</ItemTemplate>
</asp:DataList>
and its code behind:
public partial class Shopping_ShoppingCartItem : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
string salePrice = Request.QueryString["Sales Price"];
string price = Request.QueryString["price"];
if (Roles.IsUserInRole("Member"))
{
((Label)DataList1.Controls[0].FindControl("PriceLabel")).Text = salePrice;
}
else {
((Label)DataList1.Controls[0].FindControl("PriceLabel")).Text = price;
}
}
The priceLabel should show the SalesPrice value passed as querystring from another page. Please help me out. Thanks in advance!!!
Please try with the below code snippet.
ASPX
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" HorizontalAlign="Center" DataKeyField="ID" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
<asp:Label ID="PriceLabel" runat="server"></asp:Label>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
ASPX.CS
protected void Page_Load(object source, System.EventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name_1"}
};
DataList1.DataSource = data;
DataList1.DataBind();
}
protected void Page_PreRender(object source, System.EventArgs e)
{
string salePrice = Request.QueryString["SalesPrice"];
((Label)DataList1.Controls[0].FindControl("PriceLabel")).Text = salePrice;
}
Update 1
ASPX
<asp:DataList ID="DataList1" runat="server" HorizontalAlign="Center" DataKeyField="ID" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
<asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("SalesPrice") %>' ></asp:Label>
</ItemTemplate>
</asp:DataList>
ASPX.CS
protected void Page_Load(object source, System.EventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name_1", SalesPrice = 10.00}
};
DataList1.DataSource = data;
DataList1.DataBind();
}
protected void Page_PreRender(object source, System.EventArgs e)
{
if (Request.QueryString["SalesPrice"] != null && !string.IsNullOrEmpty(Request.QueryString["SalesPrice"]))
{
string salePrice = Request.QueryString["SalesPrice"];
((Label)DataList1.Controls[0].FindControl("PriceLabel")).Text = salePrice;
}
}
Let me know if any concern.
Just remove the space.
string salePrice = Request.QueryString["SalesPrice"];

UpdatePanel and panel visibility asp.net c#

I have two panels. Each panel contains an updatepanel.
The first panel is a password Textbox.
I set the second panel's visibility on page_load to false.
If the user enters the correct password, the second panel should be visible and the first panel shouldnd.
The code:
<asp:Panel ID="passwordPanel" runat="server">
<asp:UpdatePanel ID="UpdatePanel2" UpdateMode="Conditional" runat="server">
<ContentTemplate>
Geben Sie das Passwort ein:<br />
<br />
<asp:TextBox ID="txtPassword" AutoPostBack="false" runat="server" TextMode="Password"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnConfirmPassword" runat="server" AutoPostBack="true" Text="Senden" CssClass="button" OnClick="btnConfirmPassword_Click"/>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
<asp:Panel ID="panelUploadDownload" runat="server">
<h2>Upload Paketformeln CSV</h2>
<input type="file" id="myFile" name="myFile" />
<asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" CssClass="button" Text="Upload" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<br />
<asp:Label ID="lblStatus" runat="server" Text="statusLabel"></asp:Label>
<br />
<asp:Panel ID="panelChanges" runat="server" CssClass="pnlCSS">
<asp:Label ID="lblChangesHeader" runat="server" Font-Bold="True" ForeColor="Black" Text="Änderungen"></asp:Label>
<br />
<asp:Label ID="lblChanges" runat="server" ForeColor="#009900" Text="changes"></asp:Label>
<br />
<br />
<asp:Button ID="btnConfirm" runat="server" OnClick="btnConfirm_Click" CSSClass="button" Text="Änderungen bestätigen" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<br />
<br />
<br />
<h2>Download Paketformeln CSV</h2>
<p><asp:Button ID="btnDownloadCsv" runat="server" OnClick="btnDownloadCsv_Click" Text="Download CSV" CSSClass="button"/></p>
</asp:Panel>
And the C# code:
protected void btnConfirmPassword_Click(object sender, EventArgs e)
{
if (txtPassword.Text == "XX")
{
uploadDownloadPanel.Visible = true;
passwordPanel.Visible = false;
}
}
Load Event
protected void Page_Load(object sender, EventArgs e)
{
mainController = new MainController();
setStatus("", Color.Black);
lblChanges.Visible = false;
lblChangesHeader.Visible = false;
btnConfirm.Visible = false;
panelChanges.Visible = false;
panelUploadDownload.Visible = false;
}
For some reason it doesnt work. Any clues? Triggers?
The SecureString class doesn't allow you to see the value; that's the whole point of it. If you want to be able to work with the value entered into the PasswordBox, use the Password member of PasswordBox instead of the SecurePassword member:
protected void btnConfirmPassword_Click(object sender, EventArgs e)
{
if (txtPassword.Password == "XX")
{
uploadDownloadPanel.Visible = true;
passwordPanel.Visible = false;
}
}
i just removed the updatepanel from the passwordPanel and it worked.
In your load event, do the following
if(IsPostback)
{
uploadDownloadPanel.Visible = false;
}
Actually, every time when you press button, you load event also triggered
You can set your UpdateMode to Always of update panel within panelUploadDownload panel.It will work.
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Always" runat="server">

Form not rendering updated label after postback

I have been have problems with a postback from the button submit as after the button has finished the site think its a full refresh which gives me no chance to show any failure of login message or error,
I cant seem to pinpoint why this is happening and have looked up about the page cycle but can't put my finger on it.
Heres all my code for behind the page
protected void Page_Init(object sender, EventArgs e)
{
Session["user"] = "";
Session["domain"] = "";
Session["manager"] = "";
}
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
lblError.Text = "This was a post back.";
else
lblError.Text = "This was NOT a post back.";
}
protected void btnLogin_Click(object sender, EventArgs e)
{
//Allows the variable to be used through out the app
Session["user"] = username;
Session["domain"] = domain;
Session["manager"] = null;
if (true == authenticateUser(Session["domain"].ToString(), Session["user"].ToString(), password))
Response.Redirect(Response.ApplyAppPathModifier("Update.aspx"));
}
and front
<form id="form1" runat="server" style="Width:19%;" class="pure-form pure- form-stacked">
<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
<br />
<asp:TextBox ID="txtLoginID" Width="95%" Style="display:inline-block;" runat="server"></asp:TextBox>
</div>
<br/>
<asp:Label ID="lblpwd" runat="server" Text="Password"></asp:Label>
<asp:TextBox ID="txtPassword" Width="95%" TextMode="Password" runat="server"></asp:TextBox>
<asp:Label ID="lbldmn" runat="server" Text="Domain"></asp:Label>
<asp:DropDownList ID="ddlDomain" Width="95%" runat="server">
<asp:ListItem>hdnl.it</asp:ListItem>
<asp:ListItem>yodel.net</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblError" runat="server" ForeColor="Red" ></asp:Label>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="btnLogin" runat="server" Text="Login" Width="95%" OnClick="btnLogin_Click"
CssClass="button-success pure-button" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server"
AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<div class="overlay"></div>
<div class="modal">
<h2>Please Wait.....</h2>
<img alt="Loading..." src="/Images/loading.gif" />
</div>
</ProgressTemplate>
</asp:UpdateProgress>
</form>

All radiobuttons are checked c#

I have a problem with my 3 radiobuttons in c# at Visual Studio 2012 - when i run this application in browser and check first radiobutton, then check second the both of them are marked.
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
if (RadioButton1.Checked)
{
ListBox1.Items.Add(TextBox1.Text);
TextBox1.Text = "";
}
else if (RadioButton2.Checked)
{
ListBox2.Items.Add(TextBox1.Text);
TextBox1.Text = "";
}
else if (RadioButton3.Checked)
{
ListBox3.Items.Add(TextBox1.Text);
TextBox1.Text = "";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
{
ListBox1.Items.Clear();
ListBox2.Items.Clear();
ListBox3.Items.Clear();
}
}
}
}
.
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
<section class="featured">
<div class="content-wrapper">
<asp:TextBox ID="TextBox1" runat="server" Width="195px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Height="32px" Text="Button" OnClick="Button1_Click1" />
<br />
<asp:RadioButton ID="RadioButton1" runat="server" />
<asp:RadioButton ID="RadioButton2" runat="server" />
<asp:RadioButton ID="RadioButton3" runat="server" />
<br />
<br />
<asp:ListBox ID="ListBox1" runat="server" Height="123px" Width="126px"></asp:ListBox>
<asp:ListBox ID="ListBox2" runat="server" Height="123px" Width="126px"></asp:ListBox>
<asp:ListBox ID="ListBox3" runat="server" Height="123px" Width="126px"></asp:ListBox>
<br />
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
</div>
</section>
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
</asp:Content>
Make sure you set the GroupName attribute of the radiobuttons to the same value ( a name for that group of radio buttons).
eg:
<asp:RadioButton id="Radio1" Text="something" GroupName="RadioGroup1" runat="server" />
<asp:RadioButton id="Radio2" Text="another thing" GroupName="RadioGroup1" runat="server" />
<asp:RadioButton id="Radio3" Text="something else" GroupName="RadioGroup1" runat="server" />

Modal popup not generating postback to page

<ajaxToolkit:ModalPopupExtender runat="server"
id="ModalPopupExtender1"
cancelcontrolid="btnCancel" okcontrolid="btnOkay"
targetcontrolid="Button1" popupcontrolid="Panel1"
drag="true"
backgroundcssclass="ModalPopupBG"
/>
<asp:Button ID="Button1" runat="server" Text="Test Modal Popup"
onclick="Button1_Click" />
<br />
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<asp:Button ID="Button2" runat="server" Text="Post Back"
onclick="Button2_Click" />
<asp:Label ID="Label1" runat="server" AutoPostBack="true" Text="Nothing has happened yet..."></asp:Label>
<asp:Panel ID="Panel1" runat="server" AutoPostBack="true">
<div class="HellowWorldPopup">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="btnCancel" runat="server" Text="Canel"
onclick="btnCancel_Click" />
<asp:Button ID="btnOkay" runat="server" Text="Okay" onclick="btnOkay_Click" />
</div>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
So I am trying to get the Label to have the contents of what the user typed in the textbox inside the modal popup. Right now the btnOkay is not causing this to work.
.cs file:
protected void btnCancel_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
}
protected void btnOkay_Click(object sender, EventArgs e)
{
Label1.Text = TextBox1.Text;
TextBox1.AutoPostBack = true;
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "";
TextBox1.Text = "";
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text = "You clicked the button";
}
I do not want the page to post back at all, but just to update the hidden labels on the page once information is entered. How do I do this?
Edit:
Alright, this should do the trick I think.
<ajax:ModalPopupExtender runat="server" id="ModalPopupExtender1" targetcontrolid="Button1"
popupcontrolid="Panel1" drag="true" backgroundcssclass="ModalPopupBG" />
<asp:Button ID="Button1" runat="server" Text="Test Modal Popup" /><br />
<asp:Panel ID="Panel1" runat="server">
<div class="HellowWorldPopup">
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="btnCancel" runat="server" Text="Canel" onclick="btnCancel_Click" />
<asp:Button ID="btnOkay" runat="server" Text="Okay" onclick="btnOkay_Click" />
</div>
</asp:Panel>
<asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Nothing has happened yet..." />
</ContentTemplate>
</asp:UpdatePanel>
.
protected void btnCancel_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
}
protected void btnOkay_Click(object sender, EventArgs e)
{
Label1.Text = TextBox1.Text;
up.Update();
}

Categories

Resources