2012-09-26 46 views
0

我正在處理我的第一個Web項目,並且在使用GridView時需要添加IF邏輯的幫助。我有一個頁面(CustomerListPage),其中有一個GridView項目,顯示可以單擊的客戶(行)列表。點擊客戶後,用戶將被重定向到一個名爲「CustomerUsagePage」的頁面,該頁面顯示該客戶的最新記錄。這就是我的GridView的代碼看起來像我的CustomerListPage:如何使用GridView實現IF語句

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

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

我的問題是,我只能添加IF邏輯的頁面,我的GridView位於。我想單擊一個客戶,並使用if語句將他的數據顯示在我的CustomerUsagePage上。可能嗎?我知道我可以通過爲每個客戶添加一個頁面來做到這一點,但我不想沿着這條路線走下去,這似乎很乏味。有沒有人有什麼建議?

+0

你是什麼意思「添加IF邏輯」嗎?你想要檢查什麼? –

+0

我想通過檢查行的索引來檢查選擇了哪個客戶。例如:如果行[1] {//顯示信息} – Rick

回答

2

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

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

或者你可以使用經典的行爲與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 
    } 
} 
+0

會將此代碼放在CustomerUsagePage上?因爲它看起來像在GridView所在的CustomerListPage中。 – Rick

+0

您將代碼添加到GridView RowCommand事件中 –

+0

我可能錯了,但這似乎並不是我卡住的地方。我已經有我的GridView項目上的if語句。問題是,我正在重定向到同一頁面(CustomerUsagePage)。我認爲這是IF聲明應該發佈的頁面。這樣,頁面會檢查選擇了哪一行,然後知道要顯示哪些數據。 – Rick