0

我有 sql 表Icon(ID(int),ICON_IMAGE(image),hits(int)) 我正在访问 gridview 中的图像。我的 asp 页面代码如下所示

 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
     AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
 <Columns>
     <asp:TemplateField HeaderText="Image">
  <ItemTemplate>
 <asp:Image ID="Image1" runat="server" ImageUrl='<%# "imagehandler.ashx?ID=" + Eval("ID")%>'/>
 </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ICOBANKConnectionString %>" 
    SelectCommand="SELECT [ICON_IMAGE], [ID] FROM [Icon]"></asp:SqlDataSource>

imagehandler.ashx 代码是这样的

 SqlConnection con = new SqlConnection();
    con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ICOBANKConnectionString"].ConnectionString;



    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "SELECT  ICON_IMAGE FROM  Icon WHERE (ID = @ID)";
    cmd.CommandType = System.Data.CommandType.Text;
    cmd.Connection = con;

    SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.Int);
    ImageID.Value = context.Request.QueryString["ID"];
    cmd.Parameters.Add(ImageID);
    con.Open();
    SqlDataReader dReader = cmd.ExecuteReader();
    dReader.Read();
    context.Response.BinaryWrite((byte[])dReader["ICON_IMAGE"]);
    dReader.Close();
    con.Close();

它工作正常。现在的问题是我正在尝试获取图像的 id 以将其显示在其他页面上,并在有人单击图像时增加点击次数。我该怎么做呢?

4

1 回答 1

1

这是一个简单的方法:

protected void grdView_RowCommand(object sender, GridViewCommandEventArgs e)

{
    if (e.CommandName == "selectImage")
    {

        int rowIndex = Convert.ToInt32(e.CommandArgument); // Get the current row
        int cellVal = Convert.ToInt32(grdView.Rows[rowIndex].Cells[2].Text);//Get the cell value

        Response.Write(cellVal.ToString());

    }
}
于 2013-10-19T07:05:40.453 回答