有没有办法把链接放在div里面

时间:2011-03-26 13:44:43

标签: c# asp.net html css

有没有办法可以使用c#/ css在div中放置一个链接,然后在c#asp.net中创建一个删除该div的函数?

我希望将删除添加到我的div的右上角,这就像一个链接,然后在我的代码后面调用一个函数,该函数将从我的数据库中删除该条目。

我的css:

div .test
{
  width:90%; 
  z-index:1; 
  padding:27.5px; 
  border-top: thin solid #736F6E;
  border-bottom: thin solid #736F6E;
  color:#ffffff;
  margin:0 auto;
  white-space: pre;
  white-space: pre-wrap;
  white-space: pre-line;
  word-wrap: break-word;
}

编辑: 到目前为止,页面上没有任何内容呈现:

System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
                    div.Attributes["class"] = "test";
                    div.ID = "test";


                    // Deletebutton div with link
                    System.Web.UI.HtmlControls.HtmlGenericControl divClose = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
                    divClose.Attributes["class"] = "deleteButton";
                    divClose.ID = "deleteButton";
                    div.Controls.Add(divClose);
                    System.Web.UI.HtmlControls.HtmlGenericControl link = new System.Web.UI.HtmlControls.HtmlGenericControl("a");
                    link.ID = "link";
                    divClose.Controls.Add(link);

                    Image img = new Image();
                    img.ImageUrl = String.Format("{0}", reader.GetString(1));
                    img.AlternateText = "Test image";

                    div.Controls.Add(img);
                    div.Controls.Add(ParseControl(String.Format("{0}", reader.GetString(0))));
                    div.Style["clear"] = "both";

                    test1.Controls.Add(div);

的CSS:

div#test1 {
}
div .test {
  width:90%; 
  z-index:1; 
  padding:27.5px; 
  border-top: thin solid #736F6E;
  border-bottom: thin solid #736F6E;
  color:#ffffff;
  margin:0 auto;
  white-space: pre;
  white-space: pre-wrap;
  white-space: pre-line;
  word-wrap: break-word;
}
.deleteButton {
  width: 200px;
  height: 15px;
  border: 1px solid blue;
}

.deleteButton a{
  display:none;
}

.deleteButton:hover a{
  display:block;
}

2 个答案:

答案 0 :(得分:1)

CSS无法创建链接。您必须在HTML中构建它们。

答案 1 :(得分:1)

在您的HTML中创建链接。我们将使用css隐藏它,并在我们悬停div时显示它。

<div class="deleteButton">
  <asp:LinkButton runat="server" id="delButton" Text="Delete"/>
</div>

如果您在代码中创建div:

System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
div.Attributes["class"] = "test";
div.ID = "test";


// Deletebutton div with link
System.Web.UI.HtmlControls.HtmlGenericControl divClose = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
divClose.Attributes["class"] = "closeButton";
divClose.ID = "closeButton";
div.Controls.Add(divClose);
System.Web.UI.HtmlControls.HtmlGenericControl link= new System.Web.UI.HtmlControls.HtmlGenericControl("a");
link.ID = "link";
divClose.Controls.Add(link);

img = new Image();
ImageUrl = String.Format("{0}", reader.GetString(1));
img.AlternateText = "Test image";

div.Controls.Add(img);
div.Controls.Add(ParseControl(String.Format("{0}", reader.GetString(0))));
div.Style["clear"] = "both";

test1.Controls.Add(div);

这是用于隐藏/显示链接的CSS。

<!-- CSS -->
.deleteButton {
  width: 200px;
  height: 15px;
  border: 1px solid blue;
}

.deleteButton a{
  display:none;
}

.deleteButton:hover a{
  display:block;
}