c# - 将列表从代码隐藏转移到 aspx 页面

标签 c# jquery asp.net .net list

我想动态填充一个列表并在我将鼠标悬停在按钮上时显示该列表

<div class="invite">
    <asp:Button ID="Button1" runat="server" Text="Invite"  CssClass="invite-link"/>
    <div class="invite-drop">
        <div class="holder">
            <ul runat="server">
                <li>
                    <a href="#">
                        <img src="images/img3.png" width="22" height="22" alt="image description" />
                        <span>Chris Robin</span>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <img src="images/img3.png" width="22" height="22" alt="image description" />
                        <span>Danny Defoee</span>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <img src="images/img3.png" width="22" height="22" alt="image description" />
                        <span>Gustav Lee</span>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <img src="images/img3.png" width="22" height="22" alt="image description" />
                        <span>Eric Rees</span>
                    </a>
                </li>
            </ul>
            <a class="select" href="#">Or select friend</a>
        </div>
    </div>
</div>

我的列表如下,我正在尝试动态构建它:

HtmlGenericControl list = new HtmlGenericControl("ul");
for (int i = 0; i < 3; i++)
{
    HtmlGenericControl listItem = new HtmlGenericControl("li");
    HtmlGenericControl anchor = new HtmlGenericControl("a");
    Image im = new Image();
    //im.ImageUrl = ds.Tables[0].Rows[1].ItemArray.ToString();
    anchor.Attributes.Add("href", "");
    anchor.Controls.Add(im);
    anchor.InnerText = "TabX";
}

编辑:

enter image description here

我已经成功地从代码隐藏中转移了列表,但是格式不正确想要显示图像一个在另一个下面

我的问题是如何将它传输到 aspx 设计页面?并显示在悬停事件上

请帮忙

最佳答案

将服务器端动态创建的ul添加到Page控件集合或页面的任意位置,并通过css或内联样式display使其默认隐藏:无。为 ul 提供一个 idclass,这将有助于在客户端找到该元素。

在客户端,您可以使用 $('ul.classname')$('#listId) 找到它,然后使用 show( ) 在悬停事件上并根据您的要求定位它。

基于您的代码。

ul一个id>

<ul ID="list" runat="server">

请注意,在 for 循环中,您应该将 anchor 添加到列表控件。

    for (int i = 0; i < 3; i++)
    {
        HtmlGenericControl listItem = new HtmlGenericControl("li");
        HtmlGenericControl anchor = new HtmlGenericControl("a");
        Image im = new Image();
        //im.ImageUrl = ds.Tables[0].Rows[1].ItemArray.ToString();
        anchor.Attributes.Add("href", "");
        anchor.Controls.Add(im);
        anchor.InnerText = "TabX";

        list.Controls.Add(anchor);
     }

Js

$('.invite-link').hover(function(){
     $('#list').show();
}, function(){
     $('#list').hide();
});

关于c# - 将列表从代码隐藏转移到 aspx 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9636315/

相关文章:

c# - 哪些图标/图像可以直接使用 .NET Framework 类?

javascript - 滚动未显示在 jquery 创建的列表中

javascript - 如何触发附加到我的页面的内联 Javascript? (ASP.NET 网络表单)

C# 返回具有祖先和自身但没有其他子元素的 XML 元素

c# - DropdownList.selectedIndex 始终为 0(是的,我有 !isPostBack)

javascript - jQuery/XML : synchronizing data and representations using DOM-Mutation-Events

javascript - jQuery - 显示div的出现次数

c# - 无法从动态生成的文件上传控件中保存文件

asp.net - 对齐一个asp :Button to to the right of asp:Wizard's Next Button

c# - 删除多余的空格,但在 C# 中使用正则表达式保留新行