Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,
I'm autopopulating some data in DropDown List, depends upon value in textbox.
I've set AutoPostBack property of textbox to true. And both the controls are in updatePanel.
But using VPN network It is taking so much time to auto populate in dropdown list. As I've applied
indexing also on table in sql from which i'm selecting data.
Plz help me what is the issue.
This is my code for textbox :
XML
<asp:TextBox ID="txtPinCode" runat="server" CssClass="txtbox" TabIndex="23"
                                              onblur="CheckPincode(this);"
                                                AutoPostBack="True" ontextchanged="txtPinCode_TextChanged"
                                                ToolTip="Pincode must be 4 to 9 Integers" MaxLength="6"></asp:TextBox>

And this is code to autopopulate in dropdown :
C#
protected void txtPinCode_TextChanged(object sender, EventArgs e)
    {
        try
        {
            string pincode = txtPinCode.Text;
            double pin;
            bool isNum = double.TryParse(pincode, out pin);
            if (isNum)
            {
                txtPinCode.CssClass = "txtbox";
                pd.PinCode = txtPinCode.Text;
                pd.ID = 1;
                DataTable dt = new DataTable();

                dt = hd.GetPODetails(pd);

                if (dt.Rows.Count > 0)
                {
                    lblErrPinCode.Visible = false;
                    ddlPostOffice.DataTextField = dt.Columns["Description"].ToString();
                    ddlPostOffice.DataValueField = dt.Columns["ID"].ToString();
                    ddlPostOffice.DataSource = dt;
                    ddlPostOffice.DataBind();
                    ddlPostOffice.Items.Insert(0, new ListItem("---Select---", "0"));

                    pd.PinCode = txtPinCode.Text;
                    pd.ID = 3;
                    DataTable dtDist = new DataTable();

                    dtDist = hd.GetPODetails(pd);
                    if (dtDist.Rows.Count > 0)
                    {
                        txtDistrict.Text = dtDist.Rows[0]["DISTRICT_NAME"].ToString();
                    }

                }
                else
                {
                    ddlPostOffice.Items.Clear();
                    lblErrPinCode.Visible = true;
                    lblErrPinCode.Text = "Pincode and Post Office details does not exist.";
                }
            }
            else
            {
                txtPinCode.CssClass = "errtxtWidth200";
            }
        }
        catch (SqlException Sqlex)
        {
            if (Sqlex.Number == -2)
            {
                string error = Sqlex.Message;
                ErrorLogs(error);
                Alert.Show("Timeout occured. Please try later");
            }
        }
        catch (Exception ex)
        {
            string error = ex.Message;
            ErrorLogs(error);
        }
    }
Posted
Updated 8-Jul-13 23:32pm
v2
Comments
Dipali_Wagh 9-Jul-13 5:51am    
plz help me, provide me solution

Obviously I will take time.. Because network/hardware processing cause lagging in reqeust/response. you should use jquery and and its native ajax api to call code behind mehtod. I've a post regarding same, you can check Auto Complete with jQuery Template Plugin and AJAX
 
Share this answer
 
Comments
Dipali_Wagh 10-Jul-13 5:31am    
hey can u tell me how it can be done in my code?
becoz I'm using seperate controls, like textbox value will be different and dropdown values will be different.
co plz help me.
i'm not getting ur code
I've modified some code from my post to solve your problem.
But before you try this code I want to clear that this approach is only saving your network traffic between browser and IIS and doing things better that a update panel does. So if there any network related issues/lagging at your DB server level then It wont help much and I'll suggest your to fine tune your SP/Tables/Indexes instead trying this.

Copy/paste below code in new test page and try it prior actual implementation. Hope it will help.

Note - Dont forget to add EnableEventValidation="false" in your @page directive

ASP.NET
<head  runat="server">
    <title></title>
    <script src="jquery-1.8.3.min.js" type="text/javascript"></script>
    <script src="jquery.tmpl.js" type="text/javascript"></script>
    <script type="text/javascript">
      function getsuggestions(event) {

            $('#<%=txtValue.ClientID%>').val('');
            var value = $('#' + '<%=txtnames.ClientID%>').val();

            if (value.length > 0) { // here you can set minimum no. of character for search
                $.ajax({
                    type: "POST",
                    url: "AutoComplete.aspx/getSuggestions", //pagename.aspx/methodname
                    data: "{'keyword':'" + value + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg, sd, fg) {
                        if (msg.d && msg.d.length > 0) {
                            $("#" + '<%=ddlSuggestion.ClientID%>').children().remove();
                            //create option element by using "jquery.tmpl.js" 
                            //and add them to listbox
                            $("#tmplSuggestions").tmpl(msg.d).appendTo("#" + '<%=ddlSuggestion.ClientID%>');
                            $('#<%=txtValue.ClientID%>').val($('#<%=ddlSuggestion.ClientID%> option:eq(0)').val());
                        }
                        else {
                            $('#<%=txtValue.ClientID%>').val('');
                        }
                    },
                    error: function (gfh, ghg) {
                        alert("error");
                    },
                    complete: function (eret, tytyt) {
                        //alert("complete");
                    }
                });
            }
        }

        function setvalue() {
            $('#<%=txtValue.ClientID%>').val('');
            $('#<%=txtValue.ClientID%>').val($('#<%=ddlSuggestion.ClientID%>').val());
        }

        $(document).ready(function (){
            $('#<%=txtValue.ClientID%>').val('');
        })
    </script>

    <script id="tmplSuggestions" type="text/html">
        <option value="${Value}">${Name}</option>
    </script>
   
</head>
<body>
    <form id="form1"  runat="server">
    </form>
</body>


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data;
namespace TestApp
{
    public partial class AutoComplete : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static object getSuggestions(string keyword)
        {
            // Getting data into datatable to depict normal scenario, 
            // This is just illustration you can remove following code with yours

            DataTable dtSugg = GetSuggestionsFromDB();
            var list = from dr in dtSugg.AsEnumerable()
                       where dr.Field<string>("Name").Contains(keyword)
                       select new { Name = dr.Field<string>("Name"), Value = dr.Field<string>("Value") };

            return list;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string msg =string.Empty;
            if (txtValue.Text == string.Empty)
            {
                msg = "Please select one value";
            }
            else
            {
                msg = txtValue.Text;
            }
            Response.Write(msg);
        }

        protected static DataTable GetSuggestionsFromDB()
        {
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("Name");
            DataColumn dc1 = new DataColumn("Value");
            dt.Columns.Add(dc);
            dt.Columns.Add(dc1);

            dt.Rows.Add("john", "john");
            dt.Rows.Add("jerry", "jerry");
            dt.Rows.Add("jonathan", "jonathan");
            dt.Rows.Add("micheal", "micheal");
            dt.Rows.Add("mike", "mike");
            dt.Rows.Add("michelle", "michelle");
            dt.Rows.Add("cindy", "cindy");
            dt.Rows.Add("campbell", "campbell");
            dt.Rows.Add("paul", "paul");
            dt.Rows.Add("petty", "petty");
            dt.Rows.Add("paurus", "paurus");
            dt.Rows.Add("Edward", "Edward");
            dt.Rows.Add("Abbey", "Abbey");
            dt.Rows.Add("Henry", "Henry");
            dt.Rows.Add("Derek", "Derek");
            dt.Rows.Add("Diane", "Diane");
            dt.Rows.Add("Edwin", "Edwin");
            dt.Rows.Add("Abbot", "Abbot");
            dt.Rows.Add("Jack", "Jack");
            dt.Rows.Add("Abbott", "Abbott");
            dt.Rows.Add("Shirley", "Shirley");
            dt.Rows.Add("Tony", "Tony");

            return dt;
        }
    }
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900