【问题标题】:Transfer list from code behind to aspx page将列表从后面的代码传输到 aspx 页面
【发布时间】:2012-03-09 15:08:55
【问题描述】:

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

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

编辑:

我已成功从代码隐藏中传输列表,但是格式不正确,希望将图像显示在另一个下方

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

请帮忙

【问题讨论】:

    标签: c# jquery asp.net .net list


    【解决方案1】:

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

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

    根据您的代码。

    ul一个ID

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

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

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

    【讨论】:

    • 当我将鼠标悬停在按钮上时,我多次获得 tabx 有没有一种方法可以显示我在列表中获得的名称来代替它
    • 如果可能的话,为什么不从服务器端渲染正确的名称?
    • 是的,它确实对我有帮助,但是有问题请帮助检查编辑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多