Combobox databind from string not working

2 posts, 0 answers
  1. Joe
    Joe avatar
    57 posts
    Member since:
    Mar 2013

    Posted 04 Sep 2013 Link to this post

    In my application, it will pop up a radwindow when user add/edit record.
    And I want to databind a combox from a string, but my code not working.

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Allowance.aspx.vb" Inherits="Payroll_Allowance" %>
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head id="Head1" runat="server">
     
    </head>
    <body>
        <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <telerik:RadSplitter ID="RadSplitter1" runat="server" Width="100%" Height="100%" Orientation="Horizontal">
     
            <telerik:RadPane ID="rpContent" runat="server">
     <asp:Panel ID="Panel1" runat="server" Width="100%"  CssClass=thePanel>
            <asp:Table ID="Table0" runat="server" Height="100%">
                <asp:TableRow>
                    <asp:TableCell Font-Bold="true" font-size="10pt" CssClass=theHead>
            Payroll Allowance  
                    </asp:TableCell>
                </asp:TableRow>
            </asp:Table>
        </asp:Panel>
        <telerik:RadToolBar ID="rtbMain" runat="server" Width="100%">
        <Items>
        <telerik:RadToolBarButton Width="80px" Value="Add" Text="Add" CausesValidation="false"></telerik:RadToolBarButton>
        <telerik:RadToolBarButton IsSeparator="true"></telerik:RadToolBarButton>
      <%--  <telerik:RadToolBarButton Width="80px" Value="Edit" Text="Edit" CausesValidation="false" Enabled=false></telerik:RadToolBarButton>
        <telerik:RadToolBarButton IsSeparator="true"></telerik:RadToolBarButton>
        <telerik:RadToolBarButton Width="80px" Value="Delete" Text="Delete" CausesValidation="false" Enabled=false></telerik:RadToolBarButton>
        <telerik:RadToolBarButton IsSeparator="true"></telerik:RadToolBarButton>
      --%> 
        <telerik:RadToolBarButton Width="80px" Value="Report" Text="Report" visible=false CausesValidation="false"></telerik:RadToolBarButton>
        <telerik:RadToolBarButton IsSeparator="true"></telerik:RadToolBarButton>
         
        </Items>
        </telerik:RadToolBar>
        <div id="printarea">
        <asp:Table ID="tblHistory" width=800px runat="server" >
        <asp:TableRow>
        <asp:TableCell>
        <asp:Table ID="Table1" runat="server" Width=650 BorderStyle=None>
        <asp:TableRow>
        <asp:TableCell CssClass=fieldheader Width=150>Staff Name</asp:TableCell>
        <asp:TableCell Width=250><asp:Label ID="lbStaffName" runat=server></asp:Label></asp:TableCell>
        <asp:TableCell CssClass=fieldheader Width=150>Staff No.</asp:TableCell>
        <asp:TableCell Width=250><asp:Label ID="lbStaffNo" runat=server></asp:Label></asp:TableCell>
     
        </asp:TableRow>
        </asp:Table>
        </asp:TableCell>
        </asp:TableRow>
        </asp:Table>
        <br />
        <telerik:RadGrid ID="rgAllowance" runat="server" DataSourceID="ldsAllowance"
                        GridLines="None" Width="800px" AllowSorting="True" AutoGenerateColumns="false" AllowFilteringByColumn="false" ShowGroupPanel=false ShowFooter="False"
                        AllowPaging="True" PageSize="15">
                        <PagerStyle Position="TopAndBottom" />
                         
            <ClientSettings EnablePostBackOnRowClick="True" AllowColumnsReorder="True" AllowDragToGroup="false" ReorderColumnsOnClient="True">
                <Selecting AllowRowSelect="True" />            
            </ClientSettings>
               
             
                        <MasterTableView DataSourceID="ldsAllowance" DataKeyNames="AllowanceID" >
                            <CommandItemSettings ExportToPdfText="Export to Pdf"></CommandItemSettings>
                            <RowIndicatorColumn>
                                <HeaderStyle Width="10px"></HeaderStyle>
                            </RowIndicatorColumn>
                            <ExpandCollapseColumn>
                                <HeaderStyle Width="10px"></HeaderStyle>
                            </ExpandCollapseColumn>
                            <Columns>
                                <telerik:GridBoundColumn DataField="AllowanceID" HeaderText="Allowance ID" ReadOnly="True"
                                    SortExpression="AllowanceID" UniqueName="AllowanceID" Visible=false>
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="PPSCompanyCode" HeaderText="Allowance Code" ReadOnly="True"
                                    SortExpression="PPSCompanyCode" UniqueName="PPSCompanyCode"><ItemStyle Font-Underline="True" BackColor=White ForeColor="Blue" BorderColor=ActiveBorder/>
                                </telerik:GridBoundColumn>                           
                                <telerik:GridBoundColumn DataField="EntitleFrom" HeaderText="Entitle From" ReadOnly="True"  DataFormatString="{0:dd-MMM-yyyy}"
                                    SortExpression="EntitleFrom" UniqueName="EntitleFrom"><ItemStyle BackColor=White  BorderColor=ActiveBorder />
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="EntitleTo" HeaderText="Entitle To" ReadOnly="True"  DataFormatString="{0:dd-MMM-yyyy}"
                                    SortExpression="EntitleTo" UniqueName="EntitleTo"><ItemStyle BackColor=White BorderColor=ActiveBorder/>
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="AllowanceAmount" HeaderText="Remarks" ReadOnly="True" DataFormatString="{0:N}"
                                    SortExpression="AllowanceAmount" UniqueName="AllowanceAmount"><ItemStyle BackColor=White BorderColor=ActiveBorder/>
                                </telerik:GridBoundColumn>
                                
     
     
                                </Columns>
                        </MasterTableView>
                    </telerik:RadGrid>
        </div>
            </telerik:RadPane>
        </telerik:RadSplitter>
        <telerik:RadWindow ID="RadWindow1" runat="server" Width="580px" Height="300px" Behaviors="Close"
            Title="Add/Modify Payroll Allowance" >
            <ContentTemplate>
                <asp:Table ID="Table2" runat="server" Width="100%" Height="100%">
                    
                    <asp:TableRow >
                    <asp:TableCell CssClass="editheader">Allowance Code: </asp:TableCell>
                    <asp:TableCell>
                        <telerik:RadComboBox ID="dfAllowanceCode" runat="server" Width="250px"
                        AppendDataBoundItems="true" AutoPostBack="true"
                        OnDataBound="dfAllowanceCode_DataBound">
                         <Items>
                            <telerik:RadComboBoxItem Text="--Select--" Value="" />
                        </Items>
                        </telerik:RadComboBox>
     
                    </asp:TableCell>
                      </asp:TableRow>
     
                    <asp:TableRow>  
                        <asp:TableCell CssClass="editheader">Entitle From: </asp:TableCell>
                        <asp:TableCell>
                            <asp:HiddenField ID="dfAllowanceID" Value="0" runat="server" />
                            <telerik:RadDatePicker ID="dfStartDate" runat="server" Width="150px" DateInput-DateFormat="dd-MMM-yyyy" Enabled=false>
                            </telerik:RadDatePicker>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="* Required"
                                ControlToValidate="dfStartDate"></asp:RequiredFieldValidator>
                        </asp:TableCell>
                    </asp:TableRow>
                    <asp:TableRow>
                        <asp:TableCell CssClass="editheader">Entitle To: </asp:TableCell>
                        <asp:TableCell>
                           <telerik:RadDatePicker ID="dfEndDate" runat="server" Width="150px" DateInput-DateFormat="dd-MMM-yyyy" Enabled=false>
                            </telerik:RadDatePicker>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="* Required"
                                ControlToValidate="dfEndDate"></asp:RequiredFieldValidator>
                        </asp:TableCell>
                    </asp:TableRow>
                    <asp:TableRow>
                        <asp:TableCell CssClass="editheader">Amount: </asp:TableCell>
                        <asp:TableCell>
                            <telerik:RadNumericTextBox  ID="dfAmount" runat="server" Width="400px" Height="50px" Enabled="false"
                            NumberFormat-DecimalDigits="2" >
                            </telerik:RadNumericTextBox >
                        </asp:TableCell>
                    </asp:TableRow>
     
                    <asp:TableRow>
                        <asp:TableCell BackColor="#EEEEEE" ColumnSpan="2" HorizontalAlign="Center">
                            <asp:Button ID="btnSave" runat="server" Text="Save" Width="80px" />  
                            <asp:Button ID="btnDelete" runat="server" Text="Delete" Width="80px" CausesValidation="false" />
                            <asp:Button ID="btnCancel" runat="server" Text="Close" Width="80px" CausesValidation="false" />
                        </asp:TableCell>
                    </asp:TableRow>
                </asp:Table>
            </ContentTemplate>
        </telerik:RadWindow>
     
        <asp:LinqDataSource ID="ldsAllowance" runat="server" ContextTypeName="dcHRISDataContext"
            OrderBy="EntitleFrom desc,AllowanceID desc"
            TableName="HRIS_PPSAllowances" Where="SID==@SID">
             <WhereParameters>
                <asp:QueryStringParameter DefaultValue="0" Name="SID" QueryStringField="SID"
                    Type="Int32" />               
            </WhereParameters>
        </asp:LinqDataSource>
     
        </form>
    </body>
    </html>

    Code Behind:

    Protected Sub dfAllowanceCode_DataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadComboBoxItemEventArgs) Handles dfAllowanceCode.DataBound
        dfAllowanceCode.Items.Clear()
      
        Dim S As String = "PPS123, PPS22323, PPS5454, PPS1231"
        Dim arrayvalue As String() = S.Split(","c)
     
        For i As Integer = 0 To arrayvalue.Length - 1
            dfAllowanceCode.Items.Add(New Telerik.Web.UI.RadComboBoxItem(arrayvalue(i).ToString(), i))
        Next
     
    End Sub








  2. Nencho
    Admin
    Nencho avatar
    1457 posts

    Posted 09 Sep 2013 Link to this post

    Hello Joe,

    The reason for the experienced issue is that the OnDataBound event is never reached, since this event fires if  you have specified a DataSource or DataSourceID of the RadComboBox. Therefore, my suggestion would be to use another event of the RadComboBox - OnLoad for example.

    In addition, I noticed that you attempt to persist one "Select" item in the RadComboBox. Please keep in mind that with the currently used approach for populating the combo with data, this item would be overwritten and won't get displayed. Thefore, I would suggest you to use the DefaultItem of the RadComobBox, or the EmptyMessage property to specify the desired item. Here is a video, demonstrating the behavior at my end, using the OnLoad event and the DefaultItem of the RadComboBox. Please give it a try at your end and let us know if the issue still persist.

    Regards,
    Nencho
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top