简体   繁体   中英

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:

<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 ; you can then show/hide the panel accordingly: (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.

As an alternative you can wrap the panel in a conditional block if you want it all inline:

VB.NET:

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

        //put whatever inside the panel

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

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 %>' will only work if you use data-binding syntax ( '<%# %>' ), and then you will need to explicitly call DataBind() on it from code-behind anyway.

Unfortunately using '<%= $>' will just output static text and not be evaluated into a boolean to apply to the visible property.

Adapted from this similar question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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