>我在下面有这段代码,这是我调用 REST Web 服务来填充我的网格视图。我目前正在尝试完成的事情是使用我从 api 获得的image
URL 将图像显示在我的网格视图上,而不仅仅是链接。有没有简单的方法可以做到这一点?任何帮助将不胜感激,谢谢!
public partial class Book : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public class Result
{
public string title { get; set; }
public string subtitle { get; set; }
public string price { get; set; }
public string image { get; set; }
public string url { get; set; }
}
public class SearchList
{
public int resultCount;
public Result[] books;
}
protected void Button1_Click(object sender, EventArgs e)
{
string searchTerm = TextBox1.Text;
var webRequest = (HttpWebRequest)WebRequest.Create
("https://api.itbook.store/1.0/search/" + Server.UrlEncode(searchTerm));
var webResponse = (HttpWebResponse)webRequest.GetResponse();
if (webResponse.StatusCode == HttpStatusCode.OK)
{
JavaScriptSerializer json = new JavaScriptSerializer();
StreamReader sr = new StreamReader(webResponse.GetResponseStream());
string resString = sr.ReadToEnd();
SearchList list = json.Deserialize<SearchList>(resString);
GridView1.DataSource = list.books;
GridView1.DataBind();
}
else
Label1.Text = "Invalid Response";
}
}
因为您要将类列表绑定到 GridView。我建议使用带有ItemTemplate
的Stronly Typed GridView。必须确保 ItemType
属性指向您的类。
<asp:GridView ID="GridView1" runat="server" ItemType="YourNameSpace.Result">
<Columns>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<%# Item.title %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<img src="<%# Item.url %>" />
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Item.url %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>