This is a migrated thread and some comments may be shown as answers.

Telerik radgrid and radiobuttonlist

2 Answers 494 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lisa
Top achievements
Rank 1
Lisa asked on 06 Jun 2013, 05:55 PM

The code below is supposed for adding a RadioButtonList to an existing form which uses telerik controls. An appropriate radio button is displayed as selected when the page renders the first time. After that  the user can select a different radio button, and that will trigger OnSelectedIndexChanged to save the new value to the database. Somehow the condition:if (e.Item is GridEditableItem && e.Item.IsInEditMode)  is never met. I see samples on the internet but cannot make this works. I'd appreciate it if someone can correct the codes that I have below.

I want the page to display 2 radio buttons without making the user click on any link (such as Edit) to see the 2 radio buttons.

<telerik:RadGrid ID="RadGrid1"  runat="server" AutoGenerateColumns="False"
  ShowStatusBar="True" GridLines="Both"  OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound"
  CellSpacing="2" OnSelectedIndexChanged="RadGrid1_OnSelectedIndexChanged" >
  <MasterTableView NoMasterRecordsText="data not found" Width="100%" GridLines="Both" TableLayout="Fixed">
    <Columns>
      
            <telerik:GridBoundColumn  Visible="false"   DataField="id" SortExpression="id" UniqueName="id" />
  
      <telerik:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="Country">
        <ItemTemplate>
          <asp:Label ID="Label1" runat="server">test list</asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
                    <asp:RadioButtonList  CellPadding="0" DataValueField="rbtestList" SelectedValue='<%#Eval("rbtestList")%>'
                         CellSpacing="0" ID="rbtestList" runat="server" RepeatColumns="4"
                         RepeatDirection="Vertical" TabIndex="7" >
                        <asp:ListItem Text="0" Value="0"/>
                        <asp:ListItem Text="1" Value="1" />
                     </asp:RadioButtonList>
        </EditItemTemplate>
  
      </telerik:GridTemplateColumn>
        </Columns>
  </MasterTableView>
</telerik:RadGrid>
  
    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
            RadGrid1.DataSource = this.dbData;
    }
  
    protected override void Page_Load(object sender, EventArgs e)
    {
            if (!Page.IsPostBack)
            {
                if (this.dbData.Tables[0].Rows.Count < 1)
                {
                    RadGrid1.MasterTableView.IsItemInserted = true;
                }
                else
                {
                    RadGrid1.MasterTableView.IsItemInserted = false;
                }
            }
    }
      
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
            totalRows = dbData.Tables[0].Rows.Count;
              
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditableItem item = e.Item as GridEditableItem;
  
                if (totalRows > 0)
                {
                    RadioButtonList rbltestList = (RadioButtonList)item.FindControl("rbtestList");
                    rbltestList.SelectedValue = dbData.Tables[0].Rows[0]["testList"].ToString().Trim();
                }
            }
    }
  
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
            if (!Page.IsPostBack)
            {
                GridBoundColumn testListColumn =
                       (GridBoundColumn)RadGrid1.MasterTableView.GetColumn("rbtestList");
            }
    }

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 07 Jun 2013, 06:01 AM
Hi Lisa,

The Condition 'if (e.Item is GridEditableItem && e.Item.IsInEditMode)' fires only when the RadGrid is in edit/insert mode. One of your requirement is to display 2 radio buttons without making the user click on any link (such as Edit). But you have put the RadioButtonList inside EditItemTemplate. One suggestion is to show the Radgrid in edit mode on PageLoad. Another way is to put the RadioButtonList in ItemTemplate of GridTemplateColumn.

Thanks,
Shinu.
0
Lisa
Top achievements
Rank 1
answered on 18 Jun 2013, 02:33 AM
Thank you.
Tags
Grid
Asked by
Lisa
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Lisa
Top achievements
Rank 1
Share this question
or