简体   繁体   English

C#中的DataBinder.Eval

[英]DataBinder.Eval in c#

Hi anybody know how to use databinder.eval in c# 嗨,任何人都知道如何在C#中使用databinder.eval

Actually I've tried this 其实我已经试过了

LinkButton lnkName = new LinkButton();
lnkName.CommandArgument = DataBinder.Eval("object","<%#COURSE_ID%>");

it is showing error. 它显示错误。 Whats the wrong with this? 这怎么了?

For Example in design page you can use like: 对于设计页面中的示例,您可以使用类似:

<asp:Button ID="btnEdit" CommandName="Edit" 
    CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'
    CssClass="cursor_hand" runat="server" Text="Edit" />

Code Behind: 背后的代码:

int rowIndex = int.Parse(e.CommandArgument.ToString());

if (e.CommandName.Equals("Edit"))
{
   //do something
}

You can't use Eval in the code behind of an aspx page. 您不能在aspx页面后面的代码中使用Eval。

this: 这个:

lnkName.CommandArgument = DataBinder.Eval("object","<%#COURSE_ID%>");

should be this: 应该是这样的:

lnkName.CommandArgument = YOUR_OBJECT_PROPERTY_HERE;

To fill in YOUR_OBJECT_PROPERTY_HERE you either need to specify the object.property etc like normal in C# code, or you'll have to use reflection to get the property value from the object (which is what eval does for you). 要填写YOUR_OBJECT_PROPERTY_HERE,您需要像在C#代码中一样指定object.property等,或者您必须使用反射来从对象获取属性值(eval为您执行的操作)。

Here is a link showing how to use reflection to get the property information from an object. 这是显示如何使用反射从对象获取属性信息的链接。 You can use it to duplicate how eval works if you need to: https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6099345.html 如果需要,您可以使用它来复制评估的工作方式: https : //web.archive.org/web/1/http : //articles.techrepublic%2ecom%2ecom/5100-10878_11-6099345.html

Link to DataBinder Eval Method: http://msdn.microsoft.com/en-us/library/4hx47hfe.aspx 链接到DataBinder评估方法: http : //msdn.microsoft.com/zh-cn/library/4hx47hfe.aspx

How the DataBinder Eval Method works (and why the author thinks it should be avoided) http://weblogs.asp.net/jgalloway/archive/2005/09/20/425687.aspx DataBinder评估方法的工作原理(以及为什么作者认为应避免这种方法) http://weblogs.asp.net/jgalloway/archive/2005/09/20/425687.aspx

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowIndex > -1)
    {

        string h = DataBinder.Eval(e.Row.DataItem, "ColumnName").ToString();
    }
}

您应该在* .aspx代码中使用Eval表达式和<% %> ,而不是C#代码。

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

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