1

Hi anybody know how to use databinder.eval in c#

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?

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
jestges
  • 3,686
  • 24
  • 59
  • 95
  • Can you post a bit more of your code please howing the complete usage. – Greg B Apr 14 '10 at 12:38
  • Just I try to use DataBinder.Eval method through c# code instead of aspx inline. So it I tried like this. But I didnt get succeeded. DataBinder.Eval(object container,string expression) this is the signature. Here what does it mean by object container? Any idea? – jestges Apr 14 '10 at 12:43

4 Answers4

4

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
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
4

You can't use Eval in the code behind of an aspx page.

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).

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

Link to DataBinder Eval Method: http://msdn.microsoft.com/en-us/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

Community
  • 1
  • 1
kemiller2002
  • 113,795
  • 27
  • 197
  • 251
  • Hi Thank you for your reply. What does it mean by YOUR_OBJECT_PROPERTY_HERE ? I'm sorry can you give me an example? – jestges Apr 14 '10 at 12:47
  • @jestges it would be something like: [ObjectInstance].[Property] or CollegeClass.CourseID – kemiller2002 Apr 14 '10 at 12:51
  • I've gone throug many examples. In every example they are showing with static controls like But I want to create this link buttondynamically and want to set the databinder.eval – jestges Apr 14 '10 at 12:52
2
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowIndex > -1)
    {

        string h = DataBinder.Eval(e.Row.DataItem, "ColumnName").ToString();
    }
}
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
vetrivel
  • 21
  • 1
0

You should use Eval expression and <% %> in *.aspx code not with C# code.

sashaeve
  • 9,387
  • 10
  • 48
  • 61