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

RadGrid with RadComboBox returning ItemIndex 0

5 Answers 181 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joh
Top achievements
Rank 1
Joh asked on 26 Aug 2011, 08:09 PM
I have a RadGrid which has RadComboBox which are selectable without edit mode and hence the call to SelectedIndexChanged fires correctly just can't seem to extract the index of the item.  Yet when I turn the RadComboBox to a regular asp DropDownList it works fine.


<telerik:GridTemplateColumn DataField="CLASS" SortExpression="SEP_CW_CRSE_CLASS"
                UniqueName="SEP_CW_CRSE_CLASS">
                <ItemTemplate>
                    <asp:Label ID="lbCRSEClass" runat="server" Visible="false" Text='<%# Eval("SEP_CW_CRSE_CLASS") %>' />
                    <asp:DropDownList ID="rdbCRSEClass" runat="server" Width="75px"
                    OnSelectedIndexChanged="rdbCRSEClass_SelectedIndexChanged" AutoPostBack="true"
                    CommandArgument='<%# ((GridItem)Container).ItemIndex %>' />
                    <%--<telerik:RadComboBox ID="rdbCRSEClass" runat="server" OnSelectedIndexChanged="rdbCRSEClass_SelectedIndexChanged"
                        AutoPostBack="true" CommandArgument='<%# DataBinder.Eval(Container,"ItemIndex") %>'>
                    </telerik:RadComboBox>--%>
                    <%--<telerik:RadComboBox ID="rdbCRSEClass" runat="server" OnSelectedIndexChanged="rdbCRSEClass_SelectedIndexChanged"
                        AutoPostBack="true" CommandArgument='<%# ((GridItem)Container).ItemIndex %>'>
                    </telerik:RadComboBox>--%>
                </ItemTemplate>
            </telerik:GridTemplateColumn>

Code Behind:
//RadComboBoxSelectedIndexChangedEventArgs
    protected void rdbCRSEClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        string cmdArg = ((DropDownList)sender).Attributes["CommandArgument"];
        //string cmdArg = ((RadComboBox)sender).Attributes["CommandArgument"];
        int ItemIndex = Convert.ToInt32(cmdArg);
        DropDownList rdbGEGRAD = (DropDownList)(gvClassTERM.Items[ItemIndex].FindControl("rdbGEGRAD"));
        DropDownList rdbCRSEClass = (DropDownList)(gvClassTERM.Items[ItemIndex].FindControl("ddltCRSEClass"));
}

The CommandArgument works fine with dropdownlist but not RadCombo? I need to change the values on the fly rather then having to go to edit mode.  Any help would be appreciated.

- Thank You

5 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 26 Aug 2011, 09:28 PM

Hello,


protected void rdbCRSEClass_indexchanged(object sender, EventArgs e)
   {
       DropDownList rdbCRSEClass = (DropDownList)sender;
GridDataItem item = (GridDataItem)rdbCRSEClass.NamingContainer;
       DropDownList rdbGEGRAD = (DropDownList)item.FindControl("rdbGEGRAD");
       //populate 'rdbGEGRAD'
   }

Thanks,
Jayesh Goyani

0
Joh
Top achievements
Rank 1
answered on 26 Aug 2011, 11:05 PM
The first RadComboBox loops through all the dropdowns which is "OK" but the second RadComboBox fails to get selected value and defaults to the preselected value.

<telerik:GridTemplateColumn DataField="SEP_CW_CRSE_CLASS" SortExpression="SEP_CW_CRSE_CLASS"
                UniqueName="SEP_CW_CRSE_CLASS">
                <ItemTemplate>
                    <asp:Label ID="lbCRSEClass" runat="server" Visible="false" Text='<%# Eval("SEP_CW_CRSE_CLASS") %>' />
                    <telerik:RadComboBox ID="rdbCRSEClass" runat="server" OnSelectedIndexChanged="rdbCRSEClass_SelectedIndexChanged"
                        AutoPostBack="true" CommandArgument='<%# DataBinder.Eval(Container,"ItemIndex") %>'
                        Enabled="true">
                    </telerik:RadComboBox>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn DataField="SEP_CW_GEGRAD" SortExpression="SEP_CW_GEGRAD"
                UniqueName="SEP_CW_GEGRAD">
                <ItemTemplate>
                    <asp:Label ID="lbGEGrad" runat="server" Visible="false" Text='<%# Eval("SEP_CW_GEGRAD") %>' />
                    <telerik:RadComboBox ID="rdbGEGRAD" runat="server" OnSelectedIndexChanged="rdbGEGRAD_SelectedIndexChanged"
                        AutoPostBack="true" CommandArgument='<%# DataBinder.Eval(Container,"ItemIndex") %>'
                        Enabled="true">
                    </telerik:RadComboBox>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
And the Code behind:
//This works:
    protected void rdbCRSEClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadComboBox rdbCRSEClass = (RadComboBox)sender;
        GridDataItem item = (GridDataItem)rdbCRSEClass.NamingContainer;
        RadComboBox rdbGEGRAD = (RadComboBox)item.FindControl("rdbGEGRAD");
 
        Label lbTERMCode = (Label)item.FindControl("lbTERMCode");
        Label lbSubJCode = (Label)item.FindControl("lbSubJCode");
        Label lbCrseNumb = (Label)item.FindControl("lbCrseNumb");
        string termCode = lbTERMCode.Text;
        string SubJCode = lbSubJCode.Text;
        string CrseNumb = lbCrseNumb.Text;
 
        if (rdbGEGRAD != null)
        {
            if (rdbCRSEClass.SelectedValue == "GE")
            {
                 
                rdbGEGRAD.Visible = true;
                rdbGEGRAD.DataTextField = "SEP_GEGRAD_CODE";
                rdbGEGRAD.DataValueField = "SEP_GEGRAD_ATTR_CODE";
                rdbGEGRAD.DataSource = getGEData();
                rdbGEGRAD.DataBind();               
            }
            else
            {
                rdbGEGRAD.Visible = false;
            }
        }
        rdbCRSEUpdate(rdbCRSEClass.SelectedValue, termCode, SubJCode, CrseNumb);       
    }
 
//This runs only once and never gets the new selected value (keeps the old value):
 protected void rdbGEGRAD_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadComboBox rdbGEGRAD = (RadComboBox)sender;
        GridDataItem item = (GridDataItem)rdbGEGRAD.NamingContainer;       
 
        Label lbTERMCode = (Label)item.FindControl("lbTERMCode");
        Label lbSubJCode = (Label)item.FindControl("lbSubJCode");
        Label lbCrseNumb = (Label)item.FindControl("lbCrseNumb");
        string termCode = lbTERMCode.Text;
        string SubJCode = lbSubJCode.Text;
        string CrseNumb = lbCrseNumb.Text;
 
        rdbGEGradUpdate(rdbGEGRAD.SelectedValue, termCode, SubJCode, CrseNumb);
    }
0
Jayesh Goyani
Top achievements
Rank 2
answered on 27 Aug 2011, 08:59 AM
Hello,

Try to get SelectedIndex of Dropdown and let me know it works or not.

If its not worked then please let me know which type of functionality you want in your RADGRID.
so i will send code snippet of this.


Thanks,
Jayesh Goyani
0
Joh
Top achievements
Rank 1
answered on 28 Aug 2011, 10:19 PM
No doesn't work out, thanks for your help by the way.

This is the scenario:

Info: This RadGrid is inside of a WebUserControl which gets loaded dynamically via the RadTabs and MultiPage inside of a asp:pannel. This part works perfectly when going from tab to tab.

The issue is regarding when the page loads it issues a postback since it is dynamically loaded which is fine.  Now instead of allowing the users to edit all the fields I have chosen to have the RadComboBox active on the page so users can go through the whole form and select the options quickly rather then editing each field.  I guess I can throw this into edit mode and disable all the other fields but that doesn't look as nice since it will be textboxes rather then labels. So a break down of this is like so:

1. User gets to page each grid item has two RadComboBoxes,  the first RadComboBoxe  determins if the second one shows or not (this part works fine).  My original design was on a RadListView but I ran into issues of trying to group the headers and footers which would disappear sometimes. So when the user selects the first RadComboBox it loops through each combobox and sets it's state.  No matter what I feed it I can't seem to get the DataItemIndex/ItemIndex back.  It will just loop through each one of those first RadComboBoxes which is "ok" but I rather only update what is needed to be changed.

2. The second RadComboBox with the same code does not loop through each option it only goes to the first one and nothing else happens hence doesn't work out for me.  It tried the code you gave and it did the same as my previous code.  Doesn't matter if it is an EventArgs or RadComboBoxSelectedIndexChangedEventArgs.  As you can see in the last posted code... Perhaps the RadComboBox doesn't accept CommandArgument as a command like a dropdownbox does.  Because with checking I put in labels that returned the correct value for each of them.  If not how can I get the <%# DataBinder.Eval(Container,"ItemIndex") %>
or <%# ((GridItem)Container).ItemIndex %> since I can't do it with CommandArgs or any other method tried so far. 

Again thanks for your hep and support.
0
Veli
Telerik team
answered on 01 Sep 2011, 07:26 AM
Hello Joh,

As Jayesh showed in his first post in the  thread, you use the NamingContainer object of the combo. The naming container of any control in a grid item is the grid item itself. This is how you retrieve properties of the parent grid item from any control event:

protected void rdbCRSEClass_SelectedIndexChanged(object sender, EventArgs e)
{
    RadComboBox combo = (RadComboBox)sender;
    GridDataItem parentItem =(GridDataItem)combo.NamingContainer;
    int itemIndex = parentItem.ItemIndex;  
}

Veli
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
Grid
Asked by
Joh
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Joh
Top achievements
Rank 1
Veli
Telerik team
Share this question
or