ASP.NET using Bind/Eval in .aspx in If statement

核能气质少年 提交于 2019-12-09 02:15:15

问题


in my .aspx I'm looking to add in an If statement based on a value coming from the bind. I have tried the following:

<% if(bool.Parse(Eval("IsLinkable") as string)){ %>                    
        monkeys!!!!!!
        (please be aware there will be no monkeys, 
        this is only for humour purposes)
 <%} %>

IsLinkable is a bool coming from the Binder. I get the following error:

InvalidOperationException
Databinding methods such as Eval(), XPath(), and Bind() can only
be used in the context of a databound control.

回答1:


You need to add your logic to the ItemDataBound event of ListView. In the aspx you cannot have an if-statement in the context of a DataBinder: <%# if() %> doesn't work.

Have a look here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemdatabound.aspx

The event will be raised for each item that will be bound to your ListView and therefore the context in the event is related to the item.

Example, see if you can adjust it to your situation:

protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Label monkeyLabel = (Label)e.Item.FindControl("monkeyLabel");
        bool linkable = (bool)DataBinder.Eval(e.Item.DataItem, "IsLinkable");
        if (linkable)
           monkeyLabel.Text = "monkeys!!!!!! (please be aware there will be no monkeys, this is only for humour purposes)";
    }
}



回答2:


I'm pretty sure you can do something like the following

(Note I don't have a compiler handy to test the exact syntax)

text = '<%# string.Format("{0}", (bool)Eval("IsLinkable") ? "Monkeys!" : string.Empty) %>'

Yes this is c# and your using vb.net, so you'll need to use vb syntax for a ternary operator.

Edit - was able to throw into into a simple data bind situation, worked like a charm.




回答3:


You can use asp:PlaceHolder and in Visible can put eval. Like as below

   <asp:PlaceHolder ID="plc" runat="server" Visible='<%# Eval("IsLinkable")%>'>
       monkeys!!!!!!
       (please be aware there will be no monkeys, this is only for humour purposes)
   </asp:PlaceHolder>



回答4:


OMG this took entirely too long to figure out...

<asp:PlaceHolder runat="server" Visible='<%# Eval("formula.type").ToString()=="0" %>'> Content </asp:PlaceHolder>

formula.type is a linked table's int column. Thanks for the other contributions to get my resolution.




回答5:


If you are having issues getting e.Item.DataItem in Bazzz's answer try

protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    using (ListViewDataItem listViewDataItem = (ListViewDataItem) e.Item)
    {
        if (listViewDataItem != null)
        {
            Label monkeyLabel = (Label)e.Item.FindControl("monkeyLabel");
            bool linkable = (bool)DataBinder.Eval(listViewDataItem , "IsLinkable");
            if (linkable)
               monkeyLabel.Text = "monkeys!!!!!! (please be aware there will be no monkeys, this is only for humour purposes)";
        }
    }
}



回答6:


I know it is a bit late in the day for this answer but for what it is worth here is my solution to the problem:

<%# (bool)Eval("IsLinkable") ? "monkeys!!!!!!" : "" %>



回答7:


You can create a method to evaluate the value and return the value you want.

<%# IsLinkableABool( Eval("IsLinkable") ) %>

On the code behind you can create the method as follow

protected String IsLinkableABool(String isLinkable)
{
    if (isLinkable == Boolean.TrueString)
    {
         return "monkeys!!!!!! (please be aware...";    
    }
    else
    {
         return String.Empty;
    }
}



回答8:


Whenever I've needed to handle conditions within a databound control, I use the OnItemDataBound event.

So you could do:

protected void DataBound_ItemDataBoundEvent() {
     bool IsLinkable            = (bool)DataBinder.Eval(e.Item.DataItem, "IsLinkable");  
     if(IsLinkable) {
          //do stuff
     }                                     

}



回答9:


We would need to see the rest of your code, but the error message is giving me a bit of a hint. You can ONLY use Eval when you are inside of a data-bound control. SOmething such as a repeater, datagrid, etc.

If you are outside of a data bound control, you could load the value into a variable on the code-behind and make it public. Then you could use it on the ASPX for conditional processing.




回答10:


For FormView Control refer to this link.

Here is the sample code. My aspx page FormView Control look like below:

<asp:FormView ID="fv" runat="server" Height="16px" Width="832px"  
CellPadding="4" ForeColor="#333333" ondatabound="fv_DataBound"> 
    <ItemTemplate>
        <table>
            <tr>
                <td align="left" colspan="2" style="color:Blue;">
                    <asp:Label ID="lblPYN" runat="server" Text='<%# Eval("PreviousDegreeYN") %>'></asp:Label> 
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:FormView>

I am checking the value for <%# eval("PreviousDegreeYN") %>

If my eval("PreviousDegreeYN") == True, I want to display Yes in my label "lblPYN"

protected void fv_DataBound(object sender, EventArgs e)
{
    FormViewRow row = fv.Row;
    //Declaring Variable lblPYN
    Label lblPYN;
    lblPYN = (Label)row.FindControl("lblPYN");
    if (lblPYN.Text == "True")
    {
        lblPYN.ForeColor = Color.Blue;
        lblPYN.Text = "Yes";

    }
    else
    {
        lblPYN.ForeColor = Color.Blue;
        lblPYN.Text = "No";

    }
}



回答11:


Putting condition aspx page is not a good idea.also messy. U can do using ternary operator.But I suggest u to use rowdatabound events of grid view. step 1-go to grid view properties.Click on lighting button to list all event. Step 2-give a name on rowdatabound and double click

protected void onrow(object sender, GridViewRowEventArgs e)

   {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TableCell statusCell = e.Row.Cells[8];//Means column 9

        if (statusCell.Text == "0")
        {
            statusCell.Text = "No Doc uploaded";

        }
        else if (statusCell.Text == "1")
        {
            statusCell.Text = "Pending";
        }
        else if (statusCell.Text == "2")
        {
            statusCell.Text = "Verified";
        }
    }
}


来源:https://stackoverflow.com/questions/5596484/asp-net-using-bind-eval-in-aspx-in-if-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!