if evaluation in template column

Discussion in 'ASP .Net Datagrid Control' started by Crash, Aug 12, 2004.

  1. Crash

    Crash Guest

    I want to set the Selected attribute depending on the data in the data item, this was what I tried, but didn't work.

    <asp:TemplateColumn HeaderText="Schedule Type">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "Schedule Type")%>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:DropDownList Runat=server Id="SelScheduleType">
    <asp:ListItem Value=100 <%if(DataBinder.Eval(Container.DataItem, "Schedule Type")==100){%> Selected="True" <%}%>>Daily/Weekly</asp:ListItem>
    <asp:ListItem Value=200>Monthly</asp:ListItem>
    </asp:DropDownList>
    </EditItemTemplate>
    </asp:TemplateColumn>

    of course it doesnt like the
    <%if(DataBinder.Eval(Container.DataItem, "Schedule Type")==100){%> Selected="True" <%}%>

    Can anyone point me in the right direction? Thanks...
     
    Crash, Aug 12, 2004
    #1
    1. Advertising

  2. Crash

    Scott G. Guest

    This kind of thing is tricky because you need a data binding (i.e. <%# ) but you are working with a nested control.

    I would override the DataGrid OnItemDataBound event and look for the item that is being edited and set the DropDownList by hand; something like:

    void Item_Bound(object sender, DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.EditItem)
    {
    DropDownList ddl = e.Item.FindControl("SelScheduleType") as DropDownList;
    if (ddl != null)
    {
    ddl.SelectedIndex = -1;
    DataRowView r = e.Item.DataItem as DataRowView;
    if (r != null)
    {
    ListItem it = ddl.Items.FindByValue(r["Schedule Type"].ToString());
    if (it != null)
    {
    it.Selected = true;
    }
    }
    }
    }
    }

    Scott
    "Crash" <> wrote in message news:uN%...
    I want to set the Selected attribute depending on the data in the data item, this was what I tried, but didn't work.

    <asp:TemplateColumn HeaderText="Schedule Type">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "Schedule Type")%>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:DropDownList Runat=server Id="SelScheduleType">
    <asp:ListItem Value=100 <%if(DataBinder.Eval(Container.DataItem, "Schedule Type")==100){%> Selected="True" <%}%>>Daily/Weekly</asp:ListItem>
    <asp:ListItem Value=200>Monthly</asp:ListItem>
    </asp:DropDownList>
    </EditItemTemplate>
    </asp:TemplateColumn>

    of course it doesnt like the
    <%if(DataBinder.Eval(Container.DataItem, "Schedule Type")==100){%> Selected="True" <%}%>

    Can anyone point me in the right direction? Thanks...
     
    Scott G., Aug 16, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dexter
    Replies:
    1
    Views:
    432
    Eliyahu Goldin
    Jan 18, 2005
  2. Ken Varn
    Replies:
    1
    Views:
    989
    Ken Varn
    Aug 19, 2005
  3. Ilias Lazaridis
    Replies:
    2
    Views:
    401
    Ilias Lazaridis
    Apr 24, 2005
  4. Ilias Lazaridis
    Replies:
    74
    Views:
    791
    Ilias Lazaridis
    Apr 4, 2005
  5. Ilias Lazaridis
    Replies:
    18
    Views:
    359
    Bill Guindon
    Apr 9, 2005
Loading...

Share This Page