单击 asp 按钮在 RadComboBox(在 RadGrid 内)中搜索
Search in RadComboBox (inside RadGrid) on asp button click
有一个RadGrid
,里面有一个RadComboBox
(比如ComboIn),还有1个RadComboBox
在RadGrid
外面(比如ComboOut)。
当用户 select 来自 ComboOut 的任何项目时,项目将根据 ComboOut 的 selected 项目绑定到 ComboIn 中。
我正在使用 LoadOnDemand
方法绑定 ComboIn RadComboBox
。
现在我想将此要求修改为:当用户单击 ComboIn 文本区域时,不应加载整个项目列表(与 selected ComboOut 项目相关)。相反,当用户 type/key-in/search 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 中的搜索相关项目。
请回复。提前致谢。
下面的代码解决了我的问题。
在 Button_Click
事件中添加了 _ItemRequested
事件代码,稍作修改,现在工作正常。
C#
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();
}
HTML
<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>
有一个RadGrid
,里面有一个RadComboBox
(比如ComboIn),还有1个RadComboBox
在RadGrid
外面(比如ComboOut)。
当用户 select 来自 ComboOut 的任何项目时,项目将根据 ComboOut 的 selected 项目绑定到 ComboIn 中。
我正在使用 LoadOnDemand
方法绑定 ComboIn RadComboBox
。
现在我想将此要求修改为:当用户单击 ComboIn 文本区域时,不应加载整个项目列表(与 selected ComboOut 项目相关)。相反,当用户 type/key-in/search 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 中的搜索相关项目。
请回复。提前致谢。
下面的代码解决了我的问题。
在 Button_Click
事件中添加了 _ItemRequested
事件代码,稍作修改,现在工作正常。
C#
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();
}
HTML
<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>