2012-01-31 110 views
2

我有一个使用会话变量从按钮的代码将变量发送到DataGrid另一页选择会话变量与IF语句

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;"); 
    } 

上与其他网页上的代码一个

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(); 
    } 

我无法弄清楚的是如何使if语句(或者即使它将成为使用if语句)来决定将哪个参数传递给我的数据网格。

有关信息,将在默认页面上显示另外3至4个控制器(其中只有一个将被激活),参数将采用不同类型。

编辑 很抱歉,你都发现我的问题很难理解,即时没有任何手段使某一设备专业甚至什么宥可致电主管。 詹姆斯希尔和拉维给了我很多我以后的(需要测试,但看起来像)。感谢所有的尝试:D

+0

你的病情会是怎样? – 2012-01-31 15:14:49

+4

这是什么我甚至没有 – jadarnel27 2012-01-31 15:18:25

+1

@ jadarnel27你让我笑得很难! – 2012-01-31 15:21:31

回答

2

如果我理解正确的话,你需要分支基于存储在您的会话值,

你可以试试像这样

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 
    } 
2

你的问题有点难以理解。我想你问的是如何确定会话变量是否存在。为此,只需使用:

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

这不会编译 - 一个额外的括号:) – 2012-01-31 15:18:34

+1

@AndreasNiedermair,:)。固定。没有我的IDE,我会做什么? – 2012-01-31 15:19:36

0

如果数据不敏感,而不是设置一个会话变量,我建议使用在你windowopen查询字符串并通过帕拉姆的方式。

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

在网格背后的代码,做一个case语句:

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

...为什么?这个解决方案对于实际实现有什么好处?! – 2012-01-31 15:21:14

+0

查询字符串中不使用服务器资源。此外,查询字符串是无状态的,您可以获得可移植性,因为如果需要的话,可以将此功能移到客户端,这将允许在没有重新编译的情况下进行更改。如果您打算重用该值,请使用会话变量。如果不是,客户端方法是首选。 – 2012-01-31 15:25:04

+0

哇...你没有得到这个问题......如果操作系统使用客户端或服务器端来存储一些值,那么这并不重要......在某处操作系统必须执行if语句,不会改变。所以你的答案有哪些合法性? – 2012-01-31 15:26:50

0

为什么你不能有另一个称为上下文的会话变量?

string context = Session["context"]; 

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

} 

语法不检查,只有想法。