简体   繁体   English

如何使用GridView实现IF语句

[英]How to implement IF statement using GridView

I am working on my first web project and I need help adding IF logic when using GridView. 我正在开发我的第一个Web项目,在使用GridView时我需要帮助添加IF逻辑。 I have a page (CustomerListPage) that has a GridView item that displays a list of customers(rows) that can be clicked. 我有一个页面(CustomerListPage),它有一个GridView项目,显示可以单击的客户(行)列表。 Once the customer is clicked, the user is redirected to a page called "CustomerUsagePage" which shows the most recent record of this customer. 单击客户后,用户将被重定向到名为“CustomerUsagePage”的页面,该页面显示该客户的最新记录。 This is what my GridView code looks like for my CustomerListPage: 这是我的CustomerListPage的GridView代码:

 if (GridView1.SelectedRow.RowIndex == 0)
   {
       //string selectedUser = GridView1.SelectedRow.Cells[0].ToString();
       Response.Redirect("CustomerUsagePage.aspx?Customer_Name=" );
       BindGridview();
   }

The code for my CustomerUsagePage looks like this: 我的CustomerUsagePage的代码如下所示:

private void BindControlvalues()
{

    con.Open();
    SqlCommand cmd = new SqlCommand("select * from dbo.CustomerTable", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    con.Close();
    DataSet ds = new DataSet();
    da.Fill(ds);
    Label4.Text = ds.Tables[0].Rows[0][1].ToString();
    Label1.Text = ds.Tables[0].Rows[0][0].ToString();
    Label2.Text = ds.Tables[0].Rows[0][3].ToString();
    Label3.Text = ds.Tables[0].Rows[0][4].ToString();
}

My problem is that I can only add IF logic the page where my GridView is located. 我的问题是我只能在我的GridView所在的页面添加IF逻辑。 I would like to click on a customer and have his data appear on my CustomerUsagePage with an if statement. 我想点击一个客户,并使用if语句将他的数据显示在我的CustomerUsagePage上。 Is it possible? 可能吗? I know I can do this by adding a page per customer but I dont want to go down that route, it seems to tedious. 我知道我可以通过为每个客户添加一个页面来做到这一点,但我不想沿着那条路走下去,这似乎很乏味。 Does anyone have any suggestions? 有没有人有什么建议?

You can use GridView-FormView (Master/Detail) Control 您可以使用GridView-FormView (Master/Detail) Control

Link : http://www.codeproject.com/Articles/16780/GridView-FormView-Master-Detail-Control . 链接: http//www.codeproject.com/Articles/16780/GridView-FormView-Master-Detail-Control

Or you can use classic behavior with ItemCommand event 或者您可以使用ItemCommand事件的经典行为

void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    if(e.CommandName=="Add")
    {
      int index = Convert.ToInt32(e.CommandArgument);

      GridViewRow row = CustomersGridView.Rows[index];

      .....//Adjust your Response
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM