2012-04-18 96 views
1

我無法得到答案,我需要它!GridView通過ID獲取數據

問題的確是我可以通過gridview上的選擇獲取數據通過id,但是然後我使用搜索選項我在頁面上實現和gridview顯示它得到的匹配結果但如果我按下選擇它將重定向到與錯誤的頁面的頁面,而不是獲得我選擇的一個ID它獲得該單元格的第一個位置上的字段的ID。

下面是代碼:

protected void Page_Load(object sender, EventArgs e) 
{ 

    TeamGest.DBLayer.DBLTeams dbl = new TeamGest.DBLayer.DBLTeams(); 
    GridView1.DataSource = dbl.List(); 
    GridView1.DataBind(); 

    TeamGest.DBLayer.DBLPlayers dbl1 = new TeamGest.DBLayer.DBLPlayers(); 
    GridView2.DataSource = dbl1.List(); 
    GridView2.DataBind(); 
} 

protected void MyMenu_MenuItemClick(object sender, MenuEventArgs e) 
{ 
    { 
     MyMultiView.ActiveViewIndex = Int32.Parse(e.Item.Value); 
     int i; 
     for (i = 0; i <= MyMenu.Items.Count - 1; i++) 
     { 
      if (i == Convert.ToInt32(e.Item.Value)) 
      { 
       MyMenu.Items[i].Text = MyMenu.Items[i].Text; 
      } 
      else 
      { 
       MyMenu.Items[i].Text = MyMenu.Items[i].Text; 
      } 
     } 
    } 
} 

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    GridViewRow row = GridView1.SelectedRow; 

    Response.Redirect("DetalhesClube.aspx?Id="+row.Cells[0].Text); 

} 
protected void Button1_Click1(object sender, EventArgs e) 
{ 
    string searchStringTeam = TextBox1.Text; 
    GetTeamResults(searchStringTeam); 
} 
protected void GridView2_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    GridViewRow row = GridView2.SelectedRow; 
    Response.Redirect("DetalhesJogador.aspx?Id=" + row.Cells[0].Text); 
} 

protected void Button2_Click(object sender, EventArgs e) 
{ 
    string searchStringPlayer = TextBox2.Text; 
    GetPlayerResults(searchStringPlayer);  
} 

回答

0

而是在查詢字符串追加row.Cells[0].Text,在網格視圖的ItemTemplate中使用標籤控制,使用eval腳本側填充control.once網格視圖得到填充的標籤也會填充eval腳本中給出的值。

現在使用findcontrol c#函數來獲取網格內的標籤值並在查詢字符串中追加該值。

參考:How to find control in TemplateField of GridView?