0

我想获取单击的列的名称和单击的行的值。

我找到了很好的例子,但我无法获得我想要的数据。

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

1 回答 1

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
    }
}
于 2012-06-11T12:02:22.173 回答