首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RadComboBox LoadOnDemand方法没有在运行时显示SelectedValue of RadComboBox

RadComboBox LoadOnDemand方法没有在运行时显示SelectedValue of RadComboBox
EN

Stack Overflow用户
提问于 2015-08-11 08:45:29
回答 1查看 2.2K关注 0票数 1

下面是按需加载功能到RadGrid内部的RadComboBox的链接。

链接:http://demos.telerik.com/aspnet-ajax/combobox/examples/populatingwithdata/autocompletesql/defaultcs.aspx

RadComboBox内部的绑定取决于从RadComboBox外部选择的项,该项位于RadGrid之外。也就是说,如果外部RadComboBox有项目: A、B、C、D、E等,那么RadComboBox内部的绑定将是: A1到An、B1到Bn、C1-Cn等,这取决于从外部RadComboBox中选择的项目。

现在我有三个问题:

1)如何在ComboBox中使用第0索引处的c#代码在ItemRequested事件下将默认文本写成“选择项”?

2)在编辑/更新按钮点击,RadComboBox选择的值没有显示在运行时。我能够在调试模式下看到值,但在运行时它永远不会显示。

3) --在上面的RadComboBox中的搜索功能--在我这边不像预期的那样工作。

,即演示中的,搜索在A1 to A100 (总记录)中,并从A8中返回msg项A1-A8 .也就是说,在所有的项目(A100)中,它只有到A8为止。

在我这边,搜索在A1 to A50 (ItemsPerPage)中,并将msg条目A1返回给A50 of A2000 (总记录).也就是说,在所有条目(A2000)中,它得到了批/页/ItemPerPage,然后当我向下滚动时,它会显示更多的搜索结果。

我不希望这种情况发生,我希望它的工作应该与在Demo/Link中进行搜索一样。

下面是我的当前代码:

C#代码:

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
<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>

我无法解决上述三个问题。请回复。

请注意,我在Telerik是非常新的。提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-15 16:11:00

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

第二期(RadComboBox在编辑时没有显示SelectedValue ):

代码语言:javascript
复制
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();
            }
        }
    }

第三期解决方案:

代码语言:javascript
复制
    //#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'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31937150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档