当我点击gridview中的按钮时它不起作用

时间:2014-11-27 10:44:55

标签: asp.net data-binding

我是Asp.net的初学者,制作了一个小项目,但遇到了大问题,:) ... 现在我有一个情况,我使用gridview从Access数据库中检索数据,我完成了,我在网格视图中添加了按钮。当我点击按钮时出现以下错误

  

HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    Width= "100%" CellPadding="3">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <table class="style17" width="100%" border=" 0">
                    <tr>
                        <td height="100%" width="25%">
                            <asp:Image ID="Image6" runat="server" Height="144px" 
                                ImageUrl='<%# "data:image/jpg;base64, " + Convert.ToBase64String((byte[]) Eval("Picture")) %>' 
                                Width="158px" BorderColor="Black" BorderStyle="Inset" BorderWidth="1px" />
                        </td>
                        <td align="center" height="100%" width="75%">
                            <table align="right" class="style17">
                                <tr>
                                    <td align="left">
                                        <asp:Label ID="Label7" runat="server" style="font-size: 15pt; color: #0000FF" 
                                            Text='<%# Bind("Title") %>'></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label ID="Label6" runat="server" Text='<%# Bind("Brand") %>' 
                                            CssClass="style18" Font-Size="15pt"></asp:Label>
                                    </td>
                                    <td align="right">
                                        <asp:ImageButton ID="ImageButton1" runat="server" Height="51px" 
                                            ImageUrl="~/Images/orange_addtocart-trans.png" Width="159px" 
                                            onclick="ImageButton1_Click" />
                                    </td>
                                </tr>
                                <tr>
                                    <td align="left">
                                        <strong>Rs:</strong><asp:Label ID="Label2" runat="server" 
                                            Text='<%# Bind("Price") %>' CssClass="style18" Font-Size="15pt"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("Color") %>' 
                                            CssClass="style18" Font-Size="15pt"></asp:Label>
                                    </td>
                                    <td align="right">
                                        <asp:ImageButton ID="ImageButton2" runat="server" Height="51px" 
                                            ImageUrl="~/Images/orange_addtocart-trans.png" Width="159px" 
                                            onclick="ImageButton2_Click" />
                                    </td>
                                </tr>
                                <tr>
                                    <td align="left">
                                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("Condition") %>' 
                                            CssClass="style18" Font-Size="15pt"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label ID="Label5" runat="server" Text='<%# Bind("Material") %>' 
                                            CssClass="style18" Font-Size="15pt"></asp:Label>
                                    </td>
                                    <td>
                                        &nbsp;</td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <EmptyDataTemplate>
        <table class="style17">
            <tr>
                <td align="left" height="100%" width="30%">
                    <asp:Image ID="Image5" runat="server" Height="144px" 
                        ImageUrl='<%# "data:image/jpg;base64, " + Convert.ToBase64String((byte[]) Eval("Picture")) %>' 
                        Width="193px" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
    </EmptyDataTemplate>
</asp:GridView>

C#

public partial class Shirts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + Server.MapPath("~\\App_Data\\Products.mdb"));

        con.Open();

        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandText = "select * from Shirts";

        cmd.Connection = con;

        OleDbDataAdapter Adaptor = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        Adaptor.Fill(ds);

        GridView1.DataSource = ds;
        GridView1.DataBind();

        con.Close();
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
  //      Session["Title"] = "Label7.Text";
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
      //  Response.Redirect("Cart.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      //  Response.Redirect("Cart.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
           Response.Redirect("Cart.aspx");
    }
}

请帮我按下按钮 在此先感谢

2 个答案:

答案 0 :(得分:0)

您无法为此Gridview控件中的控件创建事件处理程序,因为它们不是静态的,而是针对Gridview的数据源中的每个项重复。

当在Gridview中单击按钮时,会引发RowCommand事件,因此您需要通过识别您的控件来编写此事件中的逻辑,如下所示: -

要在Gridview RowCommand事件中单独标识每个控件,您需要添加CommandName&amp;您控件的CommandArgument属性: -

<asp:ImageButton ID="ImageButton1" runat="server" Height="51px" ImageUrl="~/Images/orange_addtocart-trans.png" Width="159px" OnCommand="Foo" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>

此处, CommandName 用于标识引发事件的控件, CommandArgument 用于标识当前行。

最后,您可以在后面的代码中阅读它们: -

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName == "Foo")
  {

    int index = Convert.ToInt32(e.CommandArgument);

    GridViewRow row = GridView1.Rows[index];

    // Add you logic here
  }
}

答案 1 :(得分:0)

我按照您提供的示例,我设置了commandname和commandargument属性,并在Rowcommand事件中尝试相同的步骤,例如但是我又得到了错误

'/ E Shop'应用程序中的服务器错误。

无效的回发或回调参数。使用配置或&lt;%@ Page EnableEventValidation =“true”%&gt;启用事件验证在一个页面中。出于安全考虑,此功能可验证回发或回调事件的参数是否来自最初呈现它们的服务器控件。如果数据有效且符合预期,请使用ClientScriptManager.RegisterForEventValidation方法注册回发或回调数据以进行验证。

来源:

<asp:ImageButton ID="ImageButton1" runat="server" Height="51px" 
                                                         commandname="Foo" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
                                                            ImageUrl="~/Images/orange_addtocart-trans.png" Width="159px" 
                                                            onclick="ImageButton1_Click2"/>

C#

public partial class raff : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + Server.MapPath("~\\App_Data\\Products.mdb"));

        con.Open();

        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandText = "select * from Shirts";

        cmd.Connection = con;

        OleDbDataAdapter Adaptor = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        Adaptor.Fill(ds);

        GridView1.DataSource = ds;
        GridView1.DataBind();

        con.Close();
    }


    protected void GridView1_RowCommand(object sender,
    GridViewCommandEventArgs e)
    {
        if (e.CommandName == "AddToCart")
        {
            // Retrieve the row index stored in the 
            // CommandArgument property.
            int index = Convert.ToInt32(e.CommandArgument);

            // Retrieve the row that contains the button 
            // from the Rows collection.
            GridViewRow row = GridView1.Rows[index];

            // Add code here to add the item to the shopping cart.
            Response.Redirect("Cart.aspx");
        }

    }
    



    
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
  //      Session["Title"] = "Label7.Text";
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
      //  Response.Redirect("Cart.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      //  Response.Redirect("Cart.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
         //  Response.Redirect("Cart.aspx");
    }


    protected void Button1_Click1(object sender, EventArgs e)
    {
        Response.Redirect("Cart.aspx");
    }