【问题标题】:Make shorter options in WebForms dropdown在 WebForms 下拉列表中创建更短的选项
【发布时间】:2015-04-16 12:17:14
【问题描述】:

我在 WebForms 中创建 dropDownList 并将一些数据(“Title”和“Id”)的列表绑定到它。

Ddltitlelist.DataSource = submissionTitleList;
Ddltitlelist.DataTextField = "Submission_Title";
Ddltitlelist.DataValueField = "Submission_Id";
Ddltitlelist.DataBind();

但是当 Title 太长时看起来不太好。我尝试手动将max-width 设置为选择选项,但这不起作用。

所以我的想法是为选项设置 max-width 并在标题太长时添加“...”并为每个选项添加标题(当悬停在某个选项上时,将显示类似标准工具提示的内容)。但首先我需要限制选择选项的宽度。

【问题讨论】:

标签: html css asp.net drop-down-menu webforms


【解决方案1】:

不看你的代码很难说,但你可以做的一种方法是限制 SQL 中文本的长度。像这样的

select Submission_Id,
Left(Submission_Title, 20) as Submission_Title
from Submission
where ....

另一种选择是在数据绑定后调整长度并以这种方式修剪

ASP.NET

<asp:DropDownList ID="Ddltitlelist" runat="server"
    OnDataBound="Ddltitlelist_DataBound">
</asp:DropDownList>

背后的代码

protected void Ddltitlelist_DataBound(object sender, EventArgs e)
{
    foreach (ListItem li in Ddltitlelist.Items)
    {
        string text = li.Text;

        // set tooltip on drop down
        li.Attributes.Add("title", text);
        // if over 20 characters, trim to fit
        if (text.Length > 20)
        {
            // trim to 17 and add 3 periods for an ellipsis
            li.Text = text.Substring(0, 17) + "...";
        }
    }
}

【讨论】:

  • 谢谢,对我来说似乎是解决方案。请注意:在我的情况下,li.Attributes("title", text); 无效。我改为写li.Attributes.Add("title", text);
  • 啊,我会解决的。我只是在这里打字,没有测试它,很高兴它有效。
猜你喜欢
  • 2016-08-25
  • 1970-01-01
  • 2010-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
相关资源
最近更新 更多