当前位置:编程学习 > C#/ASP.NET >>

GridView图片显示——“System.EventArgs”不包含“RowIndex”的定义

GridView1.Rows[e.RowIndex].Cells[1].Text.ToString() “System.EventArgs”不包含“RowIndex”的定义,并且找不到可接受类型为“System.EventArgs”的第一个参数的扩展方法“RowIndex”(是否缺少 using 指令或程序集引用?)


代码如下:

first-new.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/mompage/MasterPage-frist.master" AutoEventWireup="true" CodeFile="first-new.aspx.cs" Inherits="page_first_new" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
    .style10
    {
        width: 362px;
            text-align: left;
        }
        .style11
        {
            width: 368px;
            height: 8px;
        }
        .style12
        {
            width: 50%;
            height: 8px;
        }
        .style13
        {
            width: 362px;
            height: 95px;
            font-family: 楷体;
            font-size: xx-large;
            text-align: left;
        }
        .style14
        {
            text-align: left;
        }
        .style15
        {
            width: 17px;
            height: 95px;
            font-family: 楷体;
            text-align: left;
            font-size: xx-large;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" runat="server" 
    contentplaceholderid="ContentPlaceHolder1">
    <table style="width: 100%; height: 533px; background-image: url('../image/052.jpg'); background-repeat: no-repeat;" 
        align="right">
                <tr>
                    <td class="style5">
                        <table style="width: 100%; height: 511px; font-size: large; color: #FFFFFF;">
                            <tr>
                                <td class="style13" 
                                    style="border-right-style: inset; border-left-width: medium">
                                    picture<br />
                                    </td>
                                <td class="style15" width="50%">
                                    Diary</td>
                            </tr>
                            <tr>
                                <td class="style10" 
                                    style="border-right-style: inset; border-left-width: medium">

                                    <br />

                                    <asp:ListView ID="ListView1" runat="server">
                                        <ItemTemplate>
                                            <asp:Image ID="img" runat="server" Height="300" Width="400" />
                                        </ItemTemplate>
                                    </asp:ListView>

                                </td>
                                <td class="style14" width="50%">
                                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                        ConnectionString="<%$ ConnectionStrings:haliuConnectionString %>" 
                                        SelectCommand="SELECT [Dianame] FROM [Diainfo] ORDER BY [Diatime] DESC">
                                    </asp:SqlDataSource>
                                    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                                        AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Width="459px" 
                                        Height="461px" PageSize="12">
                                        <Columns>
                                            <asp:BoundField DataField="Dianame" HeaderText="日志" 
                                                SortExpression="Dianame" />
                                        </Columns>
                                    </asp:GridView>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                </table>
        
        </asp:Content>



first-new.cs

using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Data.Sql;

using System.IO;
using System.Collections;
using System.Data.SqlClient;
using System.Configuration;


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

        bindinfo();
        bindpic();

    }
    private void bindinfo()
    {
        DataAccess da = new DataAccess();
        string sql = "SELECT * FROM picinfo";
        DataSet ds = da.dataSet(sql);
        ListView1.DataSource = ds.Tables[0];
        //  GridView1.DataSource = ds.Tables[0];
        //  GridView1.DataKeyNames = new string[] { "picid" };
        ListView1.DataKeyNames = new string[] { "picid" };
        //GridView1.DataBind();
        ListView1.DataBind();
    }

    private void bindpic()
    {
        // foreach (GridViewRow gr in GridView1.Rows)
        foreach (ListViewDataItem gr in ListView1.Items)//获取每一项图片
        {

            //   Image image = (Image)gr.Cells[0].Controls[0].FindControl("img");
            Image image = (Image)gr.Controls[0].FindControl("img");
            string id = ListView1.DataKeys[gr.DataItemIndex].Value.ToString();
            image.ImageUrl = "./page/pic.aspx?picid=" + id;

        }
        // bindinfo();
    }
   
}
谢谢 --------------------编程问答-------------------- 事件绑定错了,参数e没有你要访问的属性 --------------------编程问答--------------------
引用 1 楼 bdmh 的回复:
事件绑定错了,参数e没有你要访问的属性

要如何绑定? --------------------编程问答-------------------- 谁知道你绑定的啥 --------------------编程问答-------------------- 为GridView添加RowDataBound事件,把那个出错的代码写到该事件方法里去。 --------------------编程问答--------------------
引用 4 楼 dalmeeme 的回复:
为GridView添加RowDataBound事件,把那个出错的代码写到该事件方法里去。


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridView1.Rows[e.RowIndex].Cells[1].Text.ToString();
    }
这样么?
不行啊,报错:错误 2 “System.Web.UI.WebControls.GridViewRowEventArgs”不包含“RowIndex”的定义,并且找不到可接受类型为“System.Web.UI.WebControls.GridViewRowEventArgs”的第一个参数的扩展方法“RowIndex”(是否缺少 using 指令或程序集引用?) D:\web\page\first-new.aspx.cs 56 26 D:\web\
--------------------编程问答-------------------- 直接写:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       e.Row.Cells[1].Text.ToString();
    } --------------------编程问答-------------------- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       Response.Write(e.Row.Cells[1].Text+"<br/>");
    } --------------------编程问答--------------------
引用 7 楼 dalmeeme 的回复:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       Response.Write(e.Row.Cells[1].Text+"<br/>");
    }


两个都试了。。没变化。。我是在bindinfo()那断点的,提示上面错误。
图片仍不能显示。。
方便的话能加下我QQ么?522020653 --------------------编程问答-------------------- 我上面这个只是帮你通过编译,跟图片能不能显示无关。你最好找本书系统的学下吧。 --------------------编程问答--------------------
引用 9 楼 dalmeeme 的回复:
我上面这个只是帮你通过编译,跟图片能不能显示无关。你最好找本书系统的学下吧。


不是这个原因我实在想不通是为什么图片不能显示了,如果新建一个空白文档,把ListView复制进去,cs代码再复制进去,图片是可以显示的。 --------------------编程问答-------------------- first-new.cs
的代码里没有看到GridView1.Rows[e.RowIndex].Cells[1].Text.ToString()这一句,您是不是贴错代码了? --------------------编程问答--------------------
引用 11 楼 lyq8376 的回复:
first-new.cs
的代码里没有看到GridView1.Rows[e.RowIndex].Cells[1].Text.ToString()这一句,您是不是贴错代码了?


没有少贴,,刚发现把网页移到网站的根目录下,图片就能显示了,在文件夹里就不行
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,