Fill ASP.Net dropdownlist using jQuery from Java Script and Set selectedtext after jQuery populated dropdownlist



In my application I have requirement to fill drop down using Jquery AJAX. User will not get any postback and drop down bind
smoothly.

In my dropdown, ID and text are same. for different ID and text you have to modify code little bit.



$.ajax({
                    type: "POST",
                    url: "EntityObject.aspx/GetValues",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: "{'ID':'" + ID + "'}",
                    success: function (result) {

                        document.getElementById('<%= dropdown.ClientID %>').options.length = 0;

                        $.each(result.d, function (key, value) {
                            $('#<%=dropdown.ClientID%>').append($("<option></option>").val(value).html(value));
                        });

//set Text in filled dropdownlist.
for (var i = 0; i < document.getElementById('<%= dropdown.ClientID %>').options.length; i++) 
{
    if (document.getElementById('<%= dropdown.ClientID %>').options[i].text == selectedtext) {
          document.getElementById('<%= dropdown.ClientID %>').options[i].selected = true;
          return;
         }
}                

                    },
                    error: function (xhr, status, error) {
                        alert("Error in DropDown Fill.");

                    }
                });



Below method will come in .cs page.


[WebMethod(EnableSession = true)]
        public static List<string> GetValues(Guid searchObjectID)
        {
            List<string> list = new List<string>();
        //Logic to fill List
            return list;
        }



Posted in | 0 comments

Jquery DatePicker is not working after partial postback.

Hello,

Sometimes after partial post back, datepicker do not work as accepted.

The problem is we require to re-initialize it after partial post back is completed.

In my application I have solved above problem by below code.


Sys.WebForms.PageRequestManager.getInstance().add_endRequest event
"Raised after an asynchronous postback is finished and control has been returned to the browser."


 $(document).ready(function() {
            Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

            function EndRequestHandler(sender, args) {
                  $(".date-picker").datepicker({
                            changeMonth: true,
                            changeYear: true,
                            showButtonPanel: true,
                            showOtherMonths: true,
                            selectOtherMonths: true
                        });

            }


Above solution is work fine for me. However I have observed there are many other problem for this.solution may different in that case.


Posted in | 0 comments

Generate Hierarchy XML using SQL Server.

Hello Friend's,

Today I have worked on generating Hierarchy XML output using SQL Server. Later I have bind this XML with TreeView.

Below is my table.


for your reference below is table creation query.

CREATE TABLE [dbo].[Ketan_Example](
[Id] [int] NOT NULL,
[Item] [nvarchar](50) NULL,
[ParentID] [int] NULL,
 CONSTRAINT [PK_Ketan_Example] PRIMARY KEY CLUSTERED 
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]



Now insert dummy data in table.





Create function

CREATE FUNCTION [dbo].[ketan_HierarchyNode] (@ParentID INT) RETURNS XML
BEGIN RETURN
    (SELECT Id as Value, 
            ParentID, 
            Item,
            dbo.ketan_HierarchyNode(Id)
        FROM Ketan_Example as Node 
        WHERE ParentID = @ParentID
        FOR XML Auto)
END;

GO



Create Store Procedure.

CREATE  PROCEDURE [dbo].[GetXMLforLabeling]
AS

SELECT 
Id,
        ParentID,
        Item,
        dbo.ketan_HierarchyNode(Id)
FROM    Ketan_Example
WHERE   ParentID IS NULL 
FOR     XML AUTO




All Done !!!


Now execute the Store Procedure.





Let me know if you have any question.


Posted in | 0 comments