user1632134
user1632134

Reputation: 49

How to pass values to javascript from c# to ascx page

i am working on asp.net c#. I want to send a string value from c# to ascx page . I am using following code to do that ,

string one = @"\'" + names[0, 0] + "\'" + "," + @"\'" + names[1, 0] + "\'";
string[] splitted = one.Split('\'');
string onces =  splitted[1] + ","  + splitted[3] ;
inpHide.Value =onces;

In ascx page i should pass this values to javascript , i did like this ,

$(document).ready(function () {
  var hiddenControl = '<%= inpHide.ClientID %>';
  var values = document.getElementById(hiddenControl).value;
  alert(values);

  var tweetUsers = [values];
});
<input id="inpHide" type="hidden" runat="server" />

when i debug javascript using firebug values coming like [""twitter","microsoft""] but " quotes are being added automatically into javascript . but i should get like this ["twitter","microsoft"] because of this string javascript is not working. how can i solve this problem ? please help me to solve this problem . thank you in advance.

Upvotes: 4

Views: 1237

Answers (1)

Talha Ashfaque
Talha Ashfaque

Reputation: 4072

you should try using JSON, and pass that string to client side: use this function to serialize your string into JSON:

    public string ToJSON(this object obj)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        return serializer.Serialize(obj);
    }

and then pass the return string to your client-side, try using a public string and in javascript use that public string instead of hidden control

Upvotes: 1

Related Questions