Wrong key value bound to SelectedValue via Eval()

3 posts, 1 answers
  1. Alexander
    Alexander avatar
    16 posts
    Member since:
    Oct 2007

    Posted 27 Oct 2008 Link to this post

    There is a RadGrid, which is used to show and edit XML-data in a grid form. And the same XML-data can be edited manually.

    There is a ComboBox on the RadGrid’s edit template. It is used to select a value from dictionary (a database table of key –value pairs ).

    ComboBox is bound to a field in the grid’s datasource:

    <telerik:RadComboBox ID="ddProductName" runat="server" DataSourceID="dsProducts" DataTextField="Name" DataValueField="ProductID" SelectedValue='<%# Eval("ProductID") %>' />

    The problem is, that since grid’s underlying XML is manually editable, Eval() function may return invalid key values. That is, a value that is not present in the dictionary table.  In such cases, the above binding to SelectedValue attribute fails, and the whole page crashes.

    What do I do to overcome this problem?

  2. Answer
    Simon
    Admin
    Simon avatar
    2281 posts

    Posted 27 Oct 2008 Link to this post

    Hi Alexander,

    Why don't you try setting the selected value of the ComboBox in the OnItemDataBound event handler of the Grid?

    In the event handler you can find the ComboBox and verify whether the ProductID value exists in its Items collection and defaulting to the first Item in case it does not.

    Regards,
    Simon
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Alexander
    Alexander avatar
    16 posts
    Member since:
    Oct 2007

    Posted 29 Oct 2008 Link to this post

    Thank you! Sure enough, it works with grid's OnItemDataBound.
Back to Top