0

Can anyone please tell me how to pass arguments to javascript from c#?

I used this.Page.ClientScript.RegisterClientScriptBlock, but it is not calling the method.

SqlDataAdapter da = new SqlDataAdapter();da = o1.viewThisAddMoreFields(Convert.ToInt32(id));

   DataTable dt = new DataTable();

   da.Fill(dt);

   int count = dt.Rows.Count;

   for (int i = 0; i < count; i++)

   {

     extraFieldName[i] = dt.Rows[i]["FieldName"].ToString();

     extraFieldValue[i] = dt.Rows[i]["FieldValue"].ToString();
   }


  for (int i = 0; i < count; i++)

  {



                 ModalPopupExtender1.Hide();

                 ModalPopupExtender2.Hide();

                 Page.ClientScript.RegisterArrayDeclaration("ExtraFName", "extraFieldName[i]");

                 Page.ClientScript.RegisterArrayDeclaration("ExtraFValue", "extraFieldValue[i]");

                 Page.ClientScript.RegisterArrayDeclaration("totalCount", "count");


                 // ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript: moreFieldsEditFunction(); ", true);"+extraFieldName[i].ToString()+","+extraFieldValue[i].ToString()+"




             }

             for (int i = 0; i < count; i++)

             {
                // ClientScript.RegisterStartupScript(GetType(),"Javascript", "javascript: moreFieldsEditFunction('Hai','Hello');", true);

                 this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "xx", "<script>moreFieldsEditFunction(" + extraFieldName[i] + "," + extraFieldValue[i] + ");</script>");
             }

Javascript code is:

 function moreFieldsEditFunction(extrafname,extrafvalue) {


         alert(extrafname + "test" + extrafvalue + "Helloooooo");

     }

Is this format correct?

I can able to call using single parameter, but when passing two parameters it is not working. Any syntax error?

1 Answer 1

1

Pass the name in single quotes as string variables need to be passed in quotes.

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "xx", "<script>moreFieldsEditFunction('" + extraFieldName[i] + "','" + extraFieldValue[i] + "');</script>");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much.I made a mistake in calling javascript.After adding single quotes it works well.Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.