2012-08-25 26 views
-1

在具有模板的数据列表中,我有3个用于存储(firstname,scrapps,postedtime)的标签,用于存储配置文件图片的图像和一个用于存储图像的图像,用户将图像作为废品发布。现在我希望这个图像控件仅在用户发布图像时才可见,否则该行不可见。如何将标签控件转换为图像控件?

代码是

 protected void postscrap_Click(object sender, EventArgs e) 
     { 
     SqlConnection con = new SqlConnection(); 
      con.ConnectionString=ConnectionStrings["Con"].ConnectionString; 
      con.Open(); 

    DataTable dt2 = new DataTable(); 


    if (con.State == ConnectionState.Open) 

    { 

    SqlCommand cmd = new SqlCommand(); 

    cmd.CommandText = "Insert into Scraps(Emailscrapto,scrap,posttime) Values('" + Session["UserName"].ToString() + "','" + TextBox2.Text.ToString() + "','" + DateTime.Now.ToString() + "')"; 

     cmd.Connection = con; 

     cmd.CommandType = CommandType.Text; 

     cmd.ExecuteNonQuery(); 

     Response.Redirect("UserHome.aspx"); 

     TextBox2.Text = ""; 

     SqlDataAdapter d1 = new SqlDataAdapter(" Select * from Login l Inner join Scraps s on l.Email = s.Email Where l.Email = '" + Session["UserName"] + "'", con); 

     d1.Fill(dt2); 

     DataList1.DataSource = dt2; 

     DataList1.DataBind(); 

     con.Close(); 
    } 

    if (dt2.Rows.Count > 0) 
    { 
     TextBox2.Text = ""; 
    } 
    protected void postimg_Click(object sender, EventArgs e) 
     { 
    string ToSaveImageName; 
    string sImageFileExtension; 

    if (FileUpload1.PostedFile != null) 
    { 
     //Image postedimage = e.FindControl("postedimage"); 
     //postedimage.Visible = true; 
     string myMap = MapPath("~/").ToLower(); 
     Random r = new Random(); 
     int next = r.Next(); 
     string ImageName = FileUpload1.PostedFile.FileName; 
     sImageFileExtension = ImageName.Substring(ImageName.LastIndexOf(".")).ToLower(); 
     SqlConnection con = new SqlConnection(); 
     con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Con"].ConnectionString; 
     con.Open(); 

     DataTable dt2 = new DataTable(); 

     if (con.State == ConnectionState.Open) 

     { 
      SqlCommand cmd = new SqlCommand(); 
      if (sImageFileExtension == ".gif" || sImageFileExtension == ".png" || sImageFileExtension == ".jpg" || sImageFileExtension == ".jpeg" || sImageFileExtension == ".bmp") 
      { 
       string ImageSaveURL = myMap + "Postedimages/"+next+sImageFileExtension; 
       FileUpload1.PostedFile.SaveAs(ImageSaveURL); 
       cmd.CommandText = "Insert into Scraps(Emailscrapto,scrap,posttime) Values('" + Session["UserName"].ToString() + "','" + next + sImageFileExtension + "','" + DateTime.Now.ToString() + "')"; 
       cmd.Connection = con; 
       cmd.CommandType = CommandType.Text; 
       cmd.ExecuteNonQuery(); 
       Response.Redirect("UserHome.aspx"); 
      } 
     } 
    } 
} 
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item || 
    e.Item.ItemType == ListItemType.AlternatingItem) 
    { if (FileUpload1.HasFile != null) 
     { 
      Image postedimage = (Image)e.Item.FindControl("postedimage"); 
      postedimage.Visible = true; 
     } 
    } 
} 

设计源是这样

<asp:DataList ID="DataList1" runat="server" CellPadding="4" Font-Bold="True" Font-  Italic="False" 
       Font-Names="Verdana" Font-Overline="False" Font-Size="Medium" Font-Strikeout="False" 
       Font-Underline="False" ForeColor="#333333" OnSelectedIndexChanged="DataList1_SelectedIndexChanged" 
       Height="1592px" RepeatLayout="Flow" DataSourceID="SqlDataSource1" OnItemCommand="DataList1_ItemCommand" 
       OnEditCommand="DataList1_EditCommand" OnItemDataBound="DataList1_ItemDataBound"> 
       <AlternatingItemStyle BackColor="White" /> 
       <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
       <FooterTemplate> 
        <asp:TextBox ID="txtcomment" runat="server" Height="29px" Width="188px"></asp:TextBox> 
        &nbsp;<br /> 
        <br /> 
        <asp:Button ID="Insert" runat="server" Text="Insert" OnClick="Insert_Click" /> 
       </FooterTemplate> 
       <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
       <ItemStyle BackColor="#FFFBD6" ForeColor="#333333" /> 
       <ItemTemplate> 
        <table class="style8"> 
         <tr> 
          <td bgcolor="#FFFF99" class="style12"> 
           <asp:Label ID="Label4" runat="server" Text='<%# Eval("FirstName") %>' Font-Bold="True" 
            Font-Italic="True" ForeColor="Red"></asp:Label> 
          </td> 
          <td bgcolor="#FFFF99" class="style11"> 
           <asp:Image ID="Image3" runat="server" Height="108px" ImageUrl='<%# "~/UserImage/" +Eval("profile_pic") %>' 
            Width="98px" /> 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2" bgcolor="#CCFF99"> 
           <asp:Label ID="Label5" runat="server" Text='<%# Eval("scrap") %>' ForeColor="#000066"></asp:Label> 
           &nbsp; 
          </td> 
         </tr> 
         <tr> 
          <td bgcolor="#CCFF99" colspan="2"> 
           <asp:Image ID="postedimage" runat="server" Height="202px" ImageUrl='<%# "~/Postedimages/" +Eval("scrap") %>' 
            Visible="False" Width="262px" /> 
          </td> 
         </tr> 
         <tr> 
          <td class="style13" bgcolor="#FFC1B3"> 
           <asp:LinkButton ID="Edit" runat="server" CommandName="Edit">Edit</asp:LinkButton> 
          </td> 
          <td bgcolor="#FFC1B3"> 
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
          </td> 
         </tr> 
         <tr> 
          <td bgcolor="#CABAF8" class="style13" colspan="2"> 
           &nbsp; 
          </td> 
         </tr> 
        </table> 
        &nbsp;&nbsp; 
       </ItemTemplate> 
       <EditItemTemplate> 
        <table class="style14"> 
         <tr> 
          <td bgcolor="#E3ACC8"> 
           <asp:LinkButton ID="cmnt2" runat="server" CommandName="Comment">Comment</asp:LinkButton> 
           &nbsp;&nbsp; 
           <asp:LinkButton ID="cancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton> 
          </td> 
         </tr> 
         <tr> 
          <td bgcolor="#CCFFFF"> 
           <asp:TextBox ID="TextBox3" runat="server" BorderColor="#003300" BorderStyle="Inset" 
            Height="30px" Width="486px"></asp:TextBox> 
          </td> 
         </tr> 
        </table> 
        &nbsp;&nbsp;&nbsp;&nbsp; 
        <br /> 
       </EditItemTemplate> 
       <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> 
       <SeparatorStyle BackColor="Yellow" Font-Bold="False" Font-Italic="False" Font-Overline="False" 
        Font-Size="Small" Font-Strikeout="False" Font-Underline="False" ForeColor="Maroon" 
        HorizontalAlign="Center" /> 
      </asp:DataList> 

最初,我由postedimage.visible =假值; 但由于上述编码,空白图像行正在出现。

回答

0

您可以设置在tr依赖于数据值的知名度,你需要把它变成一个服务器控件,虽然。如果没有图像集,我假设scrap是一个空字符串。

<tr runat="server" visible='<%# Eval("scrap") != "" %>'> 
    <td bgcolor="#CCFF99" colspan="2"> 
     <asp:Image ID="postedimage" runat="server" Height="202px" ImageUrl='<%# "~/Postedimages/" +Eval("scrap") %>' 
      Visible="False" Width="262px" /> 
    </td> 
</tr> 
+0

它不起作用 – jeneous

+0

仍然显示你不想要它,或者是否有错误?当用户没有发布图片时,“scrap”包含什么值? – Tieran

+0

错误编译器错误消息:CS1525:无效表达术语 '>' 源错误: 124行: 线125: 线126: “” %>'> 127行: 线128: jeneous

相关问题