如何将标签控件转换为Image控件?

时间:2012-08-25 11:03:13

标签: asp.net

在带有模板的数据列表中,我有3个标签要存储(名字,剪贴簿,发布时间),图像存储配置文件图片以及另外一个图像控件用于存储图像,用户将图像作为剪贴图发布。现在我希望这个图像控件仅在用户发布图像时可见,否则该行不可见。

代码是

        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>

最初,我创建了pastimage.visible = false的值; 但是由于上面的编码,出现了空白图像行。

1 个答案:

答案 0 :(得分: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>