如何在gridview中设置图片网址 [英] how to set image url in gridview
本文介绍了如何在gridview中设置图片网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码:
< < span class =code-leadattribute> asp:TemplateField HeaderText = 附件 ItemStyle-VerticalAlign = 中 ItemStyle-HorizontalAlign = 中心 >
< ItemTemplate >
< asp:LinkBu tton runat = server ID = likattachment CommandName = download CommandArgument =' <% #Eval( 附件)%> ' >
< img src = ../ icon / paper-clip.png style = border:0px; < span class =code-attribute> id = image1 / >
< span class =code-keyword>< / asp:LinkButton >
< / ItemTemplate >
< ItemStyle HorizontalAlign = 中心 VerticalAlign = 中间 > < / ItemStyle >
< ; / asp:TemplateField
和关于rowcommand事件我使用以下代码:
protected void GridView1_RowCommand( object sender,GridViewCommandEventArgs e)
{
try
{
LinkButton lik =(LinkButton)e.CommandSource;
if (lik.CommandName == 下载)
{
string filename = + e.CommandArgument;
if (!string.IsNullOrEmpty(filename))
{
Response.Clear();
Response.AddHeader( Content-Disposition, attachment; filename = + filename);
// Response.AddHeader(Content-Length,file.Length.ToString());
Response.ContentType = plain / text;
Response.TransmitFile(Server.MapPath( 〜/ upload / + filename)) ;
}
else
{
Response.Write( < script> alert('No Attachment to download');< / script>);
}
}
}
catch (例外情况)
{
}
}
现在我想显示那些记录的默认图像图标在附件中没有图像..还有附件显示在列表中但带有不同图标的记录..
我认为它将由gridview的RowDataBound事件完成我试过但我的问题没有解决
Plz建议我该怎么做..
提前谢谢..
解决方案
试试下面的代码...
- 将
OnRowDataBound =GridView1_RowDataBound
添加到GridView
aspx
Page。- 在 Code Behind 页面中定义事件。
受保护 void GridView1_RowDataBound( object 发件人,Gri dViewRowEventArgs e)
{
LinkButton lnkAttachment =(LinkButton)e.Row.FindControl( likattachment 跨度>);
if (!string.IsNullOrEmpty(lnkAttachment.CommandArgument.ToString())
{
// 附件存在,做点什么。
}
else
{
// 无附件,显示图标
}
}
其他选项可以使用三元运算符,类似
ImageUrl ='<%#Eval(attachment)!= null?〜/ Images / Attachment.gif:〜/ Images / NoAttachment.gif%>'
I have use following code:
<asp:TemplateField HeaderText="Attachment" ItemStyle-VerticalAlign="Middle" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton runat="server" ID="likattachment" CommandName="download" CommandArgument='<%#Eval("attachment") %>'>
<img src="../icon/paper-clip.png" style="border:0px;" id="image1" />
</asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
</asp:TemplateField
and on rowcommand event I have use following code:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
LinkButton lik = (LinkButton)e.CommandSource;
if (lik.CommandName == "download")
{
string filename = "" + e.CommandArgument;
if (!string.IsNullOrEmpty(filename))
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
// Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "plain/text";
Response.TransmitFile(Server.MapPath("~/upload/" + filename));
}
else
{
Response.Write("<script>alert('No Attachment to download');</script>");
}
}
}
catch(Exception ex)
{
}
}
Now I want to display default image icon for those records which has no image in attachment.. Also records with attachment display in list but with different icon..
I think it will done by RowDataBound event of gridview I tried but my problem is not solved
Plz suggest me what should do..
Thanks in advance..
解决方案
Try the below code...
- Add
OnRowDataBound="GridView1_RowDataBound"
toGridView
Markup inaspx
Page.- Define the Event in Code Behind page.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { LinkButton lnkAttachment = (LinkButton)e.Row.FindControl("likattachment"); if (!string.IsNullOrEmpty(lnkAttachment.CommandArgument.ToString()) { // Attachment exists, do something. } else { // No Attachments, show icon } }
Other option can be using Ternary Operator, Something like
ImageUrl='<%# Eval("attachment")!=null ? "~/Images/Attachment.gif" : "~/Images/NoAttachment.gif"%>'
这篇关于如何在gridview中设置图片网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文