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

Search in RadComboBox (inside RadGrid) on asp button click

2 Answers 111 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Priyanka
Top achievements
Rank 1
Priyanka asked on 03 Sep 2015, 04:59 AM

Hi there is a RadComboBox inside EditItemTemplate of RadGrid along with a Button.

I am using _ItemsRequested event just to do the search in RadComboBox but not loading the whole list into it on click of RadComboBox. 

Current functionality is:
When user click on textarea of RadComboBox, type/key-in any thing,  then only the Combo binds the searched related Items into it.

Now I want to get below functionality on button click:
when user click on textarea of RadComboBox, type/key-in any thing, and click on button..then only the Combo shall bind the searched related Items into it.

Please someone reply how to achieve it ? 

Below is code of my Current functionality:

<telerik:RadGrid ID="RGGSTAcCode" runat="server"
               ShowFooter="True" GroupingEnabled="False" ShowStatusBar="true" EmptyDataText="No record available."
               AllowAutomaticInserts="False" AllowAutomaticUpdates="False" AllowAutomaticDeletes="true"
               OnNeedDataSource="RGGSTAcCode_NeedDataSource" OnItemDataBound="RGGSTAcCode_ItemDataBound"
               OnInsertCommand="RGGSTAcCode_InsertCommand" OnDeleteCommand="RGGSTAcCode_DeleteCommand"
               OnUpdateCommand="RGGSTAcCode_UpdateCommand" OnItemCommand="RGGSTAcCode_ItemCommand">
              <mastertableview ShowHeadersWhenNoRecords="true" autogeneratecolumns="false" datakeynames="AccountCodeID" InsertItemDisplay="Top"
                insertitempageindexaction="ShowItemOnCurrentPage" ShowFooter="True" CommandItemDisplay="Top" ClientIDMode="Static">                                  
                     <Columns>
                         <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn"></telerik:GridEditCommandColumn>
 
                         <telerik:GridBoundColumn DataField="AccountCodeID" HeaderText="AccountCode ID"
                           UniqueName="AccountCodeID" ReadOnly="True">                                          
                         </telerik:GridBoundColumn>                              
 
                         <telerik:GridTemplateColumn UniqueName="AccountCode" HeaderText="Account Code">
                            <ItemTemplate>
                              <asp:Label ID="lblAcCode" runat="server" Text='<%# Eval("AccountCode")%>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                               <asp:Label ID="lblAcCode2" runat="server" Text='<%# Eval("AccountCode") + " - " + Eval("AccountDescription")%>' Visible="false"></asp:Label>
                               <telerik:RadComboBox ID="ddlAccountCode" runat="server" Height="200" Width="240" DropDownWidth="310" HighlightTemplatedItems="true" CausesValidation="true"      
                                   OnItemsRequested="ddlAccountCode_ItemsRequested" EnableItemCaching="true" ShowDropDownOnTextboxClick="false"
                                   EnableLoadOnDemand="True" ShowMoreResultsBox="true" EnableVirtualScrolling="true" MarkFirstMatch="True"
                                   Filter="Contains" AppendDataBoundItems="true" DataTextField="AccountDescription" DataValueField="AccountCodeID">
                               </telerik:RadComboBox>
                                <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click"/>
                            </EditItemTemplate>
                         </telerik:GridTemplateColumn>
 
                         <telerik:GridButtonColumn ConfirmTextFormatString="Are you sure you want to Delete {0} Account Code?" ConfirmTextFields="AccountCodeID"
                         ConfirmDialogType="RadWindow" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn"></telerik:GridButtonColumn>                                                                           
                  </Columns>
                  <EditFormSettings>
                     <EditColumn ButtonType="ImageButton" />
                  </EditFormSettings>
                  <CommandItemSettings AddNewRecordText="Add new record" RefreshText="Refresh"></CommandItemSettings>
              </mastertableview>
            </telerik:RadGrid>
public DataTable GetAccCode(string CompanyCode)
{
    SqlConnection con = new SqlConnection(strcon);
    SqlCommand cmd = new SqlCommand("[Invoice].[usp_tbl_AccountCode_DL_Test]", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@CompanyCode", CompanyCode);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    try
    {
        con.Open();
        da.Fill(dt);
        con.Close();
    }
    catch (Exception ex)
    {
    }
    return dt;
}
 
#region Load on Demand
private const int ItemsPerRequest = 50;
 
private static string GetStatusMessage(int offset, int total)
{
    if (total <= 0)
    {
        return "No matches";
    }
    else
    {
        return String.Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", offset, total);
    }
}
 
protected void ddlAccountCode_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
        //Allow only search in RadComboBox, do not load whole List item initially on combobox click
        RadComboBox combo = (RadComboBox)sender;
        string c = ddlCompany.SelectedValue.ToString();
        DataTable dt = new DataTable();
        string txt = e.Text;
 
        int itemOffset=0;
        int endOffset=0;
       
        if (txt == String.Empty)
        {
            combo.ShowDropDownOnTextboxClick = false;
        }
        else
        {
            dt = GetAccCode(c); //got all Items related to selcted company in dt
            DataView dv = new DataView(dt);
            dv.RowFilter = string.Format("AccountDescription LIKE '%{0}%'", txt);
            dt = dv.ToTable();
 
            int a = dv.Count; //get the filtered/searched items
            if (dv.Count > 0)
            {
                itemOffset = e.NumberOfItems;
                endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
                e.EndOfItems = endOffset == dt.Rows.Count;
            }
            else if (a <= 0)
            {
                itemOffset = e.NumberOfItems;
                endOffset = Math.Min(a, a);
                e.EndOfItems = endOffset == a;
            }
        }
 
        //code adds/bind the records inside Combo
        for (int i = itemOffset; i < endOffset; i++)
        {
            combo.Items.Add(new RadComboBoxItem(dt.Rows[i]["AccountDescription"].ToString(), dt.Rows[i]["AccountDescription"].ToString()));
        }
 
        //If search/key-in text is not empty, show only searched records no. in footer of Combo
        if (!string.IsNullOrEmpty(e.Text))
        {
            DataView dv = new DataView(dt);
            int num = dv.Count;
            endOffset = dv.Count;
        }
        else
        {
            combo.ShowDropDownOnTextboxClick = false;
        }
        e.Message = GetStatusMessage(endOffset, dt.Rows.Count);   
}
#endregion
 
protected void btnSearch_Click(object sender, EventArgs e)
{
 
}
 

 

 

 

2 Answers, 1 is accepted

Sort by
0
Priyanka
Top achievements
Rank 1
answered on 08 Sep 2015, 03:11 AM

Below code solved my query.
Added the _ItemRequested event code inside Button_Click event with few modifications and its working fine now.

protected void ddlAccountCode_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
    RadComboBox combo = (RadComboBox)sender;
    combo.ShowDropDownOnTextboxClick = false;
    combo.Items.Clear();
 
    Session["Text"] = e.Text;
    Session["NumberOfItems"] = e.NumberOfItems;
}
 
protected void btnSearch_Click(object sender, EventArgs e)
{
    GridEditableItem editedItem = (sender as Button).NamingContainer as GridEditableItem;
    RadComboBox combo = (RadComboBox)editedItem.FindControl("ddlAccountCode");
    combo.Items.Clear();
    combo.OpenDropDownOnLoad = true;
    combo.HighlightTemplatedItems = true;
 
    string c = ddlCompany.SelectedValue.ToString(); //get the selected company name
    string txt = Session["Text"].ToString();
 
    DataTable dt = new DataTable();   
    dt = GetAccCode(c);
    DataView dv = new DataView(dt);
    dv.RowFilter = string.Format("AccountDescription LIKE '%{0}%'", txt);
    int a = dv.Count;
    if (dv.Count > 0)
    {
        dt = dv.ToTable();
    }
 
    int itemOffset = Convert.ToInt32(Session["NumberOfItems"]);
    int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
    Session["NumberOfItems"] = endOffset == dt.Rows.Count;
 
    for (int i = itemOffset; i < dv.Count; i++)
    {
        combo.Items.Add(new RadComboBoxItem(dt.Rows[i]["AccountDescription"].ToString(), dt.Rows[i]["AccountDescription"].ToString()));
    }
 
    Label lbl = (Label)combo.Footer.FindControl("lblRadComboFooter");
    lbl.Text = GetStatusMessage(endOffset, dt.Rows.Count);
 
    combo.DataBind();
}

<EditItemTemplate>
 
<asp:Label ID="lblAcCode2" runat="server" Text='<%# Eval("AccountCode") + " - " + Eval("AccountDescription")%>' Visible="false"></asp:Label>
<telerik:RadComboBox ID="ddlAccountCode" runat="server" Height="200" Width="240" DropDownWidth="310"      
 EnableLoadOnDemand="True" OnItemsRequested="ddlAccountCode_ItemsRequested" EnableItemCaching="true" 
 ShowMoreResultsBox="True" EnableVirtualScrolling="true" AllowCustomText="true" MarkFirstMatch="true"
 Filter="Contains" HighlightTemplatedItems="true" CausesValidation="true" AppendDataBoundItems="true"
 DataTextField="AccountDescription" DataValueField="AccountCodeID"
 ShowDropDownOnTextboxClick="false"
 OnClientDropDownOpening="OnClientDropDownOpening" OnClientItemsRequested="OnClientItemsRequested">
 
   <FooterTemplate>       
      <table style="text-align:center">           
        <tr>              
          <td>
             <asp:Label ID="lblRadComboFooter" runat="server"></asp:Label>
          </td>                  
        </tr>       
      </table>   
   </FooterTemplate>
 
</telerik:RadComboBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click"/>
 
</EditItemTemplate>
0
Nencho
Telerik team
answered on 10 Sep 2015, 11:28 AM
Hello Priyanka,

Thank you for sharing your solution with the community.

Regards,
Nencho
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Priyanka
Top achievements
Rank 1
Answers by
Priyanka
Top achievements
Rank 1
Nencho
Telerik team
Share this question
or