How to uncheck RadioButtonList when "+ Add new record" button is clicked

6 posts, 0 answers
  1. ILJIMAE
    ILJIMAE avatar
    10 posts
    Member since:
    May 2012

    Posted 10 May 2012 Link to this post

    Hello,

    I have a 'record update' feature enabled grid (2012 Q1).
    I am using <FormTemplate> for editing/updating the data on the grid.
    In the FormTemplate, I have a RadioButtonList named 'rdGrade' which is bound to the 'SchoolGrade' field in my database. The code for this RadioButtonList is as shown below.

    <asp:RadioButtonList ID="rdGrade" runat="server"
    SelectedValue='<%# Bind("SchoolGrade") %>'
    DataSource='<%# (new string[] { "4", "5", "6" }) %>'
    AppendDataBoundItems="True"
    RepeatDirection="Horizontal">
    </asp:RadioButtonList>

    Editing the SchoolGrade field with this RadioButtonList was fine. No errors.
    But when I try to Add a new record by clicking the "+ Add new record" button, I get an error. So I had to add some extra code to the code behind page to set the default selected value of this RadioButtonList and the code I added was:

    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
         {
                if (e.CommandName == RadGrid.InitInsertCommandName)
                {
                    //Add new" button clicked
                    e.Canceled = true;
                    //Prepare an IDictionary with the predefined values
                    System.Collections.Specialized.ListDictionary newValues = new
                    System.Collections.Specialized.ListDictionary();
                    newValues["SchoolGrade"] = 5;
                    //Insert the item and rebind
                    e.Item.OwnerTableView.InsertItem(newValues);
                }
            }

    The code above sets the default selected value of the radio button list as 5.
    My question is how do I change the code line

    newValues["SchoolGrade"] = 5;

     

    in order to initially UNCHECK the RadioButtonList rdGrade when a user clicks the 'Add new record' button on the grid?

    Thank you.


  2. Jayesh Goyani
    Jayesh Goyani avatar
    2630 posts
    Member since:
    May 2010

    Posted 11 May 2012 Link to this post

    Hello ILJIMAE,

    Please check below code snippet.

    Method 1.

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
     
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem editItem = e.Item as GridEditFormItem;
                RadioButtonList rblstudentGender = (RadioButtonList)editItem.FindControl("rblGender");
                string strSchoolGrade =Convert.ToString(editItem.GetDataKeyValue("SchoolGrade"));
                if (!string.IsNullOrEmpty(strSchoolGrade))
                {
                    // Edit MOde
                    rblstudentGender.SelectedValue = strSchoolGrade;
                    //OR
                    if (rblstudentGender.Items.FindByValue("strSchoolGrade") != null)
                    {
                        rblstudentGender.Items.FindByValue("strSchoolGrade").Selected = true;
                    }
     
                }
                else
                {
                    // INsert Mode
                    if (rblstudentGender.Items.FindByValue("5") != null)
                    {
                        rblstudentGender.Items.FindByValue("5").Selected = true;
                    }
                }
            }

    Remove Below property.
    SelectedValue='<%# Bind("SchoolGrade") %>'

    OR

    Method 2.
    SelectedValue='<%# (Container is GridEditFormInsertItem) ? "5" :  Eval("SchoolGrade") %>'





    Thanks,
    Jayesh Goyani

  3. ILJIMAE
    ILJIMAE avatar
    10 posts
    Member since:
    May 2012

    Posted 11 May 2012 Link to this post

    I really like the Method 2 because it is shorter, but what I actually wanted was,
    I wanted a de-selected/unchecked RadioButtonList to be shown on the record inserting form when a user clicks the '+Add new record' button. So is it possible to amend the code shown below to have a RadioButtonList with none of the school grade selected? (I don't want the RadioButtonList to select Grade 5 option by default)

    SelectedValue='<%# (Container is GridEditFormInsertItem) ? "5" :  Eval("SchoolGrade") %>'

    Thank you.

  4. Jayesh Goyani
    Jayesh Goyani avatar
    2630 posts
    Member since:
    May 2010

    Posted 11 May 2012 Link to this post

    Hello,

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
      
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem editItem = e.Item as GridEditFormItem;
                RadioButtonList rblstudentGender = (RadioButtonList)editItem.FindControl("rblGender");
                string strSchoolGrade =Convert.ToString(editItem.GetDataKeyValue("SchoolGrade"));
                if (!string.IsNullOrEmpty(strSchoolGrade))
                {
                    // Edit MOde
                    rblstudentGender.SelectedValue = strSchoolGrade;
                    //OR
                    if (rblstudentGender.Items.FindByValue("strSchoolGrade") != null)
                    {
                        rblstudentGender.Items.FindByValue("strSchoolGrade").Selected = true;
                    }
      
                }
            }
     
    }

    Remove below property from your code.
    SelectedValue='<%# Bind("SchoolGrade") %>'

    Add Below Code.
    <MasterTableView  DataKeyNames="SchoolGrade">


    Thanks,
    Jayesh Goyani

  5. ILJIMAE
    ILJIMAE avatar
    10 posts
    Member since:
    May 2012

    Posted 12 May 2012 Link to this post

    Hmm... I wanted to be able to use the Grid to Edit the records as well as Add new records.

    The code line shown below removes the School Grade binding from the Add new record form but it also removes the binding from the record Edit form <FormTemplate>. I need to show school grade information in the record editing form but I want to have initially unchecked School Grade RadioButtonList in the Add new record form.

    SelectedValue='<%# Bind("SchoolGrade") %>'

    Is there another way to solve this problem?

  6. Jayesh Goyani
    Jayesh Goyani avatar
    2630 posts
    Member since:
    May 2010

    Posted 13 May 2012 Link to this post

    Hello,

    Are you able to get/access your RadioButtonList in below code.
    Because by below code you are able to set value in editform.

    if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem editItem = e.Item as GridEditFormItem;
                RadioButtonList rblstudentGender = (RadioButtonList)editItem.FindControl("rblGender");
                string strSchoolGrade =Convert.ToString(editItem.GetDataKeyValue("SchoolGrade"));
                if (!string.IsNullOrEmpty(strSchoolGrade))
                {
                    // Edit MOde
                    rblstudentGender.SelectedValue = strSchoolGrade;
                    //OR
                    if (rblstudentGender.Items.FindByValue("strSchoolGrade") != null)
                    {
                        rblstudentGender.Items.FindByValue("strSchoolGrade").Selected = true;
                    }
       
                }
            }


    Thanks,
    Jayesh Goyani

Back to Top