Dylan Jackson
Dylan Jackson

Reputation: 811

Selecting session variable with an IF statement

I have this code to send variables to a datagrid on another page

 protected void Button1_Click(object sender, EventArgs e)
    {
        int agent = int.Parse(txtAgencyCode.Text);
        Session["para1"] = agent;
        Button1.Attributes.Add("onclick", "window.open('http://localhost:50771/WebSite4/Datagrid.aspx'); return false;");

    }

    protected void btnTitleSearch_Click(object sender, EventArgs e)
    {
        Session["para2"] = txtTitleSearch.Text;
        btnTitleSearch.Attributes.Add("onclick", "window.open('http://localhost:50771/WebSite4/Datagrid.aspx'); return false;");
    }

and this code on the other page that uses the session variable from button one

protected void Page_Load(object sender, EventArgs e)
    {
        int field1 = (int)(Session["para"]);
        localhost.Service ws = new localhost.Service();
        GridView1.DataSource = ws.GetJobsByAgencyID(field1);
        GridView1.DataBind();
    }

What i cannot figure out is how to make an if statement (or even if it will be an if statement that is used) to decide which parameter is passed to my datagrid.

For info there will be another 3- 4 controlos on the default page (Only one of which will be activated) and the parameters will take diffeent types.

EDIT So sorry you all found my question hard to understand, im not by any means a profesional or even what yuo may call a competent. James Hill and Ravi gave me pretty much what I was after (need to test but looks like it). Thanks all for trying :D

Upvotes: 2

Views: 6329

Answers (4)

Bill Martin
Bill Martin

Reputation: 4943

If the data isn't sensitive, rather than setting a session variable, I recommend using the querystring in your windowopen and passing the param that way.

 Button1.Attributes.Add("onclick", "window.open('http://localhost:50771/WebSite4/Datagrid.aspx?dataPassed=1'); return false;");

In the code behind of the grid, do a case statement:

int data= (int)(Request.Querystring["dataPassed"]);
switch(data)
{  case 1:
       dosomething();
       break;
   case 2:
       dosomethingelse();
       break
   default:
      throw;
      break:
}

Upvotes: 0

Jimmy
Jimmy

Reputation: 3264

Why can't you have another session variable called context?

string context = Session["context"];

switch(context)
{
case "titlesearch":
//do the things 
break;

}

syntax is not checked, only the idea.

Upvotes: 0

James Hill
James Hill

Reputation: 61842

You're question is a bit difficult to understand. I think what you're asking is how to determine if a session variable exists. For that, simply use:

if (Session["para"] != null) {
    //para exists
}
else if (Session["para2"] != null) {
    //para2 exists
}
...

Upvotes: 2

Ravi Gadag
Ravi Gadag

Reputation: 15881

if i understood correctly, you need branching based on value stored in your session,

you can try like this

  if (Session["para1"] != null) 
    {
      int AgentCode = Convert.ToInt32(Session["para1"].ToString());

      //Search by AgentCode 
           GridView1.DataSource = ws.GetJobsByAgencyID(AgentCode );
            GridView1.DataBind();

    }

   else if (Session["para2"] != null)
    {
      string title = Session["para1"].ToString();

       //Search by title
       GridView1.DataSource = ws.GetJobsByAgencyID(title );
        GridView1.DataBind();
    }
   else
   {
      // your default parameters
    }

Upvotes: 2

Related Questions