Wednesday 31 July 2013

Get NameValueCollection collection of Webconfig To Clientside

I had one issue that i had to get NameValueCollection  at client side. I get all this array at server side  perfactly. But i want that at client side. So i had lots of RND but not getting proper result. So here i am sharing you one of good solution.

My Web config

<configuration>
  <configSections>
    <section name="MyDictionary" type="System.Configuration.NameValueSectionHandler" />
  </configSections>
  <appSettings>
    <add key="test" value="test"/>
   
  </appSettings>
  <MyDictionary>
    <add key="Regular" value="10"/>
    <add key="Senior" value="11"/>
    <add key="Under25" value="17"/>
    <add key="Child" value="35"/>
    <add key="Educator" value="28"/>
    <add key="Child2" value="134"/>
  </MyDictionary>

</configuration>

MyTest.aspx.cs



    public NameValueCollection section { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            section = (NameValueCollection)ConfigurationManager.GetSection("MyDictionary");
            str = section["Regular"];
            keys = section.AllKeys;
         
            for (int i = 0; i < section.Keys.Count; i++)
            {
                Page.ClientScript.RegisterArrayDeclaration("myArray", "'" + section[i] + "'");
                Page.ClientScript.RegisterArrayDeclaration("myArray1", "'" + section.Keys[i] + "'");               
            }

        }


    }

MyTest.aspx

 <script type="text/javascript">
        $(document).ready(function () {           
            myfunction();
        });
        function myfunction() {
            for (var i = 0; i < myArray.length; i++) {
                alert(myArray[i]);
            }
        }
    </script>


No comments:

Post a Comment