如何在运行时在gridview列中添加图像 [英] How to add image in gridview column at runtime

查看:85
本文介绍了如何在运行时在gridview列中添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想在运行时在网格视图中添加图片。我使用以下代码。它的显示错误请求帮助我解决问题。



 <   asp:GridView     ID   =  gvSpecialist    runat   =  server    AutoGenerateColumns   =  false   宽度  =  965px  



onpageindexchanging = gvSpecialist_PageIndexChanging >

< >
< asp:TemplateField HeaderText = 医生姓名 >
< itemtemplate >
< asp:LinkBut​​ton ID = lnkNames runat = server ForeColor = black 文字 =' <%# Eval( 名称%> ' CommandName = 名称 CommandArgument =' <% #Eval( 名称%> ' >
< / itemtemplate >
< itemstyle width = 250px cssclass = gridcol / >
< HeaderStyle Horizo​​ntalAlign = 中心 / >

< asp:TemplateField >
< itemtemplate >
< asp:Image ID = Image1 runat = 服务器 ImageUrl =' <% #BindImage(Eval( online))< span class =code-pagedirective>%> '
ImageAlign = / >

< / itemtemplate >

< / columns >
< footerstyle backcolor = #D4A97F font-bold = True forecolor = 白色 / >
< pagerstyle backcolor = #D4A97F forecolor = 黑色 horizo​​ntalalign = 中心 >
Wrap =FalseFont-Bold =True/>
< SelectedRowStyle < span class =code-attribute> BackColor = #E2DED6 Font-Bold = True ForeColor = #333333 / >
< HeaderStyle BackColor = #d4a97f Font-Bold = True ForeColor = black

高度 = 30 换行 = 错误 宽度 = 75 / >
< editrowstyle < span class =code-attribute> backcolor = #999999 / >
< alternatingrowstyle backcolor = #E2D ED6 forecolor = black / >





代码背后:

  public   string  BindImage( string  online)
{

if (online == Y
{
string imgpath = 〜/ images / Greenflag.png;
return imgpath;
}
else
{
string imgpath = 〜/ images / Redflag.png;
return imgpath;
}

}



但显示错误,因为无法将对象转换为字符串..



请帮助



谢谢

parithi

解决方案

< blockquote>嗨



我认为你必须将参数从object转换为string。



所以你的代码需要改变如下



 < span class =code-keyword><   asp:GridView     ID   =  gvSpecialist    runat   =  server     AutoGenerateColumns   = < span class =code-keyword> false   宽度  =  965px  



onpageindexchanging = gvSpecialist_PageIndexChanging >

< >
< asp:TemplateField HeaderText = 医生姓名 >
< itemtemplate >
< asp:LinkBut​​ton ID = lnkNames runat = server ForeColor = black 文字 =' <% #Eval( 名称%> ' CommandName = 名称 CommandArgument =' <% #Eval( 名称%> ' >
< / itemtemplate >
< itemstyle width = 250px cssclass = gridcol / >
< HeaderStyle Horizo​​ntalAlign = 中心 < span class =code-keyword> / >

< asp:TemplateField < span class =code-keyword>>
< itemtemplate >
< asp:Image ID = Image1 runat = server ImageUrl =' <% #BindImage(Eval( online .ToString()% > ' ImageAlign = / >

< / itemtemplate >

< / columns >
< footerstyle backcolor = #D4A97F font-bold = True forecolor = 白色 / >
< pagerstyle backcolor = #D4A97F forecolor = 黑色 horizo​​ntalalign = 中心 >
Wrap =FalseFont-Bold =True/>







我希望这个会帮助你


 <   asp:TemplateField     HeaderText   = 缩略图图像 >  
< ItemTemplate > < asp:ImageButton ID = imgThumbnail runat = server ImageUrl =' <%#GetThumbnail(Convert.ToString(DataBinder。 Eval(Container.DataItem, ImagePath))) %> ' 宽度 = 60px 高度 = 60px AlternateText = Thumbanail Image 工具提示 =' <%#GetThumbnail(Convert.ToString(DataBinder.Eval) (Container.DataItem, ImagePath)))% > ' / >
< < span class =code-leadattribute> / ItemTemplate >
< / asp:TemplateField >







  public   string  GetThumbnail( string  strpath)
{
if (!string .IsNullOrEmpty(strpath))
{
string str1 = http://图像文件夹的虚拟路径/ > + / + strpath。的ToString();

return str1;
}
其他
{
返回 ;
}
}


使用图像路径绑定图像字段,然后像这样更新路径:



  public   void  dynimg() 
{
SqlCommand cmd1 = 默认(SqlCommand);
SqlConnection conn1 = 默认(SqlConnection);
动态 sql1 = update Rx set url ='images / 1.gif'其中motion = 1;
conn1 = new SqlConnection( 数据源= 174.142.75.238;初始目录= TDS;用户ID = gac;密码= ffffff);
cmd1 = new SqlCommand(sql1,conn1);
conn1.Open();
cmd1.ExecuteNonQuery();
}


Hi all,
i want to add image in grid view at run time. I am using the following code. its showing error pleas help me to solve the problem .

<asp:GridView ID="gvSpecialist" runat="server" AutoGenerateColumns="false" Width="965px"

     

onpageindexchanging="gvSpecialist_PageIndexChanging" >
        
        <columns>
        <asp:TemplateField HeaderText="Doctor Name">
            <itemtemplate>
                <asp:LinkButton ID="lnkNames" runat="server" ForeColor="black" Text='<%#Eval("Name")%>' CommandName="Name" CommandArgument='<%# Eval("Name") %>'>
            </itemtemplate>
            <itemstyle width="250px" cssclass="gridcol" />
            <HeaderStyle  HorizontalAlign="Center" />

       <asp:TemplateField  >
           <itemtemplate>
           <asp:Image ID="Image1" runat="server"  ImageUrl='<%# BindImage(Eval("online")) %>' ImageAlign="Left" />
         
        </itemtemplate>
     
        </columns>
        <footerstyle backcolor="#D4A97F" font-bold="True" forecolor="White" />
        <pagerstyle backcolor="#D4A97F" forecolor="Black" horizontalalign="Center">
            Wrap="False" Font-Bold="True" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#d4a97f" Font-Bold="True" ForeColor="black" 

            Height="30" Wrap="False" Width="75" />
        <editrowstyle backcolor="#999999" />
        <alternatingrowstyle backcolor="#E2DED6" forecolor="black" />



In code behind:

public string BindImage(string online)
{
    
    if (online == "Y")
    {
        string imgpath = "~/images/Greenflag.png";
        return imgpath;
    }
    else
    {
        string imgpath = "~/images/Redflag.png";
        return imgpath;
    }

}


but it showing error as cannot convert object to string..

please help

thanks
parithi

解决方案

Hi

I think you have to do the convert the parameter from object to string.

So your code need to be altered as below

<asp:GridView ID="gvSpecialist" runat="server" AutoGenerateColumns="false" Width="965px"



onpageindexchanging="gvSpecialist_PageIndexChanging" >

        <columns>
        <asp:TemplateField HeaderText="Doctor Name">
            <itemtemplate>
                <asp:LinkButton ID="lnkNames" runat="server" ForeColor="black" Text='<%#Eval("Name")%>' CommandName="Name" CommandArgument='<%# Eval("Name") %>'>
            </itemtemplate>
            <itemstyle width="250px" cssclass="gridcol" />
            <HeaderStyle  HorizontalAlign="Center" />

       <asp:TemplateField  >
           <itemtemplate>
           <asp:Image ID="Image1" runat="server"  ImageUrl='<%# BindImage(Eval("online").ToString()) %>' ImageAlign="Left" />

        </itemtemplate>

        </columns>
        <footerstyle backcolor="#D4A97F" font-bold="True" forecolor="White" />
        <pagerstyle backcolor="#D4A97F" forecolor="Black" horizontalalign="Center">
            Wrap="False" Font-Bold="True" />




I hope this one will help you


<asp:TemplateField HeaderText="Thumbnail Image">
                               <ItemTemplate>                                   <asp:ImageButton ID="imgThumbnail" runat="server" ImageUrl='<%# GetThumbnail(Convert.ToString(DataBinder.Eval(Container.DataItem,"ImagePath"))) %>'       Width="60px" Height="60px" AlternateText="Thumbanail Image " ToolTip='<%# GetThumbnail(Convert.ToString(DataBinder.Eval(Container.DataItem,"ImagePath"))) %>' />
                               </ItemTemplate>
                           </asp:TemplateField>




public string GetThumbnail(string strpath)
    {
        if (!string.IsNullOrEmpty(strpath))
        {
            string str1 = "http://virtual path of images folder/"> + "/" + strpath.ToString();
        
            return str1;
        }
        else
        {
            return "";
        }
    }


Bind image field with the path of image and then update path like this:

public void dynimg()
{
    SqlCommand cmd1 = default(SqlCommand);
    SqlConnection conn1 = default(SqlConnection);
    dynamic sql1 = "update Rx set url='images/1.gif' where motion=1";
    conn1 = new SqlConnection("Data Source=174.142.75.238;Initial Catalog=TDS;User ID=gac;Password=ffffff");
    cmd1 = new SqlCommand(sql1, conn1);
    conn1.Open();
    cmd1.ExecuteNonQuery();
}


这篇关于如何在运行时在gridview列中添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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