6 Replies - 1222 Views - Last Post: 28 May 2013 - 09:59 PM Rate Topic: -----

#1 Joni_78  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-November 12

Send ListBox SelectedValue into next page?

Posted 28 May 2013 - 05:57 AM

Is it possible to somehow send the ListBox SelectedValue into next page? I have lbMovies ListBox in main page, when user selects item in it and clicks a button then next page loads, but lbMovies.SelectedValue is not accessible from there anymore.

protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            Something in here?
        }

    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        using (SqlCeConnection conn = new SqlCeConnection(GetConnectionString()))
        {
            try
            {
                conn.Open();
                SqlCeCommand cmd = new SqlCeCommand("UPDATE Movies SET Name=@Val1,... conn);
                cmd.Parameters.AddWithValue("@Val1", tbName.Text);
                ...
                cmd.Parameters.AddWithValue("@Val6", lbMovies.SelectedValue);
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message;
            }
            finally
            {
                conn.Close();
            }
        }
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Send ListBox SelectedValue into next page?

#2 Joni_78  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-November 12

Re: Send ListBox SelectedValue into next page?

Posted 28 May 2013 - 08:05 AM

For example, trying something simple that should work.

I added this on code behind on first page:

public TextBox MovieID
    {
        get { return tbID; }
    }


Then this on the second page:

if (PreviousPage != null)
        {
            TextBox1.Text = PreviousPage.MovieID.Text;
        }


But whatever I try I get this error: 'System.Web.UI.Page' does not contain a definition for 'MovieID' and no extension method 'MovieID' accepting a first argument of type 'System.Web.UI.Page' could be found (are you missing a using directive or an assembly reference?)

These are the using strings I have:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlServerCe;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8937
  • View blog
  • Posts: 33,462
  • Joined: 12-June 08

Re: Send ListBox SelectedValue into next page?

Posted 28 May 2013 - 08:08 AM

No.. you wouldn't do that. Don't forget ASP.NET pages are just slightly more fancy version of a webpage. When a page refreshes there is no previous existing form (like say in a win app). The client blows that data out and grabs the new page.

What you need to do is read up on the 'session state' and 'session variables'.

http://msdn.microsof...=vs.100%29.aspx
http://msdn.microsof...y/ms952597.aspx
Was This Post Helpful? 0
  • +
  • -

#4 Joni_78  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-November 12

Re: Send ListBox SelectedValue into next page?

Posted 28 May 2013 - 08:36 AM

View Postmodi123_1, on 28 May 2013 - 08:08 AM, said:

No.. you wouldn't do that. Don't forget ASP.NET pages are just slightly more fancy version of a webpage. When a page refreshes there is no previous existing form (like say in a win app). The client blows that data out and grabs the new page.

What you need to do is read up on the 'session state' and 'session variables'.

http://msdn.microsof...=vs.100%29.aspx
http://msdn.microsof...y/ms952597.aspx


This has a masterpage, I thought that postback can do this? Also I have a working sample site of my second post and that site works, just when i try to add that same in my site, it doesn't.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8937
  • View blog
  • Posts: 33,462
  • Joined: 12-June 08

Re: Send ListBox SelectedValue into next page?

Posted 28 May 2013 - 08:40 AM

Quote

This has a masterpage, I thought that postback can do this?

Postback is about submitting data to the server and, as far as you explained it, you are interested in pass data between pages. The best way for that - session variables.


Quote

Also I have a working sample site of my second post and that site works, just when i try to add that same in my site, it doesn't.

I have no idea what this means. Rephrase that statement
Was This Post Helpful? 0
  • +
  • -

#6 Joni_78  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-November 12

Re: Send ListBox SelectedValue into next page?

Posted 28 May 2013 - 09:23 PM

I mean I have a site that works like this

On the Default.aspx there is TextBox named tbID. It gets a value from SQL database like this:
protected void lbCustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selected = lbCustomer.SelectedValue;
        SqlConnection con = null;
        SqlCommand cmd = null;
        string sql = "Select * from Customer where Name='" + selected + "'";
        
        using (con = new SqlConnection(conStr))
        {
            cmd = con.CreateCommand();
            try
            {
                con.Open();
                cmd.CommandText = sql;
                SqlDataReader dr = null;
                dr = cmd.ExecuteReader(); 
                while (dr.Read())
                {
                    ...
                    tbID.Text =dr["CustomerID"].ToString();
                   
                }
                dr.Close();
                con.Close();         
            }     
            catch
            {
                
            }
        }   
    }


Then there is this later on that page:
public TextBox CustomerID
    {
        get { return tbID; }
    }

 protected void btnAddCustomer_Click(object sender, EventArgs e)
    {
        Server.Transfer("Add.aspx");
    }


Then on the Add.aspx page there is this:
protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            TextBox1.Text = PreviousPage.CustomerID.Text;
        }   
    }


In this sample, Add.aspx page gets value from previous page.
Was This Post Helpful? 0
  • +
  • -

#7 Joni_78  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-November 12

Re: Send ListBox SelectedValue into next page?

Posted 28 May 2013 - 09:59 PM

If I try to do the same I get error on CustomerID. 'System.Web.UI.Page' does not contain a definition for 'MovieID' and no extension method 'MovieID' accepting a first argument of type 'System.Web.UI.Page' could be found (are you missing a using directive or an assembly reference?)

protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            TextBox1.Text = PreviousPage.CustomerID.Text;
        }

    }

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1