在DATAGRID的模板中加入图片控件的问题,急! >>>然后在CS文件中给控件赋值为图片的路径you need to do it in ItemDataBound event handler or after DataBind, do it to each DataGridItem, show your code 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <ASP:DATAGRID id="MyDataGrid" ShowHeader="False" AutoGenerateColumns="False" width="100%" AllowPaging="True" PageSize="6" Runat="server"> <Columns> <asp:TemplateColumn> <ItemTemplate> <Table width="100%" border="0" CellPadding="0" CellSpacing="0" height="12"> <tr> <td width="12%" valign="top"><a href='/site/movie?ID=<%#DataBinder.Eval(Container.DataItem,"Video_ID")%>' > <asp:Image ID="PhotoImg" Runat="server" ImageUrl="../../images/btn_go.gif"></asp:Image</a></td> <td width="88%" Align="right" valign="top"> <Table width="99%" Border="0" cellspacing="0" cellpadding="2"> <tr VAlign="top"> <td width="100%" colspan="4"><a href='/site/movie?ID=<%#DataBinder.Eval(Container.DataItem,"Video_ID")%>'><font color="#669900"><b><%#DataBinder.Eval(Container.DataItem,"Video_CN_Name")%></b></font></a><a href="../movie/index.asp"><font color="#669900"></font></a></td> </tr> <tr VAlign="top"> <td width="8%" height="15" bgColor="#f5f5ef" Class="font12">主演:</td> <td width="42%" bgColor="#f5f5ef" Class="font12"><%#DataBinder.Eval(Container.DataItem,"Video_Player")%></td> <td width="8%" bgColor="#f5f5ef" Class="font12">語言:</td> <td width="42%" bgColor="#f5f5ef" Class="font12"><%#DataBinder.Eval(Container.DataItem,"Video_Language")%></td> </tr> <tr> <td Class="font12">類別:</td> <td Class="font12"></td> <td Class="font12">公司:</td> <td Class="font12"><%#DataBinder.Eval(Container.DataItem,"Video_Corp")%></td> </tr> <tr VAlign="top"> <td bgColor="#f5f5ef" Class="font12">格式:</td> <td bgColor="#f5f5ef" Class="font12"><img src='/images/icon/file_<%#DataBinder.Eval(Container.DataItem,"Format_Name")%>.gif' align="absMiddle" ></td> <td bgColor="#f5f5ef" Class="font12"> </td> <td bgColor="#f5f5ef" Class="font12"> </td> </tr> <tr VAlign="top"> <td ColSpan="4"><%#DataBinder.Eval(Container.DataItem,"Video_Intro")%></td> </tr> </Table> </td> </tr> </Table> <table width="98%" border="0" cellpadding="0" cellspacing="0"> <tr> <td height="8"></td> </tr> <tr> <td height="1" background="/images/bg_hdot.gif"></td> </tr> <tr> <td height="8"></td> </tr> </table> </ItemTemplate> </asp:TemplateColumn> </Columns> <PagerStyle Visible="False" /></ASP:DATAGRID> CS文件中是这样写的:public void Page_Load(object sender, EventArgs e) {myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["Conn"]);DataSet ds = BindGrid();imgName = "/photo/MoviePhoto/";imgName += ds.Tables[0].Rows[0]["Video_Serial"].ToString().Substring(0,2);imgName += "/" + ds.Tables[0].Rows[0]["Video_DirYear"].ToString();imgName += "/" + ds.Tables[0].Rows[0]["Video_DirMonth"].ToString();imgName += "/" + ds.Tables[0].Rows[0]["Video_Serial"].ToString();imgName += "/" + ds.Tables[0].Rows[0]["Video_Image_Name"].ToString();PhotoImg.ImageUrl=imgName.ToString();if(Page.IsPostBack){this.btnGo.Click += new System.EventHandler(this.GoPageClick);}MyDataGrid.DataSource=ds.Tables["Video_VIEW"].DefaultView;MyDataGrid.DataBind();} MyDataGrid.DataSource=ds.Tables["Video_VIEW"].DefaultView;MyDataGrid.DataBind();int i=0;foreach (DataGridItem dgi in MyDataGrid.Items){ System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)dgi.FindControl("PhotoImg"); imgName = "/photo/MoviePhoto/"; imgName += ds.Tables[0].Rows[i]["Video_Serial"].ToString().Substring(0,2); imgName += "/" + ds.Tables[0].Rows[i]["Video_DirYear"].ToString(); imgName += "/" + ds.Tables[0].Rows[i]["Video_DirMonth"].ToString(); imgName += "/" + ds.Tables[0].Rows[i]["Video_Serial"].ToString(); imgName += "/" + ds.Tables[0].Rows[i]["Video_Image_Name"].ToString(); img.ImageUrl=imgName.ToString(); i++;} <asp:Image ID="PhotoImg" Runat="server" ImageUrl="../../images/btn_go.gif"></asp:Image</a>...</asp:Image>... TO:saucer(思归, .NET MVP) 还是一样的错误:System.NullReferenceException: 未将对象引用设置到对象的实例。 TO:saucer(思归, .NET MVP) 不好意思,是我搞错了.可以了,非常感谢!PS:如果我想对从DATASET中取出的字符串进行截取,该如何写呢?我试过这样:DataBinder.Eval(Container.DataItem,"Video_Player").Substring(0,200)是错的. string s = DataBinder.Eval(Container.DataItem,"Video_Player").ToString();if (s.Length > 200) s = s.Substring(0,200);orDataBinder.Eval(Container.DataItem,"Video_Player").ToString().Length > 200 ? DataBinder.Eval(Container.DataItem,"Video_Player").ToString().Substring(0,200) : DataBinder.Eval(Container.DataItem,"Video_Player").ToString() TO:saucer(思归, .NET MVP)能否解释一下foreach这个循环中内容的意思呢?为什么这样取出的值可以在DATAGRID的对应记录中显示正确的图片地址? 求教:如何将一个页面中的textbox的值,显示在另一个页面的textbox(或lable)中? asp.net用户控件利弊 如何对2个文本文件的比较? 怎么添加一个Null到数据库里面?? 关于设定属性的问题(急,在线,高分) ASP和ASP.NET有什么区别啊! 帮助~ 怎么将VS.net2003带的水晶报表升级到10版 变量定义控件名 asp.net(C#)中如何调用dll 急用:谁有asp.net的图片上传程序,给我发一个。asp的能不能转换成.net的呀? 一个简单的数据库插入问题~!~
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<Table width="100%" border="0" CellPadding="0" CellSpacing="0" height="12">
<tr>
<td width="12%" valign="top"><a href='/site/movie?ID=<%#DataBinder.Eval(Container.DataItem,"Video_ID")%>' >
<asp:Image ID="PhotoImg" Runat="server" ImageUrl="../../images/btn_go.gif"></asp:Image</a></td>
<td width="88%" Align="right" valign="top">
<Table width="99%" Border="0" cellspacing="0" cellpadding="2">
<tr VAlign="top">
<td width="100%" colspan="4"><a href='/site/movie?ID=<%#DataBinder.Eval(Container.DataItem,"Video_ID")%>'><font color="#669900"><b><%#DataBinder.Eval(Container.DataItem,"Video_CN_Name")%></b></font></a><a href="../movie/index.asp"><font color="#669900"></font></a></td>
</tr>
<tr VAlign="top">
<td width="8%" height="15" bgColor="#f5f5ef" Class="font12">主演:</td>
<td width="42%" bgColor="#f5f5ef" Class="font12"><%#DataBinder.Eval(Container.DataItem,"Video_Player")%></td>
<td width="8%" bgColor="#f5f5ef" Class="font12">語言:</td>
<td width="42%" bgColor="#f5f5ef" Class="font12"><%#DataBinder.Eval(Container.DataItem,"Video_Language")%></td>
</tr>
<tr>
<td Class="font12">類別:</td>
<td Class="font12"></td>
<td Class="font12">公司:</td>
<td Class="font12"><%#DataBinder.Eval(Container.DataItem,"Video_Corp")%></td>
</tr>
<tr VAlign="top">
<td bgColor="#f5f5ef" Class="font12">格式:</td>
<td bgColor="#f5f5ef" Class="font12"><img src='/images/icon/file_<%#DataBinder.Eval(Container.DataItem,"Format_Name")%>.gif' align="absMiddle" ></td>
<td bgColor="#f5f5ef" Class="font12"> </td>
<td bgColor="#f5f5ef" Class="font12"> </td>
</tr>
<tr VAlign="top">
<td ColSpan="4"><%#DataBinder.Eval(Container.DataItem,"Video_Intro")%></td>
</tr>
</Table>
</td>
</tr>
</Table>
<table width="98%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="8"></td>
</tr>
<tr>
<td height="1" background="/images/bg_hdot.gif"></td>
</tr>
<tr>
<td height="8"></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle Visible="False" />
</ASP:DATAGRID>
public void Page_Load(object sender, EventArgs e)
{
myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["Conn"]);
DataSet ds = BindGrid();
imgName = "/photo/MoviePhoto/";
imgName += ds.Tables[0].Rows[0]["Video_Serial"].ToString().Substring(0,2);
imgName += "/" + ds.Tables[0].Rows[0]["Video_DirYear"].ToString();
imgName += "/" + ds.Tables[0].Rows[0]["Video_DirMonth"].ToString();
imgName += "/" + ds.Tables[0].Rows[0]["Video_Serial"].ToString();
imgName += "/" + ds.Tables[0].Rows[0]["Video_Image_Name"].ToString();PhotoImg.ImageUrl=imgName.ToString();if(Page.IsPostBack)
{
this.btnGo.Click += new System.EventHandler(this.GoPageClick);
}
MyDataGrid.DataSource=ds.Tables["Video_VIEW"].DefaultView;
MyDataGrid.DataBind();
}
MyDataGrid.DataSource=ds.Tables["Video_VIEW"].DefaultView;
MyDataGrid.DataBind();int i=0;
foreach (DataGridItem dgi in MyDataGrid.Items)
{
System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)dgi.FindControl("PhotoImg");
imgName = "/photo/MoviePhoto/";
imgName += ds.Tables[0].Rows[i]["Video_Serial"].ToString().Substring(0,2);
imgName += "/" + ds.Tables[0].Rows[i]["Video_DirYear"].ToString();
imgName += "/" + ds.Tables[0].Rows[i]["Video_DirMonth"].ToString();
imgName += "/" + ds.Tables[0].Rows[i]["Video_Serial"].ToString();
imgName += "/" + ds.Tables[0].Rows[i]["Video_Image_Name"].ToString(); img.ImageUrl=imgName.ToString();
i++;
}
还是一样的错误:
System.NullReferenceException: 未将对象引用设置到对象的实例。
我试过这样:DataBinder.Eval(Container.DataItem,"Video_Player").Substring(0,200)是错的.
if (s.Length > 200)
s = s.Substring(0,200);orDataBinder.Eval(Container.DataItem,"Video_Player").ToString().Length > 200 ? DataBinder.Eval(Container.DataItem,"Video_Player").ToString().Substring(0,200) : DataBinder.Eval(Container.DataItem,"Video_Player").ToString()