简体   繁体   English

asp.net链接按钮和jQuery模式弹出

[英]asp.net linkbutton and jquery modal pop

I Have a linkbutton to do some task , when i click the button i need to display the results in modal popup.. I created the buttons dynamically in code, and attached the event to it too like this 我有一个linkbutton来执行某些任务,当我单击按钮时,我需要在模式弹出窗口中显示结果。.我在代码中动态创建了按钮,并像这样将事件附加到它上

      LinkButton lnkBtn = new LinkButton();
      lnkBtn.CssClass = "lnk";
      lnkBtn.Text = Server.UrlDecode(r.URL);
      lnkBtn.CommandArgument = r.OriginalSentence;
      lnkBtn.Command += new CommandEventHandler(lnkBtn_Command);

in the event I have to Download the Html from the url, and display it in modal div tag 如果我必须从网址下载HTML,并以模式div标签显示它

 private void lnkBtn_Command(object sender, CommandEventArgs args)
{
    string URL = ((LinkButton)sender).Text;
    string HtmlDoc = DownloadURL(URL);
    string HighlightedHTML = HtmlDoc.Replace(((LinkButton)sender).CommandArgument, "<span                 style='background-color:red;'>" + ((LinkButton)sender).CommandArgument + "</span>");
    popup.InnerHtml = HighlightedHTML;

    StringBuilder strScript = new StringBuilder();


    strScript.Append("$(" + "\".lnk\"" + ").click(function () {");
    strScript.Append("$(\"#popup\").dialog('open');");
    strScript.Append(" return false; });");



   Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", strScript.ToString(), true);


}

every thing is done will the problem that I have to click the button twice, first to fire the event and register the script the second time to display the modal, I Need to display the modal from the first click.. Please help 一切都将解决的问题是我必须单击两次按钮,首先触发事件并第二次注册脚本以显示模式,我需要从第一次单击中显示模式。

You'll need to place your code that generates the Javascript in a page event like Page_Init. 您需要将生成Javascript的代码放在诸如Page_Init之类的页面事件中。 This way it will inject the javascript on each page load, rather than on link click. 这样,它将在每次页面加载时(而不是在链接点击时)注入javascript。

I solved the problem using Webmethod and ajax request 我使用Webmethod和Ajax请求解决了问题

[WebMethod]
public static string Highlight1(int _SentenceID)
{
    var QSentence = DocResultsBLL.ResultsList.Find(a => a.OriginalSentenceID == _SentenceID);
    string URL = QSentence.URL;
    string HtmlDoc = DownloadURL(URL);//search.Items[0].Link);
    string HighlightedHTML = HtmlDoc.Replace(QSentence.OriginalSentence, "<span style='background-color:red;'>" + QSentence.OriginalSentence + "</span>");
   // popup.InnerHtml = HighlightedHTML;
    return  HighlightedHTML;

}

then I Displayed the Dialog 然后我显示对话框

 $(".lnk").click(function () {
        $.ajax({
            type: 'POST',
            url: 'Google_Results.aspx/Highlight1',
            contentType: 'application/json; charset=utf-8',
            data: '{ _SentenceID:'+ $(this).attr('id') +'}', 
            dataType: 'json',
            success: function (msg) {
               // alert('Hello');
                document.getElementById("popup").innerHTML = msg.d;
               // $("#popup").html(msg.d);
                $("#popup").dialog("open");
                $("#popup").bPopup({
                    //            speed: 650,
                    //            transition: 'slideIn',
                    //            transitionClose: 'slideBack'
                    //       //    modalColor: 'greenYellow'
                    easing: 'easeOutBack', //uses jQuery easing plugin
                    speed: 450,
                    transition: 'slideDown'
                });
                return false;
            },
            failure: function (response) {
                alert('error');

            }

        });
        return false;

thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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