Get SelectedValue of RadComboBox in FilterTemplate of GridTemplateColumn in RadGrid

10 posts, 1 answers
  1. Celeste
    Celeste avatar
    44 posts
    Member since:
    Oct 2010

    Posted 23 Nov 2010 Link to this post

    Dear Forum,

    Please assist! I have created a RadGrid with GridTemplateColumns where the FilterTemplates contain RadComboBoxes. I cannot figure out how to access the selected value or item of the RadComboBox in codebehind (C#) and I need to do this on the RadGrid's _ItemCommand event for this criteria if (e.CommandName == Telerik.Web.UI.RadGrid.FilterCommandName).

    No clue, and I've read everything I can find.

    Celeste

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 24 Nov 2010 Link to this post

    Hello,


    There is a documentation which shows how to use RadComoBox in FilrterTemplate of Grid. Hope this is what you are searching for.
    Filter template



    -Shinu.
  3. Celeste
    Celeste avatar
    44 posts
    Member since:
    Oct 2010

    Posted 24 Nov 2010 Link to this post

    Shinu,

    No, that is not what I am looking for. I've already read through and tried that. Please just re-read my post and if you can help it would be appreciated.

    Celeste
  4. Tsvetina
    Admin
    Tsvetina avatar
    2481 posts

    Posted 26 Nov 2010 Link to this post

    Hello Celeste,

    In this case, if you filter through firing a filter command, you can try accessing the RadComboBox in the following way:
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
     if (e.CommandName == RadGrid.FilterCommandName)
     {
       Pair filterPair = (Pair)e.CommandArgument;
       RadComboBox filterBox = (e.Item as GridFilteringItem)[filterPair.Second.ToString()].Controls[0] as RadComboBox;
       string value = filterBox.SelectedValue;
     }


    All the best,
    Tsvetina
    the Telerik team
    Browse the vast support resources we have to jumpstart 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.
  5. Celeste
    Celeste avatar
    44 posts
    Member since:
    Oct 2010

    Posted 29 Nov 2010 Link to this post

    Tsvetina,

    Yes, I tried that approach but the RadComboBox filterBox is null after that line of code, so I get the "Object reference not set to an instance of an object" error.

    Celeste
  6. Tsvetina
    Admin
    Tsvetina avatar
    2481 posts

    Posted 30 Nov 2010 Link to this post

    Hi Celeste,

    Could you please share the code that you are using to set up the described scenario - the filter template, combo box selection handling, etc, so we can provide you with a solution relevant to your specific scenario?

    Greetings,
    Tsvetina
    the Telerik team
    Browse the vast support resources we have to jumpstart 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.
  7. Celeste
    Celeste avatar
    44 posts
    Member since:
    Oct 2010

    Posted 30 Nov 2010 Link to this post

    Here is the code for the filter template and  combo box selection handling :

    <

     

    telerik:GridTemplateColumn DataField="AccountName" HeaderText="Account Name"

     

     

    UniqueName="AccountName" GroupByExpression="AccountName Group By AccountName"

     

     

    SortExpression="AccountName" >

     

     

    <FilterTemplate>

     

     

    <telerik:RadComboBox ID="rcbAccountName" DataSourceID="SDSAccountName"

     

     

    EnableTheming="false" DataTextField="AccountName" Font-Size="X-Small"

     

     

    DataValueField="AccountName" Height="200px" AppendDataBoundItems="true"

     

     

    SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("AccountName").CurrentFilterValue %>'

     

     

    runat="server" OnClientSelectedIndexChanged="AccountNameChanged"

     

     

    EnableAutomaticLoadOnDemand="False" >

     

     

    <Items>

     

     

    <telerik:RadComboBoxItem EnableTheming="false" Font-Size="X-Small" />

     

     

    </Items>

     

     

    </telerik:RadComboBox>

     

     

    <telerik:RadScriptBlock ID="rsbAccountName" runat="server">

     

     

    <script type="text/javascript">

     

     

    function AccountNameChanged(sender, args) {

     

     

    var tableView=$find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");

     

    tableView.filter(

    "AccountName", args.get_item().get_value(), "EqualTo");

     

    }

     

    </script>

     

     

    </telerik:RadScriptBlock>

     

     

    </FilterTemplate>

     

     

    <ItemTemplate>

     

    <%

    # Eval("AccountName")%>

     

     

    </ItemTemplate>

     

     

    </telerik:GridTemplateColumn>

    What I need to do with the information is construct  a list of the filter expressions for the user -- they get added to a bulleted list:

     

     protected void rgAccounts_ItemCommand(object source, GridCommandEventArgs e)
            {

             
                string msg;
                if (e.CommandName == Telerik.Web.UI.RadGrid.FilterCommandName)
                {
                    Pair filterPair = (Pair)e.CommandArgument;
                    msg = String.Concat("Filter: '", filterPair.First, "' for ", filterPair.Second, "; ");
          

                 RadComboBox thisRCB = ((RadComboBox)(e.Item as GridFilteringItem)[filterPair.Second.ToString()].Controls[0]);
                 string strThisSelectedItem = thisRCB.SelectedItem.ToString();
                 string strThisSelectedValue = thisRCB.SelectedValue.ToString();
                 string strThisText = thisRCB.Text;
                 string filterPattern = ((TextBox)(e.Item as GridFilteringItem)[filterPair.Second.ToString()].Controls[0]).Text;

                     
                 msg = String.Concat(msg, "Search Pattern: '", filterPattern, "'");

                 // if there is an item in the list that starts with "Filter: " and the same first 11 chars of filterPair.first, then delete it
                 String filterColumn = filterPair.Second.ToString();

                 for (int i = 0; i < blistMessages.Items.Count; i++)
                 {
                  ListItem li = blistMessages.Items[i];
                  if (li.Text.Contains(filterPair.Second.ToString()))
                  {
                          blistMessages.Items.Remove(li);
                         }
                    }
                    AddMessageToList(this.rgAccounts.MasterTableView.FilterExpression);
                }
            }

  8. Tsvetina
    Admin
    Tsvetina avatar
    2481 posts

    Posted 03 Dec 2010 Link to this post

    Hi Celeste,

    Have you tried debugging your code in item command to see what is inside the Controls collection of the filtering item? I created a sample grid with your template column declaration and the RadComboBox was actually in (e.Item as GridFilteringItem)["ColumnUnuiqueName"].Controls[1], not Controls[0]. The other option is to try and use FindControl() in order to get a reference to the combo.

    Best wishes,
    Tsvetina
    the Telerik team
    Browse the vast support resources we have to jumpstart 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.
  9. Celeste
    Celeste avatar
    44 posts
    Member since:
    Oct 2010

    Posted 03 Dec 2010 Link to this post

    Tsvetina,

    Thank you, Controls[1] resolved the issue. But I would appreciate if you could screenshot for me of how you found the controls collection when you debugged, try as I may, I could not work out how to drill into (e.Item as GridFilterinItem) and find the collection. Can you post a screenshot to show how to find the collection during debug?

    Thanks,
    Celeste
  10. Answer
    Tsvetina
    Admin
    Tsvetina avatar
    2481 posts

    Posted 06 Dec 2010 Link to this post

    Hi Celeste,

    You can easily examine the Controls collection by using the Quick Watch dialog box in Visual Studio by checking first the length of the collection and if it is larger than 0 - checking each index separately. You can read more on Quick Watch at that address. Additionally, I attach a screenshot demonstrating how I inspected the filtering item.

    Kind regards,
    Tsvetina
    the Telerik team
    Browse the vast support resources we have to jumpstart 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.
Back to Top