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

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

5 Answers 227 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ILJIMAE
Top achievements
Rank 1
ILJIMAE asked on 11 May 2012, 03:00 AM

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.


5 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 11 May 2012, 06:55 AM
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
0
ILJIMAE
Top achievements
Rank 1
answered on 11 May 2012, 08:48 AM
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.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 11 May 2012, 09:19 AM
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
0
ILJIMAE
Top achievements
Rank 1
answered on 13 May 2012, 03:57 AM
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?

0
Jayesh Goyani
Top achievements
Rank 2
answered on 13 May 2012, 05:35 AM
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
Tags
Grid
Asked by
ILJIMAE
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
ILJIMAE
Top achievements
Rank 1
Share this question
or