c# - Gridview 行命令

标签 c# asp.net gridview

当我转到网站中的 gridview 时,我遇到了一个小问题,我的行命令按钮不起作用。我不知道为什么。

这是我的页面代码:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
        if (e.CommandName == "Ver")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow selectedRow = GridView1.Rows[index];

            Session["Imagem"] = GridView1.DataKeys[index]["Imagem"];
            Session["Nome"] = GridView1.DataKeys[index]["Nome"];
            Session["Preço"] = GridView1.DataKeys[index]["Preço"];
            Session["Descricao"] = GridView1.DataKeys[index]["Descricao"];
            Session["Categoria"] = GridView1.DataKeys[index]["Categoria"];
            Session["id"] = GridView1.DataKeys[index]["Id"];
            Session["estado"] = GridView1.DataKeys[index]["estado"];
              GridView1.DataBind();
            Response.Redirect("Detalhes.aspx");
      }
}

我的 gridview 标记是这样的:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     DataKeyNames="Imagem ,Nome,Preço,Descricao,Categoria,id,estado" 
     DataSourceID="SqlShop" OnRowCommand="GridView1_RowCommand" 
     BackColor="White" BorderColor="#3366CC" BorderStyle="None" 
     BorderWidth="1px" CellPadding="4" style="margin-top: 0px">
    <Columns>
        <asp:TemplateField HeaderText="Imagem" SortExpression="Imagem">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Imagem") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Imagem") %>' Height="8%" Width="8%" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Nome" HeaderText="Nome" SortExpression="Nome" />
        <asp:BoundField DataField="Preço" HeaderText="Preço" SortExpression="Preço" />
        <asp:BoundField DataField="Descricao" HeaderText="Descricao" SortExpression="Descricao" />
        <asp:BoundField DataField="Categoria" HeaderText="Categoria" SortExpression="Categoria" />
        <asp:ButtonField ButtonType="Button" CommandName="Select" Text="ver" />
    </Columns>
    <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
    <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
    <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
    <RowStyle BackColor="White" ForeColor="#003399" />
    <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
    <SortedAscendingCellStyle BackColor="#EDF6F6" />
    <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
    <SortedDescendingCellStyle BackColor="#D6DFDF" />
    <SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
<asp:SqlDataSource ID="SqlShop" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
     SelectCommand="SELECT * FROM [Produtos]">
</asp:SqlDataSource>

最佳答案

C#代码中的命令名不正确

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select") //this part
        {
            int index = Convert.ToInt32(e.CommandArgument);
            ...
            ...
            Response.Redirect("Detalhes.aspx");
        }

    }

或者 您可以将 ButtonField 参数的值更改为

    <asp:ButtonField ButtonType="Button" CommandName="ver" Text="Select" />

关于c# - Gridview 行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31313479/

相关文章:

c# - 检查 dateTime 是周末还是工作日

c# - 将 Sql Server uniqueidentifier/updated date 列与 Linq to Sql 结合使用 - 最佳方法

c# - 使用 Autofac 将 SignalR IHubContext 注入(inject) Controller

asp.net - Azure站点: Compiler Error Message: CS1056: Unexpected character '$'

c# - 如何解决RDLC中的 "Report item not linked to Dataset"错误?

asp.net - 按适当的列(即 TemplateField)对 gridView 进行排序

html - 如何用 6 个元素制作这样的 HTML 网格系统?

c# - 哪些 C# 类对象获取非托管资源?有名单吗?

c# - FluentMigrator 命令返回未找到迁移

c# - 使用 DataBind() 后无法对 GridView 进行排序