SelectedIndexChanged to filter ascx control

439 Views Asked by At

I am using a databound dropdown list to populate a combobox with project iterations and an ascx control to display a tag cloud. I am retrieving the selectedValue of the dropdown and storing it as a session to filter out the tag cloud (for the entire project or by iteration). I am getting an error, because the default value which I have entered cannot be then converted to an integer. Thanks in advance for your help!

filteroptions.Items.Insert(0, "Entire Project");

ASP.NET FILE:

protected void filteroptions_SelectedIndexChanged(object sender, EventArgs e)
{
    string selected_iteration = filteroptions.SelectedValue;

    Session["iteration"] = selected_iteration;

}

ASCX CONTROL:

protected void Page_Load(object sender, EventArgs e)
{
    proj_name = Request.QueryString["project"].ToString();
    proj_id = Request.QueryString["id"].ToString();

    iteration = (string)Session["iteration"]; 

    BindTagCloud();

}

private void BindTagCloud()
{

    int pro_id = Convert.ToInt32(proj_id);
    int iteration_id = Convert.ToInt32(iteration);

 ....

 if (iteration_id != 0)
    {
        ListView1.DataSource = tagCloudNegativeIteration;
        ListView1.DataBind();

        ListView2.DataSource = tagCloudPositiveIteration;
        ListView2.DataBind();

    }
    else
    {
        ListView1.DataSource = tagCloudNegative;
        ListView1.DataBind();

        ListView2.DataSource = tagCloudPositive;
        ListView2.DataBind();

    }
}
3

There are 3 best solutions below

0
On BEST ANSWER

The problem is that you are setting the iteration value to null on the initial load. You can use this code to always fall back to a default if for any reason your session variable becomes null. You might want to make your iteration variable an integer so you can convert it in your load.

if(String.IsNullOrEmpty(Sesssion["iteration"])
     iteration = "0";
else
     iteration = Session["iteration"]

And change the way your adding items to what aquinas suggested.

0
On

use this..

if(Session["iteration"] == Defaultvalue)
  itereation = "0";
else
  iteration = (string)Session["iteration"]; 

And default value is the value that is stored in session["iteration"] if no value is stored in that then use null as defaultvalue.

0
On

Well, you're not storing an integer value. This code:

filteroptions.Items.Insert(0, "Entire Project");

is probably not doing what you think it is doing. This is not saying "add a new listitem with a key of 0 and the text "Entire Project". Instead, it is saying insert a new listitem at Position 0 with Value and Text of "Entire Project"

you probably want something like,

filteroptions.Items.Insert(0, new ListItem("Entire Project", "0"));