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

Getting empty rows after PostBack

3 Answers 123 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Philip Senechal
Top achievements
Rank 1
Philip Senechal asked on 06 Apr 2011, 12:13 AM
I'm going to try to briefly describe the situation because the setup is a bit complicated

I have a RadGrid with popup form insert/edit.
When the popup form appears, the first object is a combobox that contains the Report ID.
The ItemDataBound event for the grid populates the popup form with the correct Report Options by building the controls and adding them to a Panel container. The Report Options are determined by the combobox that contains the Report ID

For example...if the Report ID is 1, the ItemDataBound event generates 2 comboboxes for Report Date and Report Format. Report Date has 4 options and Report Format has 2 options.

Now...the combobox for the Report ID is set to autopostback and there is a OnSelectedIndexChanged event that reruns the code that creates the Report Option controls.

For example...if I change the combobox so that the Report ID is 2, the ItemDataBound event only generates 1 combobox for Report Format which is supposed to only have 2 options.

Here's where the problem occurs. All of this works perfectly, except that when I select Report ID 2, the combobox for Report Format has 4 options in it...the last 2 are completely blank.

It seems that for some reason it's remembering that the first combobox when Report ID was selected had 4 options and even though it's changing the combobox to have the right values, it still thinks there is supposed to be 4 options when in fact there are only 2.

I tried doing a combo.Items.Clear() before building the combobox, but that didn't seem to work. I also tried clearing out the controls on the Panel and that didn't work either. You can also see from the code below that each combobox is created with a unique ID based on the ID of the Option. The Report Date combobox is generated as dd_1 while the Report Format combobox is generated as dd_8.

I guess I'm looking for a way to make sure the ComboBox has the correct number of options so that the user cannot select "blank" items.

I don't know how much help this code is going to be, but here is how I'm creating the controls

protected void BuildOptions(GridEditFormItem editFormItem)
{
    RadComboBox dd_RptID = (RadComboBox)editFormItem.FindControl("dd_RptID");
 
    EISDataContext db = new EISDataContext();
 
    // get the unique report options for the current selected report so we can render the correct controls
    var q_options = (from options in db.tReportConfigs
                     where options.RptID == dd_RptID.SelectedItem.Value
                     select options.OptID).Distinct();
 
    foreach (var option in q_options)
    {
        string optionType = (from options in db.tReportOptions
                             where options.OptID == option
                             select options.OptTyp).FirstOrDefault();
        string optionName = (from options in db.tReportOptions
                             where options.OptID == option
                             select options.OptNm).FirstOrDefault();
 
        // get the option values for this option
        var q_optionValues = from optionvalues in db.tReportConfigs
                             where optionvalues.RptID == dd_RptID.SelectedItem.Value && optionvalues.OptID == option
                             select optionvalues;
 
        // create a div for the option control and populate it with the correct control
        Panel panelContent = new Panel();
        panelContent.Attributes.Add("class", "popupcontent");
 
        if (optionType == "ComboBox")
        {
            RadComboBox combo = new RadComboBox();
            combo.ID = "dd_" + option.ToString();
            foreach (var optionValue in q_optionValues)
            {
                combo.Items.Add(new RadComboBoxItem(optionValue.tReportOptionValue.OptNm, optionValue.tReportOptionValue.OptVal));
            }
 
            panelContent.Controls.Add(combo);
        }
 
        // create a div for the option title and populate it with the option name
        Panel panelTitle = new Panel();
        panelTitle.Attributes.Add("class", "popuptitle");
        Label pnlLabel = new Label();
        pnlLabel.Text = optionName;
        panelTitle.Controls.Add(pnlLabel);
 
        // create a div for the clear control
        Panel panelClear = new Panel();
        panelClear.Attributes.Add("class", "clear");
 
        editFormItem.FindControl("divOptions").Controls.Add(panelTitle);
        editFormItem.FindControl("divOptions").Controls.Add(panelContent);
        editFormItem.FindControl("divOptions").Controls.Add(panelClear);
    }
}

Any suggestions you can provide are greatly appreciated...thanks.

3 Answers, 1 is accepted

Sort by
0
Dimitar Terziev
Telerik team
answered on 11 Apr 2011, 04:31 PM
Hi Philip,

You issues seems very strange, so in order to troubleshoot it  I would need more information.

Provide us with the implementation of the SelectedIndexChanged event handler function so I could examine it. Also could you verify that you haven't got empty rows in the data source causing to have these empty items.

Could you verify that EnableViewState property of the RadGrid is set to False? If this property is not False, ItemDataBound won't be fired upon post-back.

Regards,
Dimitar Terziev
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Philip Senechal
Top achievements
Rank 1
answered on 11 Apr 2011, 06:59 PM
Certainly...here is the EditFormSettings for the RadGrid where the ComboBox with the SelectedIndexChanged event is configured
<EditFormSettings EditFormType="Template" InsertCaption="Add a New Schedule" CaptionFormatString="Edit Schedule: {0}"
    CaptionDataField="SchID" PopUpSettings-Width="500px">
    <FormTemplate>
        <div class="popuptitle">
            Report Name:
        </div>
        <div class="popupcontent">
            <telerik:RadComboBox ID="dd_RptID" Skin="Default" Font-Size="11px" ShowToggleImage="true"
                MarkFirstMatch="true" AppendDataBoundItems="true" OnSelectedIndexChanged="dd_RptID_SelectedIndexChanged"
                AutoPostBack="true" runat="server" />
        </div>
        <div class="clear">
        </div>
        <div class="popuptitle">
            Start Date:
        </div>
        <div class="popupcontent">
            <telerik:RadDatePicker ID="dp_SchStDt" Skin="Default" DateInput-Font-Size="11px"
                ShowPopupOnFocus="true" Calendar-ShowOtherMonthsDays="false" Calendar-ShowRowHeaders="false"
                runat="server" />
        </div>
        <div class="clear">
        </div>
        <div id="divOptions" runat="server">
        </div>
        <div class="clear">
        </div>
        <div class="popupbuttons">
            <asp:ImageButton ID="btn_ReqSubmit" ImageUrl='<%# (Container is GridEditFormInsertItem) ? "~/App_Themes/InfoSource/Images/ico_add_24.png" : "~/App_Themes/InfoSource/Images/ico_update_24.png" %>'
                CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'
                runat="server" />
        </div>
        <div class="popupbuttons">
            <asp:LinkButton ID="lbl_ReqSubmit" Text='<%# (Container is GridEditFormInsertItem) ? "Add Schedule" : "Update Schedule" %>'
                CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'
                runat="server" />
        </div>
        <div class="popupbuttons">
                  
            <asp:ImageButton ID="btn_ReqCancel" ImageUrl="~/App_Themes/InfoSource/Images/ico_cancel_24.png"
                CommandName="Cancel" runat="server" />
        </div>
        <div class="popupbuttons">
            <asp:LinkButton ID="lbl_ReqCancel" Text="Cancel" CommandName="Cancel" runat="server" />
        </div>
    </FormTemplate>
</EditFormSettings>

and here is the code behind for that event
protected void dd_RptID_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    RadComboBox combo = (RadComboBox)sender;
 
    GridEditFormInsertItem editFormItem = (GridEditFormInsertItem)combo.NamingContainer;
    BuildOptions(editFormItem);
}

I did not have EnableViewState set to false, however, when I attempted that, the postback after changing the ComboBox to another value doesn't save that change and returns me to the first selection the list again. So, if Call Response is the first item in the ComboBox and I use the dropdown to change to Call Detail, when the popup does a postback, I'm returned to Call Response again.

Thank you for your assistance...please let me know if you need anything to help troubleshoot this.

If you need the code for the BuildOptions function, here it is:

protected void BuildOptions(GridEditFormItem editFormItem)
{
    RadComboBox dd_RptID = (RadComboBox)editFormItem.FindControl("dd_RptID");
 
    EISDataContext db = new EISDataContext();
 
    // get the unique report options for the current selected report so we can render the correct controls
    var q_options = (from options in db.tReportConfigs
                     where options.RptID == dd_RptID.SelectedItem.Value
                     select options.OptID).Distinct();
 
    foreach (var option in q_options)
    {
        string optionType = (from options in db.tReportOptions
                             where options.OptID == option
                             select options.OptTyp).FirstOrDefault();
        string optionName = (from options in db.tReportOptions
                             where options.OptID == option
                             select options.OptNm).FirstOrDefault();
 
        // get the option values for this option
        var q_optionValues = from optionvalues in db.tReportConfigs
                             where optionvalues.RptID == dd_RptID.SelectedItem.Value && optionvalues.OptID == option
                             select optionvalues;
 
        // create a div for the option control and populate it with the correct control
        Panel panelContent = new Panel();
        panelContent.Attributes.Add("class", "popupcontent");
 
        if (optionType == "ComboBox")
        {
            RadComboBox combo = new RadComboBox();
            combo.ID = "dd_" + option.ToString();
            combo.Font.Size = 8;
 
            foreach (var optionValue in q_optionValues)
            {
                combo.Items.Add(new RadComboBoxItem(optionValue.tReportOptionValue.OptNm, optionValue.tReportOptionValue.OptVal));
            }
 
            panelContent.Controls.Add(combo);
        }
        if (optionType == "DatePicker")
        {
            RadDatePicker dp = new RadDatePicker();
            dp.ID = "dp_" + option.ToString();
 
            panelContent.Controls.Add(dp);
        }
 
        // create a div for the option title and populate it with the option name
        Panel panelTitle = new Panel();
        panelTitle.Attributes.Add("class", "popuptitle");
        Label pnlLabel = new Label();
        pnlLabel.Text = optionName + ":";
        panelTitle.Controls.Add(pnlLabel);
 
        // create a div for the clear control
        Panel panelClear = new Panel();
        panelClear.Attributes.Add("class", "clear");
 
        editFormItem.FindControl("divOptions").Controls.Add(panelTitle);
        editFormItem.FindControl("divOptions").Controls.Add(panelContent);
        editFormItem.FindControl("divOptions").Controls.Add(panelClear);
        }
    }
}
0
Dimitar Terziev
Telerik team
answered on 18 Apr 2011, 02:36 PM
Hello Philip,

The problem you are experiencing is related  to the fact that you fill your RadComboBox with items and then add it to the controls collection of the page. Once the RadComboBox is added to the page its ViewState is loaded, neglecting the changes made before and thus you end with four instead of two items.

In order to deal with this problem you should add your RadComboBoxes to the controls collection first and then populate them with items.

As another approach you could set the EnableViewState = false property of the RadCombobox. This should fix the issue too.

Kind regards,
Dimitar Terziev
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
ComboBox
Asked by
Philip Senechal
Top achievements
Rank 1
Answers by
Dimitar Terziev
Telerik team
Philip Senechal
Top achievements
Rank 1
Share this question
or