如何在repeater中根据条件显示/隐藏HTML
如何在repeater中根据条件显示/隐藏HTML
<tr><td><span class="style1">
版主回复:</span><br>
<span class="style4"><%# DataBinder.Eval(Container.DataItem, "strRepcontent") %></span>
</td></tr>
比如我想根据判断<%# DataBinder.Eval(Container.DataItem, "strRepcontent") %>是否为空来显示上面的全部代码,应该怎么判断呢!希望高手赐教!
我试过这个方法,但是不管用,也不知道错在哪里?
<asp:Panel Visible='<%# !(bool)((DataBinder.Eval(Container.DataItem, "strRepcontent")==" ")) %> ' Runat=server>
<tr><td><span class="style1">
版主回复:</span><br>
<span class="style4"><%# DataBinder.Eval(Container.DataItem, "strRepcontent") %></span>
</td></tr>
</asp:Panel>
版主回复:</span><br>
<span class="style4"><%# DataBinder.Eval(Container.DataItem, "strRepcontent") %></span>
</td></tr>
比如我想根据判断<%# DataBinder.Eval(Container.DataItem, "strRepcontent") %>是否为空来显示上面的全部代码,应该怎么判断呢!希望高手赐教!
我试过这个方法,但是不管用,也不知道错在哪里?
<asp:Panel Visible='<%# !(bool)((DataBinder.Eval(Container.DataItem, "strRepcontent")==" ")) %> ' Runat=server>
<tr><td><span class="style1">
版主回复:</span><br>
<span class="style4"><%# DataBinder.Eval(Container.DataItem, "strRepcontent") %></span>
</td></tr>
</asp:Panel>
解决方法:
1\<asp:Panel Visible='<%# !(bool)((DataBinder.Eval(Container.DataItem, "strRepcontent").ToString()=="")) %> ' Runat=server>
2\ <asp:Panel Visible='<%# !(bool)((DataBinder.Eval(Container.DataItem, "strRepcontent").ToString()=="")) %> ' Runat=server>
-----------------------------------------------
显示隐藏?
服务端使用Visible属性,
客户端可以使用style.display="none"(block)或者style.visiblity="hidden"(visible)实现
-----------------------------------------
代码如下:
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
'Create a connection
Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
BindData()
End If
End Sub
Sub BindData()
'2. Create the command object, passing in the SQL string
Const strSQL as String = "SELECT * FROM tblFAQ WHERE FAQID <= 20"
'Set the datagrid's datasource to the datareader and databind
Dim resultsDataSet as New DataSet()
Dim myDataAdapter as SqlDataAdapter = New SqlDataAdapter(strSQL, myConnection)
myDataAdapter.Fill(resultsDataSet)
rptFAQs.DataSource = resultsDataSet
rptFAQs.DataBind()
End Sub
</script>
<script language="JavaScript">
function ToggleDisplay(id)
{
var elem = document.getElementById('d' + id);
if (elem)
{
if (elem.style.display != 'block')
{
elem.style.display = 'block';
elem.style.visibility = 'visible';
}
else
{
elem.style.display = 'none';
elem.style.visibility = 'hidden';
}
}
}
</script>
<style>
.header { font-size: larger; font-weight: bold; cursor: hand; cursor:pointer;
background-color:#cccccc; font-family: Verdana; }
.details { display:none; visibility:hidden; background-color:#eeeeee;
font-family: Verdana; }
</style>
<asp:Repeater id="rptFAQs" runat="server">
<ItemTemplate>
<div id='h<%# DataBinder.Eval(Container, "ItemIndex") %>' class="header"
onclick='ToggleDisplay(<%# DataBinder.Eval(Container, "ItemIndex") %>);'>
<%# DataBinder.Eval(Container.DataItem, "Description") %>
</div>
<div id='d<%# DataBinder.Eval(Container, "ItemIndex") %>' class="details">
<b>Submitted By:</b> <%# DataBinder.Eval(Container.DataItem, "SubmittedByName") %><br />
<b>Views:</b> <%# DataBinder.Eval(Container.DataItem, "ViewCount", "{0:d}") %><br />
<b>FAQ:</b><br />
<%# DataBinder.Eval(Container.DataItem, "Answer") %>
</div>
</ItemTemplate>
</asp:Repeater>
-----------------------------------------------
显示隐藏?
服务端使用Visible属性,
客户端可以使用style.display="none"(block)或者style.visiblity="hidden"(visible)实现
-----------------------------------------
代码如下:
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
'Create a connection
Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
BindData()
End If
End Sub
Sub BindData()
'2. Create the command object, passing in the SQL string
Const strSQL as String = "SELECT * FROM tblFAQ WHERE FAQID <= 20"
'Set the datagrid's datasource to the datareader and databind
Dim resultsDataSet as New DataSet()
Dim myDataAdapter as SqlDataAdapter = New SqlDataAdapter(strSQL, myConnection)
myDataAdapter.Fill(resultsDataSet)
rptFAQs.DataSource = resultsDataSet
rptFAQs.DataBind()
End Sub
</script>
<script language="JavaScript">
function ToggleDisplay(id)
{
var elem = document.getElementById('d' + id);
if (elem)
{
if (elem.style.display != 'block')
{
elem.style.display = 'block';
elem.style.visibility = 'visible';
}
else
{
elem.style.display = 'none';
elem.style.visibility = 'hidden';
}
}
}
</script>
<style>
.header { font-size: larger; font-weight: bold; cursor: hand; cursor:pointer;
background-color:#cccccc; font-family: Verdana; }
.details { display:none; visibility:hidden; background-color:#eeeeee;
font-family: Verdana; }
</style>
<asp:Repeater id="rptFAQs" runat="server">
<ItemTemplate>
<div id='h<%# DataBinder.Eval(Container, "ItemIndex") %>' class="header"
onclick='ToggleDisplay(<%# DataBinder.Eval(Container, "ItemIndex") %>);'>
<%# DataBinder.Eval(Container.DataItem, "Description") %>
</div>
<div id='d<%# DataBinder.Eval(Container, "ItemIndex") %>' class="details">
<b>Submitted By:</b> <%# DataBinder.Eval(Container.DataItem, "SubmittedByName") %><br />
<b>Views:</b> <%# DataBinder.Eval(Container.DataItem, "ViewCount", "{0:d}") %><br />
<b>FAQ:</b><br />
<%# DataBinder.Eval(Container.DataItem, "Answer") %>
</div>
</ItemTemplate>
</asp:Repeater>