Re: Trying to find SelectedItem in collection

3 posts, 0 answers
  1. Bill
    Bill avatar
    175 posts
    Member since:
    Apr 2010

    Posted 14 Apr 2010 Link to this post

    This is an excerpt from a post in aspnet forums:

    On the other hand, if you use <asp:CheckBox inside <ItemTemplate> </ItemTemplate> and if you store the values of true/false or 1/0 as field of type bit in your database, then you can do that as follows:

    1. <ItemTemplate>  
    2.     <asp:CheckBox ID="chk" runat="server" Checked='<%#DataBinder.Eval(Container.DataItem, "itemType") %>' />  
    3. </ItemTemplate>


     

    I'm using the Telerik GridTableView and have the following html:

    <code>

    <tr>
                                                            <td>
                                                                Item Type
                                                            </td>
                                                            <td>
                                                                <asp:RadioButtonList ID="rblItemType" RepeatDirection="Horizontal" runat="server" >
                                                                    <asp:ListItem runat="server" Text="Visible System Wide" Value="1" />
                                                                    <asp:ListItem runat="server" Text="Invisible System Wide" Value="0" />
                                                                </asp:RadioButtonList>
                                                            </td>
                                                        </tr>

    </code> 

    I would like to set the selectedvalue thru the html as you described above, but didn't work for me. No intellisense was available for the Checked item.

     

    If it didn't work thru the html, I wanted to do it thru the code-behind. Here is the following, but can't seem to get it right.

    <code>

    RadioButtonList itmtype = e.Item.FindControl("rblItemType") as RadioButtonList; // this has 2 items in the list - 1 & 0
                                    itmtype.SelectedValue = itmtype.Items.FindByValue("
    the value coming back from the database").ToString();

    </code>

    I don't know what should be placed in the unbold faced itme above inside the findbyvalue.

    I really need some help on this one.....

  2. Bill
    Bill avatar
    175 posts
    Member since:
    Apr 2010

    Posted 14 Apr 2010 Link to this post

    As per a user on the asp.net forums, here is the following post:

    I just looked up the manual pages for ListItem and RadioButtonList. Neither of them has a Checked property.
    This may be a problem related to the Telerik Control. If so, you may want to ask on their forums.

    So, what is the workaround or fix to allow me to select the selected or checked value via the HTML?
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Martin
    Admin
    Martin avatar
    811 posts

    Posted 19 Apr 2010 Link to this post

    Hello William Yeager,

    I am afraid that databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.ListItem does not have a DataBinding event. For more information you can review:

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitem_members%28v=VS.90%29.aspx

    As to the programmatic approach i have tried the following code and it woks as expected on my side:

    <asp:ScriptManager runat="server" ID="ScriptManager1">
    </asp:ScriptManager>
        <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
            AutoGenerateColumns="false" onitemdatabound="RadGrid1_ItemDataBound">
            <MasterTableView>
                <Columns>
                    <telerik:GridTemplateColumn>
                        <ItemTemplate>
                            <table>
                                <tr>
                                    <td>
                                        Item Type
                                    </td>
                                    <td>
                                        <asp:RadioButtonList ID="rblItemType" RepeatDirection="Horizontal" runat="server">
                                            <asp:ListItem runat="server" Text="Visible System Wide" Value="1" />
                                            <asp:ListItem runat="server" Text="Invisible System Wide" Value="0" />
                                        </asp:RadioButtonList>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>

    Code behind:

    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            DataTable table = new DataTable();
            table.Columns.Add("Selected", typeof(byte));
            for (int i = 0; i < 5; i++)
            {
                if (i % 2 == 0)
                {
                    table.Rows.Add(1);
                }
                else
                {
                    table.Rows.Add(0);
                }
            }
            RadGrid1.DataSource = table;
        }
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                RadioButtonList itmtype = e.Item.FindControl("rblItemType") as RadioButtonList;
                string b = (e.Item.DataItem as DataRowView)["Selected"].ToString();
                itmtype.Items.FindByValue(b).Selected = true;
            }
        }

    I hope this helps,
    Martin
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Back to Top