在asp按钮单击中搜索RadComboBox(在RadGrid内)

时间:2015-09-03 02:04:38

标签: c# asp.net radgrid radcombobox

其中有一个RadGrid,其中有RadComboBox(Say ComboIn),RadComboBox之外还有一个RadGrid(比如ComboOut)。
当用户从ComboOut中选择任何项目时,将根据所选的ComboOut项目在ComboIn中绑定项目。
我使用LoadOnDemand方法绑定 ComboIn RadComboBox

现在我想将此要求修改为:当用户单击ComboIn textarea时,不应加载整个项目列表(与选定的ComboOut项相关)。而是当用户键入/键入/搜索ComboIn中的项目并单击asp按钮时,只有与搜索文本相关的列表才会加载到ComboIn中。

以下是我目前使用的代码:

HTML

        <telerik:RadComboBox ID="ddlCompany" runat="server" Height="200" Width="240"
          DropDownWidth="310" EmptyMessage="- Select Product -" HighlightTemplatedItems="true" CausesValidation="false"
          Filter="Contains" AppendDataBoundItems="true" AllowCustomText="true" AutoPostBack="true"
          DataTextField="Title" DataValueField="Code" OnSelectedIndexChanged="ddlCompany_SelectedIndexChanged">
        </telerik:RadComboBox>

            <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>

C#

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)
{
    string c = ddlCompany.SelectedValue.ToString();
    DataTable dt = GetAccCode(c);
    DataView dv = new DataView(dt);
    string txt = e.Text;
    dv.RowFilter = string.Format("AccountDescription LIKE '%{0}%'", txt);
    int a = dv.Count;
    if (dv.Count > 0)
    {
        dt = dv.ToTable();
    }

    RadComboBox combo = (RadComboBox)sender;

    int itemOffset = e.NumberOfItems;
    int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
    e.EndOfItems = endOffset == dt.Rows.Count;

    for (int i = itemOffset; i < endOffset; i++)
    {
        combo.Items.Add(new RadComboBoxItem(dt.Rows[i]["AccountDescription"].ToString(), dt.Rows[i]["AccountDescription"].ToString()));
    }

    if (!string.IsNullOrEmpty(e.Text))
    {
        int num = dv.Count;
        endOffset = dv.Count;
    }
    e.Message = GetStatusMessage(endOffset, dt.Rows.Count);     
}
#endregion

protected void btnSearch_Click(object sender, EventArgs e)
{

}

我的代码工作正常。我唯一没有得到的是如何在asp按钮点击中仅绑定ComboIn中的搜索相关项目。
请回复。提前谢谢。

1 个答案:

答案 0 :(得分:1)

下面的代码解决了我的问题。

while (j<=3){ 事件中添加了_ItemRequested事件代码,但几乎没有修改,现在工作正常。

<强> C#

Button_Click

<强> HTML

        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();
        }