如何在Gridview中绑定图像动态查看所有单元格&从单元格获取值 [英] How to Bind Images in Gridview all cells dynamically & get values from cell

查看:68
本文介绍了如何在Gridview中绑定图像动态查看所有单元格&从单元格获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须构建一个应用程序,其中我必须在gridview单元中动态附加图像,然后单击图像后我将获得有关图像的详细信息

例如,我要创建一个板球评级网站,在该网站上可以看到排名前10的板球运动员的图像,所以假设我有100名板球运动员,那么前10个板球运动员将可见,并根据评级动态选择前10名板球运动员.然后在单击要显示的图像后有关击球手的详细信息,例如奔跑,罢工等

i have to build an application in which i have to attach images dynamically in gridview cell and after clicking on the image i shud get the detail about the image

ex,i m creating a cricket ratings website where images of top 10 batsmen will be visible ,so suppose i have 100 batsmen then first 10 will be visible and depending upon rating top 10 will be choosen dynamically.then after clicking on image i want to show details about the batsmen eg runs,strikerate etc

推荐答案

为此,您需要这样做.

首先将100个击球手的图像的路径存储到数据库中.
这样.

For this you need to do like this.

First of all store the path of the images of 100 batsmen to the database.
like this.

CREATE TABLE IMAGESOURCE (
 ID INT IDENTITY(1,1) PRIMARY KEY,
 IMAGESRC NVARCHAR(50),
 RANKING INT
)


并像这样存储imagesrc ....


and store the imagesrc like this....

batsmen1.jpg
batsmen2.jpg
batsmen3.jpg
batsmen4.jpg
....
batsmen100.jpg



现在,当您像这样将数据绑定到网格时...



now while you bind the data to your grid like this...

protected void databind()
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString.ToString());
    SqlDataAdapter da = new SqlDataAdapter();
    con.Open();
    da.SelectCommand = new SqlCommand();
    da.SelectCommand.Connection = con;
    da.SelectCommand.CommandType = CommandType.Text;
    da.SelectCommand.CommandText = "SELECT TOP 10 IMAGESRC FROM IMAGESOURCE ORDER BY RANKING DESC";
    DataSet ds = new DataSet();
    da.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
}



现在为gridview创建rowdatabound事件.



now create a rowdatabound event for gridview.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    ImageButton imgProd = (ImageButton)e.Row.FindControl("imgBetsmen");
    if (imgProd != null)
    {
        imgProd.ImageUrl = "~/Images/Betsmen/" + ((System.Data.DataRowView)e.Row.DataItem)["IMAGESRC"].ToString();
        //now you can write your own code to display the detail
        //on that image button click with onclick and postbackurl property of your image button
    }
}



这里的imgBetsmen控件是放置在网格视图项目模板内的图像按钮,用于将要塞图像显示到网格...
像这样..



here the control imgBetsmen is the image button placed inside the grid view item template to display the betsmen images to grid...
like this..

<asp:gridview id="GridView1" runat="server" autogeneratecolumns="false" onrowdatabound="GridView1_RowDataBound" xmlns:asp="#unknown">
<columns>
    <asp:templatefield>
        <itemtemplate>
            <asp:imagebutton id="imgBetsmen" runat="server" />
        </itemtemplate>
    </asp:templatefield>
</columns>
</asp:gridview>


这篇关于如何在Gridview中绑定图像动态查看所有单元格&amp;从单元格获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆