将DropDownList的“style”属性绑定到一个asp.net ListView中 [英] Bind the 'style' property of a DropDownList inside an asp.net ListView

查看:178
本文介绍了将DropDownList的“style”属性绑定到一个asp.net ListView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ListView模板中有以下代码。

I have following code inside my ListView template.

  <asp:DropDownList runat="server" ID="myDropDown" Width="60px"
     SelectedValue='<%# Eval("SelectedValue") %>' 
     DataSource='<%# Eval("DropDownList") %>'
     style='display:<%# (bool)Eval("ShowDropDown") ? "block" : "none" %>;' >
  </asp:DropDownList>

想法是它会生成html,但是隐藏,所以我可以显示客户端。

The idea is that it will generate the html, but hidden so I can show the client-side.

然而,生成的html如下所示:

However, the html generated looks like this:

<select id="myDropDown" 
   style="width:60px;display:<%# (bool)Eval("ShowDropDown") ? "block" : "none" %>;">

我不明白为什么会这样做。有没有办法这样做(不使用类)?

I don't understand why it does this. Is there a way to do this (without using classes)?

推荐答案

您可以添加ListView ItemDataBound事件,然后添加样式属性为每行下拉列表。示例代码

You can add a ListView ItemDataBound Event and then add the style attribute for dropdownlist in each row. Sample Code:

protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
                DropDownList myDropDown = (DropDownLiast)e.Item.FindControl("myDropDown");

                System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;
                myDropDown.Style["display"] = ((bool)rowView["ShowDropDown"])?"block":"none";
        }
}

这篇关于将DropDownList的“style”属性绑定到一个asp.net ListView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆