简体   繁体   中英

I need to execute Java script function from c# code behind

I need to execute Java script function from c# code behind . Using Asp.net server control Repeater and checking condition when condition is true need to run java script function which will turn hyper link display block this is my javascript function

function MyFunction(username) {
  document.getElementById("hyp").style.display = "block";
}

This is my Asp.net code after that condition if true I call c# code behind method writeText() :

<%# Eval("part2").ToString() == "part2" ? writeText(Eval("Albumtitle").ToString(), Eval("Albumtitle").ToString(), inc.ToString()) : writeLink(Eval("Albumtitle").ToString(), Eval("Albumtitle").ToString())%>

protected string writeText(string PageDataId, string AlbumId, string a)
{
    ClientScript.RegisterStartupScript(this.GetType(), "hwa", "MyFunction('Checkjj');", true);
    string html = "";
    html += "";
    return html;
}

This c# call javascript function which display html anchor to block which was display none at the start

Here is that anchor tag

<a id="hyp" name="hyp"  href="#" class="lightbox<%=inc+"b" %>">Part II</a>

With your html: <a id="hyp" name="hyp" ... add the attribute runat="server" . Then on the server side you can access the object like if it were a server control.

There is not need to execute Javascript on the server side to perform the action you need.

Something like on "ItemDataBound" Event for the repeater:

  void MyRepeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
string someVar = "someValue"  ;  
           ((HtmlAnchor)e.Item.FindControl("hyp")).Attributes.Add ("css", "lightbox" + someVar);          
       }  

In order to use this event don't forget to add the event and the runat server attribute :

<asp:Repeater ID="MyRepeater" runat="server" OnItemDataBound="MyRepeater_ItemDataBound">

       <a id="hyp" name="hyp"  href="#" runat="server">Part II</a>

</asp:Repeater>  

You can find this blog helpful:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM