我尝试从按钮点击事件设置图像源URL,但出现错误" EventArgs不包含行图像gridview的定义"
Gridview代码:
<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
答案 0 :(得分:0)
将模板更改为
- (void)layoutSubviews {
[super layoutSubviews];
NSArray *subViewArray = [self subviews];
for (id view in subViewArray) {
if ([view isKindOfClass:(NSClassFromString(@"_UIToolbarContentView"))]) {
UIView *testView = view;
testView.userInteractionEnabled = NO;
}
}
}
代码背后的变化:
<asp:TemplateField HeaderText="Album Art">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Width="174px" Height="174px" ImageUrl='<%# Eval("AlbumArt") %>' />
</ItemTemplate>
</asp:TemplateField>
答案 1 :(得分:0)
问题不在于这一行
img.ImageUrl = image;
但前面的行
System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)e.Row.Cells[2].FindControl("Image1");
您正在使用e.Row.Cells
,但是您在按钮点击中使用此功能,而不是RowDataBound事件,因此e
没有Row
属性。使用
GridView1.Rows[i].Cells[2].FindControl("Image1");