Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
When a user is insert a number in the textbox, I need to save this information in a session.

Because when the user have picked 7 fruits the panel will close and a new panel with the information off what fruit and how many fruit he have picked is going to get eval out in a repeater or label.

But my session is showing 0.

This is what I have try:

C#
<asp:Panel ID="Panel_visfrugt" runat="server">
        <table border="1">
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [frugt]"></asp:SqlDataSource>
            <tr>
                <td>Antal</td>
                <td><asp:Label ID="Label1" runat="server" Text=""></asp:Label></td>
            </tr>
            <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemCommand="Repeater1_ItemCommand">
                <ItemTemplate>

            
            <tr>
                <td><%#Eval("frugt_navn") %></td>
                <td><asp:TextBox ID="txtbox1" runat="server" Width="30"></asp:TextBox></td>
                
            </tr>
           
    </ItemTemplate>
            </asp:Repeater>
        </table>
    
    <asp:Button ID="Button1" runat="server" Text="Læg i kurv" OnClick="Button1_Click1" /> 
    </asp:Panel>
        <asp:Panel ID="Panel_vis" runat="server" >
            
            <h4>De har nu lagt disse ting i kurven</h4>
            <asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource1">
                <ItemTemplate>

              <%#Eval("frugt_navn") %>

            
            <asp:Label ID="Label_vis" runat="server" Text="Label"></asp:Label>
  </ItemTemplate>
            </asp:Repeater>
        </asp:Panel>
        <asp:Button ID="Button2" runat="server" Text="slet session" OnClick="Button2_Click" />


C#
 private int Value = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
       
        Panel_visfrugt.Visible = true;
        Panel_vis.Visible = false;
        
       
    }
protected void Button1_Click1(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in Repeater1.Items)
        {
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                TextBox Textbox_antal = item.FindControl("txtbox1") as TextBox;

                if (Textbox_antal != null)
                {
                    Textbox_antal.Text = "0";
                }
                
                Value = Convert.ToInt32(Value + int.Parse(Textbox_antal.Text));
                Session["frugt"] = Value;

                if (Value != 7)
                {
                    Label1.Text = "Der er 7 nu";
                    Panel_visfrugt.Visible = false;
                    Panel_vis.Visible = true;

                }
                else if (Value > 7)
                {
                    Label1.Text = "Det er kun muligt at bestille 7 stk frugt";
                    
                }
            }
        }
        if (Session["frugt"] != null)
        {
            Label_vis.Text = Convert.ToString(Session["frugt"]);
        }
          
              
    }



    protected void Button2_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Response.Redirect("Default.aspx");
    }


What is I doing wrong?

Tina
Posted
Comments
M Rayhan 8-Oct-13 7:13am    
Did you check that which value is going inside Session["frugt"] using line by line debugging? Probably in your foreach loop any zero value is overwriting the value of Session["frugt"]. CHeck that.
tina_overgaard 8-Oct-13 7:26am    
There is no value getting inside the session
RelicV 8-Oct-13 7:30am    
if (Textbox_antal != null)
{
Textbox_antal.Text = "0";
}
That's where the error is. As far as my understanding of the code goes, if the textbox is not null, then set the textbox's value to '0', which would be overwriting the user entered value.

This is not what you want.. do you..? If the textbox exists, then why are you re-setting its value to 0..?

Makes sense..? If not, pls let me know.

Thnx,
RelicV
tina_overgaard 8-Oct-13 7:33am    
It make sense. But how do I set the Textbox_antal to be 0 when nothing is insert in the textbox
RelicV 8-Oct-13 7:36am    
if (Textbox_antal != null)
{
if(string.IsNullOrEmpty(Textbox_antal.Text))
{
Textbox_antal.Text = "0";
}
}

This is how you are going to set the value to Zero if there is no data entered into the textbox.

Try This

Session["number"] = Conver.ToInt32(TxtNum.Text);
 
Share this answer
 
v2
is Textbox_antal.Text have float or decimal value
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900