>>>然后在CS文件中给控件赋值为图片的路径you need to do it in ItemDataBound event handler or after DataBind, do it to each DataGridItem, show your code

解决方案 »

  1.   

    <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">&nbsp;</td>
    <td bgColor="#f5f5ef" Class="font12">&nbsp;</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>
      

  2.   

    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();
    }
      

  3.   


    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++;
    }
      

  4.   

    <asp:Image ID="PhotoImg" Runat="server" ImageUrl="../../images/btn_go.gif"></asp:Image</a>...</asp:Image>...
      

  5.   

    TO:saucer(思归, .NET MVP) 
    还是一样的错误:
    System.NullReferenceException: 未将对象引用设置到对象的实例。
      

  6.   

    TO:saucer(思归, .NET MVP) 不好意思,是我搞错了.可以了,非常感谢!PS:如果我想对从DATASET中取出的字符串进行截取,该如何写呢?
    我试过这样:DataBinder.Eval(Container.DataItem,"Video_Player").Substring(0,200)是错的.
      

  7.   

    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()
      

  8.   

    TO:saucer(思归, .NET MVP)能否解释一下foreach这个循环中内容的意思呢?为什么这样取出的值可以在DATAGRID的对应记录中显示正确的图片地址?