RadComboBox LoadOnDemand 方法在运行时不显示 RadComboBox 的 SelectedValue
RadComboBox LoadOnDemand approach is not showing the SelectedValue of RadComboBox at runtime
我在下面 link 关注 RadGrid 内的 RadComboBox 的 按需加载 功能。
Link: http://demos.telerik.com/aspnet-ajax/combobox/examples/populatingwithdata/autocompletesql/defaultcs.aspx
Inside RadComboBox 中的绑定取决于从 RadGrid 外部的 Outside RadComboBox 中选择的项目。
即,如果 RadComboBox 外部有项目:A、B、C、D、E 等
那么 Inside RadComboBox 中的绑定将是:A1 到 An、B1 到 Bn、C1-Cn 等,具体取决于从 Outside RadComboBox 中选择的项目。
现在我有 3 个问题:
1) 如何在 ItemRequested 事件下,在第 0 个索引处使用 c# 代码在 Inside ComboBox 中将默认文本写为 "Select Item"?
2) 在 Edit/Update 按钮点击时,RadComboBox 选择的值在 运行 时没有显示。我能够在调试模式下看到该值,但在 运行 时间它从未显示。
3) 上面行中的 RadComboBox 中的搜索功能在我这边没有按预期工作。
即, 在 Demo 中,搜索是在 A1 到 A100(总记录)和 return msg Items A1-A8 out of A8 之间。 ...即,在所有项目 (A100) 中,它只到 A8。
在我这边,搜索在 A1 到 A50 (ItemsPerPage) 和 return msg Items A1 到 A50 out of A2000(总记录数) ....即,在所有项目 (A2000) 中,它变得 batch/page/ItemPerPage 明智,然后当我向下滚动时它显示更多搜索结果。
我不希望发生这种情况,我希望它的工作方式与在 Demo/Link 中进行搜索时的工作方式相同。
以下是我当前的代码:
C#代码:
protected void RGGSTAcCode_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
//bind dropdwon while "Add"
string CompanyCode = ddlCompany.SelectedValue.ToString();
GridEditableItem item = (GridEditableItem)e.Item;
if (!(e.Item is IGridInsertItem))
{
RadComboBox rcb = (RadComboBox)item.FindControl("ddlAccountCode");
//Select particular dropdown value while "Edit"
Label lblAcCode2 = item.FindControl("lblAcCode2") as Label;
if (!string.IsNullOrEmpty(lblAcCode2.Text))
{
rcb.SelectedValue = Session["AccountDescription"].ToString();
string selVal = rcb.SelectedValue;
}
}
}
}
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;
}
//#Load on Demand
private const int ItemsPerRequest = 50;
private static string GetStatusMessage(int offset, int total)
{
if (total <= 0)
return "No matches";
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 = new DataTable();
dt = GetAccCode(c);
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()));
}
e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
}
protected void ddlAccountCode_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
string a = e.Value;
Session["AccountCodeID"] = a;
string b = e.Text;
Session["AccountDescription"] = b;
}
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>
<br />
<telerik:RadAjaxPanel ID="RadAjaxPanel4" runat="server">
<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: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" ItemsPerRequest="10"
EnableLoadOnDemand="True" ShowMoreResultsBox="true" EnableVirtualScrolling="true"
AutoPostBack="true" OnSelectedIndexChanged="ddlAccountCode_SelectedIndexChanged"
Filter="Contains" AppendDataBoundItems="true" DataTextField="AccountDescription" DataValueField="AccountCodeID">
</telerik:RadComboBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="AccountDescription" HeaderText="Description" UniqueName="AccountDescription" SortExpression="AccountDescription" InsertVisiblityMode="AlwaysHidden" ReadOnly="true" ></telerik:GridBoundColumn>
<telerik:GridBoundColumn aggregate="SUM" DataField="Amount" HeaderText="Amount" FooterAggregateFormatString="Total : {0:###,##0.00}" DataFormatString="{0:n}" FooterStyle-BackColor="#ffc04c" UniqueName="Amount" SortExpression="Amount"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Remark" HeaderText="IFCA Remark" UniqueName="Remark" SortExpression="Remark">
</telerik:GridBoundColumn>
<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>
</telerik:RadAjaxPanel>
我无法解决以上3个问题。请回复。
请注意,我是 Telerik 的新手。提前致谢
以下代码解决了我的问题:
第二期(编辑时 RadComboBox 在 运行 时不显示 SelectedValue):
protected void RGGSTAcCode_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
//bind dropdwon while "Add"
string CompanyCode = ddlCompany.SelectedValue.ToString();
GridEditableItem item = (GridEditableItem)e.Item;
if (!(e.Item is IGridInsertItem))
{
RadComboBox rcb = (RadComboBox)item.FindControl("ddlAccountCode");
//bind combobox in Edit mode from DB when using LoadOnDemand
RadComboBoxItem rcbi = new RadComboBoxItem();
rcbi.Text = ((DataRowView)e.Item.DataItem)["AccountCode"].ToString() + '-' + ((DataRowView)e.Item.DataItem)["AccountDescription"].ToString();
rcbi.Value = ((DataRowView)e.Item.DataItem)["AccountCodeID"].ToString();
rcb.Items.Add(rcbi);
rcbi.DataBind();
}
}
}
第三期解决方案:
//#Load on Demand
private const int ItemsPerRequest = 50;
private static string GetStatusMessage(int offset, int total)
{
if (total <= 0)
return "No matches";
return String.Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", offset, total);
}
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;
}
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);
}
//#End of 'Load on Demand'
我在下面 link 关注 RadGrid 内的 RadComboBox 的 按需加载 功能。
Link: http://demos.telerik.com/aspnet-ajax/combobox/examples/populatingwithdata/autocompletesql/defaultcs.aspx
Inside RadComboBox 中的绑定取决于从 RadGrid 外部的 Outside RadComboBox 中选择的项目。
即,如果 RadComboBox 外部有项目:A、B、C、D、E 等
那么 Inside RadComboBox 中的绑定将是:A1 到 An、B1 到 Bn、C1-Cn 等,具体取决于从 Outside RadComboBox 中选择的项目。
现在我有 3 个问题:
1) 如何在 ItemRequested 事件下,在第 0 个索引处使用 c# 代码在 Inside ComboBox 中将默认文本写为 "Select Item"?
2) 在 Edit/Update 按钮点击时,RadComboBox 选择的值在 运行 时没有显示。我能够在调试模式下看到该值,但在 运行 时间它从未显示。
3) 上面行中的 RadComboBox 中的搜索功能在我这边没有按预期工作。
即, 在 Demo 中,搜索是在 A1 到 A100(总记录)和 return msg Items A1-A8 out of A8 之间。 ...即,在所有项目 (A100) 中,它只到 A8。
在我这边,搜索在 A1 到 A50 (ItemsPerPage) 和 return msg Items A1 到 A50 out of A2000(总记录数) ....即,在所有项目 (A2000) 中,它变得 batch/page/ItemPerPage 明智,然后当我向下滚动时它显示更多搜索结果。
我不希望发生这种情况,我希望它的工作方式与在 Demo/Link 中进行搜索时的工作方式相同。
以下是我当前的代码:
C#代码:
protected void RGGSTAcCode_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
//bind dropdwon while "Add"
string CompanyCode = ddlCompany.SelectedValue.ToString();
GridEditableItem item = (GridEditableItem)e.Item;
if (!(e.Item is IGridInsertItem))
{
RadComboBox rcb = (RadComboBox)item.FindControl("ddlAccountCode");
//Select particular dropdown value while "Edit"
Label lblAcCode2 = item.FindControl("lblAcCode2") as Label;
if (!string.IsNullOrEmpty(lblAcCode2.Text))
{
rcb.SelectedValue = Session["AccountDescription"].ToString();
string selVal = rcb.SelectedValue;
}
}
}
}
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;
}
//#Load on Demand
private const int ItemsPerRequest = 50;
private static string GetStatusMessage(int offset, int total)
{
if (total <= 0)
return "No matches";
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 = new DataTable();
dt = GetAccCode(c);
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()));
}
e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
}
protected void ddlAccountCode_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
string a = e.Value;
Session["AccountCodeID"] = a;
string b = e.Text;
Session["AccountDescription"] = b;
}
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>
<br />
<telerik:RadAjaxPanel ID="RadAjaxPanel4" runat="server">
<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: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" ItemsPerRequest="10"
EnableLoadOnDemand="True" ShowMoreResultsBox="true" EnableVirtualScrolling="true"
AutoPostBack="true" OnSelectedIndexChanged="ddlAccountCode_SelectedIndexChanged"
Filter="Contains" AppendDataBoundItems="true" DataTextField="AccountDescription" DataValueField="AccountCodeID">
</telerik:RadComboBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="AccountDescription" HeaderText="Description" UniqueName="AccountDescription" SortExpression="AccountDescription" InsertVisiblityMode="AlwaysHidden" ReadOnly="true" ></telerik:GridBoundColumn>
<telerik:GridBoundColumn aggregate="SUM" DataField="Amount" HeaderText="Amount" FooterAggregateFormatString="Total : {0:###,##0.00}" DataFormatString="{0:n}" FooterStyle-BackColor="#ffc04c" UniqueName="Amount" SortExpression="Amount"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Remark" HeaderText="IFCA Remark" UniqueName="Remark" SortExpression="Remark">
</telerik:GridBoundColumn>
<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>
</telerik:RadAjaxPanel>
我无法解决以上3个问题。请回复。
请注意,我是 Telerik 的新手。提前致谢
以下代码解决了我的问题:
第二期(编辑时 RadComboBox 在 运行 时不显示 SelectedValue):
protected void RGGSTAcCode_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
//bind dropdwon while "Add"
string CompanyCode = ddlCompany.SelectedValue.ToString();
GridEditableItem item = (GridEditableItem)e.Item;
if (!(e.Item is IGridInsertItem))
{
RadComboBox rcb = (RadComboBox)item.FindControl("ddlAccountCode");
//bind combobox in Edit mode from DB when using LoadOnDemand
RadComboBoxItem rcbi = new RadComboBoxItem();
rcbi.Text = ((DataRowView)e.Item.DataItem)["AccountCode"].ToString() + '-' + ((DataRowView)e.Item.DataItem)["AccountDescription"].ToString();
rcbi.Value = ((DataRowView)e.Item.DataItem)["AccountCodeID"].ToString();
rcb.Items.Add(rcbi);
rcbi.DataBind();
}
}
}
第三期解决方案:
//#Load on Demand
private const int ItemsPerRequest = 50;
private static string GetStatusMessage(int offset, int total)
{
if (total <= 0)
return "No matches";
return String.Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", offset, total);
}
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;
}
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);
}
//#End of 'Load on Demand'