【发布时间】:2018-04-05 22:53:57
【问题描述】:
我正在尝试从按钮单击事件设置图像源 URL,但出现错误“EventArgs 不包含行图像网格视图的定义”
网格视图代码:
<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>
从 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";
}
这里是代码抛出错误:
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
【问题讨论】:
-
我在您的代码中找不到任何关于
btnMusic_Submit控件的信息,这是一个 Button 控件 ID,它位于何处?如果是真的,我可以确定使用的是 Button 的 EventArgs 而不是 Gridview 之一。 -
是的,它是按钮控件 ID ...它应该在 .aspx 页面中
-
看来我找到了问题的根源,你能澄清一下
txtMusic也属于gridview之外吗?带有 gridview 的输入页面的屏幕截图可能有助于可视化 gridview 内容。 -
另外,名称、播放次数和图像未声明,我可以看到。
-
tetsuya 是的,它属于 gridview 之外
标签: c# asp.net image gridview webforms