2 Replies - 3964 Views - Last Post: 10 November 2011 - 12:10 PM

#1 irah123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-November 11

Not able to pass value from SelectedIndexChanged when button clicked

Posted 09 November 2011 - 03:26 PM

Not able to pass value from dropdown list 's SelectedIndexChanged when a button is clicked.

The Label13.Text is not displaying the selected value and it is always displaying 0 (int default value).

Whereas label14.text is displaying the intended value ( selected value).
namespace PIM
{
    public partial class InsertProjectScreen : System.Web.UI.Page
    {
        public int companyid;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label13.Text = companyid.ToString();

        }

   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            companyid = Convert.ToInt32(DropDownList1.SelectedItem.Value);
            Label14.Text = companyid.ToString();
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Not able to pass value from SelectedIndexChanged when button clicked

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Not able to pass value from SelectedIndexChanged when button clicked

Posted 09 November 2011 - 09:31 PM

Every time your page is created it is going to reset the value of companyid to the default value. If you want to save the value across postbacks you will need to store the value in a Session variable.

Instead of just

public int companyid;



you could change it to

public int companyid
{
  get
  {
    object temp = Session["companyid"];
    return (temp == null) ? 0 : (int)temp;
  }
  set
  {
    Session["companyid"] = value;
  }
}



This will store the value in a session variable named "companyid". If you have not set a value for it yet then it will return 0, otherwise it will return the value that was entered into it.

This post has been edited by Nakor: 09 November 2011 - 09:32 PM

Was This Post Helpful? 1
  • +
  • -

#3 irah123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-November 11

Re: Not able to pass value from SelectedIndexChanged when button clicked

Posted 10 November 2011 - 12:10 PM

thanks it works!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1