在带有find控件属性的gridview asp.net中进行图像绑定 [英] Image Binding in gridview asp.net with find control property

查看:171
本文介绍了在带有find控件属性的gridview asp.net中进行图像绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从按钮点击事件中设置图片源URL,但给出错误EventArgs不包含定义的行图像gridview

Gridview代码:

p>

 < asp:GridView ID =GridView1runat =serverAutoGenerateColumns =FalseCellPadding =4ForeColor = #333333GridLines =NoneHeight =222pxWidth =859pxstyle =text-align:center;> 
< AlternatingRowStyle BackColor =WhiteForeColor =#284775/>
<列>
< asp:BoundField HeaderText =ArtistDatafield =Artist/>
< asp:BoundField HeaderText =AlbumDatafield =Album/>
< asp:BoundField HeaderText =PlaycountDatafield =Playcount/>

< asp:TemplateField HeaderText =专辑封面>
< ItemTemplate>
< asp:Image ID =Image1runat =serverwidth =174pxheight =174px/>
< / ItemTemplate>
< / asp:TemplateField>

< /列>
< EditRowStyle BackColor =#999999/>
< FooterStyle BackColor =#5D7B9DFont-Bold =TrueForeColor =White/>
< HeaderStyle BackColor =#5D7B9DFont-Bold =TrueForeColor =White/>
< RowStyle BackColor =#F7F6F3ForeColor =#333333/>
< SelectedRowStyle BackColor =#E2DED6Font-Bold =TrueForeColor =#333333/>
< SortedAscendingCellStyle BackColor =#E9E7E2/>
< SortedAscendingHeaderStyle BackColor =#506C8C/>
< SortedDescendingCellStyle BackColor =#FFFDF8/>
< SortedDescendingHeaderStyle BackColor =#6F8DAE/>
< / asp:GridView>

从json绑定:

  protected void btnMusic_Submit_Click(object sender,EventArgs e)
{

if(txtMusic.Text!= null&&txtMusic.Text!=)
{
string artist = txtMusic.Text;
var requestUrl =http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&artist=+ artist +& api_key = []& format = json;
var client = new WebClient();
client.Headers.Add(user-agent,Mozilla / 5.0(Windows NT 6.1; Win64; x64)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 58.0.3029.110 Safari / 537.36);
var response = client.DownloadString(requestUrl);
response = response.Replace(@#text,text);
dynamic stuff = JObject.Parse(response);
DataTable dt = new DataTable();
dt.Columns.Add(Artist,typeof(string));
dt.Columns.Add(Album,typeof(string));
dt.Columns.Add(Playcount,typeof(string));
dt.Columns.Add(AlbumArt,typeof(string));
for(int i = 0; i <5; i ++)
{
lblInfo.InnerText = artist;
name =(stuff.topalbums.album [i] .name.ToString());
playcount = stuff.topalbums.album [i] .playcount.ToString();
image = stuff.topalbums.album [i] .image [2] .text.ToString();
System.Web.UI.WebControls.Image img =(System.Web.UI.WebControls.Image)e.Row.Cells [2] .FindControl(Image1);
img.ImageUrl =图片;

dt.Rows.Add(artist,name,playcount,image);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
lblInfo.InnerText =请输入艺术家姓名;
}

这里是代码抛出错误:

  System.Web.UI.WebControls.Image img =(System.Web.UI.WebControls.Image)e.Row.Cells [2] .FindControl(Image1 ); 
img.ImageUrl =图片; //这是错误来源


解决方案

问题不在于这一行

  img.ImageUrl = image; 

但前行

  System.Web.UI.WebControls.Image img =(System.Web.UI.WebControls.Image)e.Row.Cells [2] .FindControl(Image1); 

您正在使用 e.Row.Cells ,但是您在Button Click中使用它,而不是RowDataBound事件,因此 e 没有 Row 属性。使用

  GridView1.Rows [i] .Cells [2] .FindControl(Image1); 


I'm trying to set image source URL from button click event but gives error "EventArgs does not contain definition for row image gridview"

Gridview Code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Height="222px" Width="859px"  style="text-align:center;">
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                        <Columns>
                            <asp:BoundField HeaderText="Artist" Datafield="Artist"/>
                            <asp:BoundField HeaderText="Album" Datafield="Album"/>
                            <asp:BoundField HeaderText="Playcount" Datafield="Playcount"/>

                            <asp:TemplateField HeaderText="Album Art">
                                <ItemTemplate>
                                    <asp:Image ID="Image1" runat="server" width="174px" height="174px"/>
                                </ItemTemplate>
                            </asp:TemplateField>

                        </Columns>
                        <EditRowStyle BackColor="#999999" />
                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#E9E7E2" />
                        <SortedAscendingHeaderStyle BackColor="#506C8C" />
                        <SortedDescendingCellStyle BackColor="#FFFDF8" />
                        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

Binding from json:

protected void btnMusic_Submit_Click(object sender, EventArgs e)
    {

        if (txtMusic.Text != null && txtMusic.Text != "")
        {
            string artist = txtMusic.Text;
            var requestUrl = "http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&artist=" + artist + "&api_key=[]&format=json";
            var client = new WebClient();
            client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome / 58.0.3029.110 Safari / 537.36");
            var response = client.DownloadString(requestUrl);
            response = response.Replace(@"#text", "text");
            dynamic stuff = JObject.Parse(response);
            DataTable dt = new DataTable();
            dt.Columns.Add("Artist", typeof(string));
            dt.Columns.Add("Album", typeof(string));
            dt.Columns.Add("Playcount", typeof(string));
            dt.Columns.Add("AlbumArt", typeof(string));
            for (int i = 0; i < 5; i++)
            {
                lblInfo.InnerText = artist;
                name = (stuff.topalbums.album[i].name.ToString());
                playcount = stuff.topalbums.album[i].playcount.ToString();
                image = stuff.topalbums.album[i].image[2].text.ToString();
                System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)e.Row.Cells[2].FindControl("Image1");
                img.ImageUrl = image;

                dt.Rows.Add(artist, name, playcount, image);
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        else
            lblInfo.InnerText = "Please Enter an Artist Name";
    }

Here is the code throwing error:

System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)e.Row.Cells[2].FindControl("Image1");
img.ImageUrl = image; // This is where Error Comes

解决方案

The problem is not this line

img.ImageUrl = image;

But the line before

System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)e.Row.Cells[2].FindControl("Image1");

You are using e.Row.Cells, but you are using this in a Button Click, not a RowDataBound event so e does not have a Row property. Use

GridView1.Rows[i].Cells[2].FindControl("Image1");

这篇关于在带有find控件属性的gridview asp.net中进行图像绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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