Auto Complete On Entry Event

4 posts, 1 answers
  1. Lawrence
    Lawrence avatar
    2 posts
    Member since:
    Jun 2015

    Posted 31 May Link to this post

    Hi all,

    I'm having a difficulties here. I create a RadGrid and use an EditFormSettings for inserting new data. Within the EditFormSettings , I have a RadAutoCompleteBox ID="plant" which will get the value from database. I want,  when Users choose the Plant in RadAutoCompleteBox, system will parse the value of Latitude & Longitude to the RadTextBox which defined.

    My challenge here, I'm able to retrieve the Latitude & Longitude value but unable to assign to the respective textbox. 

    Please help..

    My code as below;
    C#

    protected void Plant_TextChanged(object sender, Telerik.Web.UI.AutoCompleteEntryEventArgs e)
    {
       string a = e.Entry.Text;
       foreach (GridEditableItem item in RadGrid2.EditItems)
        {  
            RadTextBox lati = (RadTextBox)item.FindControl("plantLat2");
            RadTextBox longi = item.FindControl("plantLong2") as RadTextBox;
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ibmsConnectionString"].ToString());
            SqlCommand cmd = new SqlCommand("plant_GPS_list", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            con.Open();
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.Add("@Plant", SqlDbType.VarChar, 50);
            da.SelectCommand.Parameters["@Plant"].Value = a;
            SqlDataReader dr = da.SelectCommand.ExecuteReader();
            try
            {
                if (dr.Read())
                {
                    lati.Text = dr["Latitude"].ToString();
                    longi.Text = dr["Longitude"].ToString();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            con.Close();
        }
    }

    ASPX

    <telerik:RadGrid ID="RadGrid2" runat="server" DataSourceID="SqlDataSource2" OnDetailTableDataBind="RadGrid2_DetailTableDataBind"
        OnNeedDataSource="RadGrid2_NeedDataSource" AllowFilteringByColumn="True" AllowPaging="True"
        OnItemCreated="RadGrid2_ItemCreated" OnInsertCommand="RadGrid2_InsertCommand"
        OnItemDataBound="RadGrid2_ItemDataBound" OnItemCommand="RadGrid2_ItemCommand">
        <GroupingSettings CollapseAllTooltip="Collapse all groups" />
        <MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource2" DataKeyNames="Plant"
            CommandItemDisplay="Top">
            <CommandItemTemplate>
                <div style="padding: 10px 10px; color: #2B373D !important; font-weight: bold;">
                    <asp:LinkButton ID="LinkButton2" runat="server" CommandName="InitInsert" Visible='<%# !RadGrid2.MasterTableView.IsItemInserted %>'>
                        <i class="fa fa-plus"></i> Add Platform / Plant
                    </asp:LinkButton>
                </div>
            </CommandItemTemplate>
            <Columns>
                ....
            </Columns>
            <DetailTables>
                <telerik:GridTableView Name="BpName">
                    .......
                </telerik:GridTableView>
            </DetailTables>
            <EditFormSettings EditFormType="Template">
                <FormTemplate>
                    <div class="form-horizontal">
                        <div class="form-group">
                            <div class="col-md-2 control-label">
                                Plant :</div>
                            <div class="col-md-10">
                                <div class="form-inline">
                                    <div class="form-group">
                                        <telerik:RadAutoCompleteBox ID="plant" runat="server" DataSourceID="dsPlant" DataTextField="Plant"
                                            OnEntryAdded="Plant_TextChanged" DataValueField="Plant" InputType="Token" Width="300px"
                                            AllowCustomEntry="true" AutoPostBack="True">
                                            <TextSettings SelectionMode="Single" />
                                        </telerik:RadAutoCompleteBox>
                                    </div>
                                    <div class="form-group">
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="plant"
                                            ErrorMessage="* Please insert Plant." Font-Bold="True" ForeColor="Red" SetFocusOnError="True">
                                        </asp:RequiredFieldValidator>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-2 control-label">
                                GPS Location :</label>
                            <div class="col-md-10">
                                <div class="form-horizontal">
                                    <div class="form-group">
                                        <label class="col-sm-12 col-md-1 control-label">
                                            Latitude
                                        </label>
                                        <div class="col-sm-12 col-md-10">
                                            <telerik:RadTextBox ID="plantLat2" runat="server" Text='<%# Bind( "Latitude") %>'>
                                            </telerik:RadTextBox>
                                            <asp:RangeValidator ID="LatRangeValidator2" runat="server" ControlToValidate="plantLat2"
                                                ErrorMessage="* Choose a coordinate between -90 and 90 degrees inclusive." Font-Bold="true"
                                                ForeColor="Red" MaximumValue="90" MinimumValue="-90" EnableClientScript="False"
                                                Type="Double" SetFocusOnError="True">
                                            </asp:RangeValidator>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class=" col-sm-12 col-md-1 control-label">
                                            Longitude
                                        </label>
                                        <div class="col-sm-12 col-md-10">
                                            <telerik:RadTextBox ID="plantLong2" runat="server" Text='<%# Bind( "Longitude") %>'>
                                            </telerik:RadTextBox>
                                            <asp:RangeValidator ID="LongRangeValidator2" runat="server" ControlToValidate="plantLong2"
                                                ErrorMessage="* Choose a coordinate between -180 and 180 degrees inclusive."
                                                Font-Bold="true" ForeColor="Red" MaximumValue="180" MinimumValue="-180" EnableClientScript="False"
                                                Type="Double" SetFocusOnError="True">
                                            </asp:RangeValidator>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-2 control-label">
                            </div>
                            <div class="col-md-10">
                                <asp:Button ID="plantUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                    CssClass="btn btn-warning" runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
                                </asp:Button
                                <asp:Button ID="plantCancel" Text="Cancel" runat="server" CausesValidation="False"
                                    CssClass="btn btn-info" CommandName="Cancel"></asp:Button>
                            </div>
                        </div>
                    </div>
                </FormTemplate>
            </EditFormSettings>
        </MasterTableView>
    </telerik:RadGrid>

  2. Answer
    Peter Milchev
    Admin
    Peter Milchev avatar
    136 posts

    Posted 03 Jun Link to this post

    Hello Lawrence,

    Would you try to replace your handler with the following and see if it works on your side also:

    protected void Plant_TextChanged(object sender, Telerik.Web.UI.AutoCompleteEntryEventArgs e)
    {
        string a = e.Entry.Text;
     
        RadAutoCompleteBox racb = sender as RadAutoCompleteBox;
        GridEditFormItem item = (GridEditFormItem)racb.NamingContainer;
     
        RadTextBox lati = (RadTextBox)item.FindControl("plantLat2");
        RadTextBox longi = item.FindControl("plantLong2") as RadTextBox;
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ibmsConnectionString"].ToString());
        SqlCommand cmd = new SqlCommand("plant_GPS_list", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        con.Open();
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        da.SelectCommand.Parameters.Add("@Plant", SqlDbType.VarChar, 50);
        da.SelectCommand.Parameters["@Plant"].Value = a;
        SqlDataReader dr = da.SelectCommand.ExecuteReader();
        try
        {
            if (dr.Read())
            {
                lati.Text = dr["Latitude"].ToString();
                longi.Text = dr["Longitude"].ToString();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        con.Close();
    }


    What I changed is the item used to find the control. If this does not solve the problem, please open a support ticket and send us a runnable sample, so we could recreate locally your case and investigate it further. Here is an article on Isolating a problem in a sample project.

    Regards,
    Peter Milchev
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Lawrence
    Lawrence avatar
    2 posts
    Member since:
    Jun 2015

    Posted 05 Jun in reply to Peter Milchev Link to this post

    Hi Peter,

    It's working fine.

    Thanks a lot.

  5. Peter Milchev
    Admin
    Peter Milchev avatar
    136 posts

    Posted 07 Jun Link to this post

    Hi Lawrence,

    I am glad that the suggested approach helped you solve the issue.

    Regards,
    Peter Milchev
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top