views:

46

answers:

1

Hi

I thought Eval("JobTitle") converts returned value to type String, but that doesn’t seem to be the case, since value (returned by Eval("JobTitle")) passed to GetSelectedJobTitle() was of type Object and not String, which caused an error saying “cannot convert from object to string”.

<EditItemTemplate>
    <asp:DropDownList ID="EditJob" runat="server" 
           SelectedIndex='<%# GetSelectedJobTitle(Eval("JobTitle")) %>'
           DataSource=’<%# Titles %>’>    
    </asp:DropDownList>
</EditItemTemplate>


public int GetSelectedJobTitle(string title)
{
    ...
}

public string[] Titles
{
    ...
}

a) So when does the conversion (from Object to String) of value returned from Eval("JobTitle")) happen then?

b) And if Eval doesn’t do the conversion, what method does?

thanx


EDIT:


I assume that in our example GetSelectedJobTitle() is called before Asp.Net evaluates ( and converts it to string ) the expression contained inside <%# %>?

+1  A: 

Eval returns "object". You have to cast it to a string if you know that you'll get a string.

<EditItemTemplate>
    <asp:DropDownList ID="EditJob" runat="server" 
           SelectedIndex='<%# GetSelectedJobTitle((string)Eval("JobTitle")) %>'
           DataSource=’<%# Titles %>’>    
    </asp:DropDownList>
</EditItemTemplate>

The conversion happens during the DataBind Event.

EDIT: Better answer the comments here.

Our big difference is that statement:

Yours:

<%# GetSelectedJobTitle(Eval("JobTitle")) %>

gives me also a

Error 2 Argument '1': cannot convert from 'object' to 'string' p:\WebSite1\Default.aspx 19

Mine:

<%# GetSelectedJobTitle((string)Eval("JobTitle")) %>

Compiles!

Arthur
A) "The conversion happens during the DataBind Event." GetSelectedJobTitle() also gets called during DataBind event, but when exactly does it get called and when exactly does the conversion happen? B) Also, I assume that in our example GetSelectedJobTitle() is called before Asp.Net evaluates ( and converts it to string ) the expression contained inside <%# %>?
carewithl
A) Yes, GetSelectedJobTitles is called during DataBinding. B) NO! First, Eval is called, returns an object, gets casted to string (in my code snipped) and then GetSelectedJobTitles get's called.
Arthur
I thought Asp.Net converts to String only the value returned by <%# %> expression --> thus in our example the value returned by GetSelectedJobTitle?! I thought the order was the following – Eval returns a value V of type object, then GetSelectedJobTitle ( V is passed as argument of type Object ) is called and only then does Asp.Net convert resulting value to String?! You’ve said that value returned by Eval gets casted to String and then GetSelectedJobTitle gets called.But if that was true, then I wouldn’t get “cannot convert from object to string” error
carewithl
We are talking about the same - sorry, if I'm not able to produce good English. Could it be, that in your example "JobTitle" is null? then DBNull.Value would be returned, not a string or null.
Arthur
A)"Could it be, that in your example "JobTitle" is null?"No, JobTitle is not null. B) "We are talking about the same" if I understand you correctly, then you agree that order is the following: Eval returns a value V of type object, then GetSelectedJobTitle ( V is passed as argument of type Object ) is called and only then does Asp.Net convert resulting value to String?
carewithl
See edits in my Answer
Arthur
Oh, I didn't notice that you converted to string the value returned by Eval. thanx for helping me
carewithl