I have a problem and I can't find an answer to it. In VS2012 I created an ASP.NET website in C#.

I have my membership provider setup and working, now I wanted to make an adminpage where an admin could edit the role and block a user. From the server explorer, I dragged the asp_membership table to the page and it created itself.

I've deleted a few irrelevant columns and added an itemtemplatefield with two buttons. Take a look at the code first:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="UserId" DataSourceID="SqlDataSource1" 
    EmptyDataText="There are no data records to display." 
    OnSelectedIndexChanged="GridView1_SelectedIndexChanged" >
    <Columns>
        <asp:CommandField ShowDeleteButton="True" />
        <asp:TemplateField HeaderText="Block users">
            <ItemTemplate>
                <asp:Button runat="server" ID="btnBlock" CommandName="Block"
                    Text="Block" OnClick="btnBlock_Click" Visible='<%# Convert.ToBoolean(Eval("IsLockedOut").ToString()) %>'/>
                <asp:Button runat="server" ID="btnDeblock" CommandName="Deblock"
                    Text="Deblock" OnClick="bntDeblock_Click" Visible='<%# Convert.ToBoolean(Eval("IsLockedOut").ToString()) %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="UserId" HeaderText="User id" ReadOnly="True" 
            SortExpression="UserId" />
        <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
        <asp:CheckBoxField DataField="IsApproved" HeaderText="Confirmed" 
            SortExpression="IsApproved" />
        <asp:BoundField DataField="CreateDate" HeaderText="Created" 
            SortExpression="CreateDate" />
        <asp:BoundField DataField="LastLoginDate" HeaderText="Last login" 
            SortExpression="LastLoginDate" />
        <asp:CheckBoxField DataField="IsLockedOut" HeaderText="Locked" 
            SortExpression="IsLockedOut" />
        <asp:BoundField DataField="LastLockoutDate" HeaderText="Last lockout" 
            SortExpression="LastLockoutDate" />
        <asp:BoundField DataField="FailedPasswordAttemptCount" 
            HeaderText="Failed logins" 
            SortExpression="FailedPasswordAttemptCount" />
        <asp:BoundField DataField="Comment" HeaderText="Comments" 
            SortExpression="Comment" />
    </Columns>
</asp:GridView>

I'm trying to make two butons (block and deblock) that should be visible if the checkboxfield "IsLockedOut" is disabled and invisible if... you get the point.

The buttons both disappear when I load the page. I assume the error resides here: <%# Convert.ToBoolean(Eval("IsLockedOut").ToString()) %>? Can someone help me?

有帮助吗?

解决方案

You shouldn't need the .ToString() or the Convert.ToBoolean(); .NET will pretty much do that under the hood for you.

Visible='<%# Eval("IsLockedOut") %>'

What do you see if you add a <asp:Label /> to your ItemTemplate and bind the Text property as the above? Is your IsLockedOut property a Nullable<Boolean>? If it is, you could try this:

Visible = '<%# Eval("IsLockedOut") ?? true %>'

Or

Visible = '<%# ((Nullable<bool>)Eval("IsLockedOut")).GetValueOrDefault(true) %>'
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top