2012-06-11 21 views
0

我想獲取點擊列的名稱和點擊行的值。Gridview Row - 值和列名

我發現很好的例子,但我不能得到我想要的這些數據。

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       e.Row.Attributes["onClick"] = "location.href='view.aspx?id_lekarza=" + DataBinder.Eval(e.Row.DataItem, "Id_lekarza") + " &klikniete=" + "sss" + " '"; 
      } 
     } 

e.Row.RowIndex所有時間0?

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       e.Row.Attributes.Add("onclick", "javascript:alert('" + e.Row.RowIndex + "');"); 
       e.Row.Style.Add("cursor", "pointer"); 
      } 
     } 

回答

1

在你的GridView可以添加模板字段象下面這樣:

<asp:TemplateField ItemStyle-Width="44px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" 
          HeaderStyle-Height="40px" ShowHeader="false"> 
          <ItemTemplate> 
           <asp:ImageButton ID="btnSelect" runat="server" ImageUrl="~/Images/btnSelect.png" 
            CommandName="Select" CommandArgument='<%# Eval("ID") %>'/></ItemTemplate> 
          <HeaderStyle Height="40px"></HeaderStyle> 
          <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="44px"></ItemStyle> 
</asp:TemplateField> 

而在你的C#代碼隱藏您應該創建的GridView的RowCommand事件,並訪問該行的CommandArgument像以下:

protected void GV_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
if (e.CommandName == "Select") 
    { 
    //You get the ID of that specific row that you have selected : 
    string ID=e.CommandArgument.ToString(); 
    //Here you write your code !   For example a select query to get that row from database by the ID you have gained in above line of code 
    } 
} 
+0

你可以舉個例子來說明事件GridView1_RowCreated()嗎? – RPD

+0

上面的代碼就是一個例子!你有沒有失望?你非常想知道什麼? – Karamafrooz

+0

列的名稱被選中行 – RPD