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 に答える