Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
See more:
I am calling javascript function from anchor tag. But problem is i am rendering anchor tag from code rendering block. From here i am fetching a value from database and sending to the function as a parameter. My code is:

ASP.NET
<div>
<%-- <h3> Curent </h3>--%>
<ul>
<%                                
   if (_dsContent.Tables.Count == 0)
      return;
   System.Collections.Generic.List<string> lstCat =
   new System.Collections.Generic.List<string>();

   foreach (System.Data.DataTable table in _dsContent.Tables)
   {
    foreach (System.Data.DataRow row in table.Rows)
    {
     if (String.IsNullOrEmpty(Convert.ToString(row["ContentPart"])))
       {
         if (Convert.ToString(row["Content"]).Length > 200)
           content = Convert.ToString(row["Content"]).Substring(0, 200);
         else
           content = Convert.ToString(row["Content"]);
       }
     else
       {
       if (Convert.ToString(row["ContentPart"]).Length > 200)
        content = Convert.ToString(row["ContentPart"]).Substring(0, 200);
       else
         content = Convert.ToString(row["ContentPart"]);
       }                       

string catStr = Convert.ToString(row["categoryString"]);
string[] arrayCat = new string[2];

if (!String.IsNullOrEmpty(catStr))
{
  arrayCat = catStr.Split(',');
}
if (!String.IsNullOrEmpty(content))
{
  if (!lstCat.Contains(arrayCat[1]))
   {
 Response.Write("<div style="color:Red; font-weight:<br mode="hold" /> bold;font-size:20px">" + arrayCat[1] + "</div>");
 lstCat.Add(arrayCat[1]);
 }

 Response.Write("<li>");
 Response.Write("<div style="background-<br mode="hold" /> color:#DFD48D">Posted On - " + row["InsertionDate"] + "</div>");

 Response.Write("<div style="font-weight: bold;text-<br mode="hold" /> align:justify">'" + row["Header"] + "'</div>");
 Response.Write("<div style="font-size: small">Date - '" + row["Date"] + "'</div>");
 Response.Write("<div style="text-align:justify">");
 Response.Write(content);

 string jud = Convert.ToString(row["Content"]);

 Response.Write("");

 Response.Write("       
 <a href='#'  runat="server"  önclick=ShowContent()>");

 Response.Write("Read More...");
 Response.Write("");

 Response.Write("</div>");
 Response.Write("</li>");
 }
 }
 }                               
    %>

 </string></string></ul>
 </div>



here i am not able to send string jud as parameter. my javascript function is

C#
<script language="javascript" type="text/javascript">

    function ShowContent(jud) {

        PageMethods.ShowContent(jud,Success, Failure);

    }
    function Success(result) {
        window.open('showcontent.aspx', null,'left=200, top=30, height=550, width= 500, 
         status=no, resizable= yes, scrollbars= yes,toolbar= no,location= no, menubar=
                 no');
    }
    function Failure(error) {
        alert(error);
    }

   </script>
Posted
v4
Comments
Sergey Alexandrovich Kryukov 1-Dec-13 12:26pm    
What do you mean by "calling from a tag"? There is no such concept as "call" in HTML. You can call Javascript function from Javascript function used as an event handle for some HTML element, but where do you try to do that? In what line?
—SA

1 solution

Do you call your js function inside the ready method is as below ? .Check that.

XML
<script type="text/javascript">
$(document).ready(function() {

//your code

});
</script>
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Dec-13 12:29pm    
Apparently, OP didn't... Please see my comment to the question. Your answer is fair enough, this is the way to go (but not the only possible one). Voted 5.
—SA
Sampath Lokuge 1-Dec-13 12:53pm    
Thanks Sergey.Yep You're right.But no response from the OP yet :(
Sampath Lokuge 1-Dec-13 13:03pm    
Hi Sergey,
This is not related to the above question. But I would like to ask a question from you. That is How is going to find a time for spends this much of time with code project. Because you're the Number One contributor of this site always. Are you employee on this site or how you find the time ?
Sergey Alexandrovich Kryukov 1-Dec-13 13:24pm    
No, I'm not an employee, and I have another full time job... :-)
—SA
Sampath Lokuge 1-Dec-13 13:29pm    
Wow Great.Keep up the Great work for the Community :)

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