简体   繁体   English

asp.net控件属性中的嵌入式代码

[英]Embedded Code in attribute of asp.net controls

I want bind the visibility of a panel to the selectedValue Or selectedIndex of a gridview, so i try this: 我想将面板的可见性绑定到gridview的selectedValue或selectedIndex,所以我尝试这样做:

<asp:Panel ID="pnlPic" Visible='<%   gvAllQuarries.SelectedIndex  == -1 ? false:true  %>' runat="server" >

or 要么

 <asp:Panel ID="pnlPic" Visible='<%=   gvAllQuarries.SelectedIndex  == -1 ? false:true  %>' runat="server" >

but syntax is wrong and unknown for intelisense. 但是语法是错误的,对于智能感知来说是未知的。 how can i bind like this? 我怎么能这样绑定? is it possible? 可能吗?

Try putting an event handler on the SelectedIndexChanged event of the GridView ; 尝试将事件处理程序放在GridViewSelectedIndexChanged事件上; you can then show/hide the panel accordingly: (VB.NET) 然后您可以相应地显示/隐藏面板:(VB.NET)

Protected Sub gvAllQuarries_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gvAllQuarries.SelectedIndexChanged

    pnlPic.Visible = Not (gvAllQuarries.SelectedIndex = -1)

End Sub

Mess around with the logic to get it playing how you want, adapt to C# if need be. 弄乱逻辑,使其按您的意愿进行播放,如有需要,请适应C#。

As an alternative you can wrap the panel in a conditional block if you want it all inline: 另外,如果您希望所有面板都内联,则可以将面板包装在条件块中:

VB.NET: VB.NET:

<% If gvAllQuarries.SelectedIndex <> 1 Then%>
    <asp:Panel ID="Panel1" runat="server">

        //put whatever inside the panel

    </asp:Panel>
<% End If %>

C#: C#:

<% if (gvAllQuarries.SelectedIndex != 1) { %>
    <asp:Panel ID="Panel1" runat="server">

        //put whatever inside the panel

    </asp:Panel>
<% } %>

It's a bit messy, but the only way I can see of you getting it all inline without code-behind. 有点混乱,但是我能看到的唯一方法就是不用代码隐藏就可以将它们全部内联。 The option you are trying Visible='<% gvAllQuarries.SelectedIndex == -1 ? false:true %>' 您正在尝试的选项Visible='<% gvAllQuarries.SelectedIndex == -1 ? false:true %>' Visible='<% gvAllQuarries.SelectedIndex == -1 ? false:true %>' will only work if you use data-binding syntax ( '<%# %>' ), and then you will need to explicitly call DataBind() on it from code-behind anyway. Visible='<% gvAllQuarries.SelectedIndex == -1 ? false:true %>'仅在使用数据绑定语法( '<%# %>' )时才起作用,然后无论如何,您都需要从代码背后显式调用DataBind()

Unfortunately using '<%= $>' will just output static text and not be evaluated into a boolean to apply to the visible property. 不幸的是,使用'<%= $>'只会输出静态文本,不会被评估为布尔值以应用于visible属性。

Adapted from this similar question . 适应了这个类似的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM