简体   繁体   English

带有Asp.NET代码的JQuery消息

[英]JQuery Message with Asp.NET code behind

Don't have much knowledge on JQuery/CSS and would be helpful if someone could help me here. 没有太多有关JQuery / CSS的知识,如果有人可以在这里帮助我,将对您有所帮助。

The idea is to show a client side message box using JQuery while grabbing some data from code behind. 我们的想法是使用JQuery显示客户端消息框,同时从后面的代码中获取一些数据。 The data from server is dynamic and will be assigned to the function parameter on button click in code behind. 来自服务器的数据是动态的,并且将在后面的代码中单击按钮时分配给函数参数。

Issue: It is working as expected only once. 问题:它只按预期工作一次。 Second time onwards, I am able to see the message pops up but no contents ( anything I defined within message div) 第二次,我能够看到弹出的消息,但没有内容(我在消息div中定义的任何内容)

Script: 脚本:

 function ShowPopup(message) { $(function () { $("#dialog").dialog({ height: 400, width: 500, modal: true, position: { my: 'top', at: 'top+350' }, title: message, buttons: { Close: function () { $(this).dialog("close"); } } } ); }); }; 

Html: HTML:

  <div id="dialog" class="ui-helper-hidden"> <div id="tabs"> <ul> <li><a href="#tabs-1" >Proin elit arcu</a></li> <li><a href="#tabs-2">Rutrum commodo</a></li> </ul> <div id="tabs-1"> <p>Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.</p> </div> <div id="tabs-2"> <p>Phasellus ipsum. Nunc tristique tempus lectus.</p> </div> </div> </div> 

Code behind: 代码背后:

 protected void BtnMoreInfo_OnClick(object sender, EventArgs e)
    {
        string message = "Message from server side";
        ScriptManager.RegisterStartupScript((sender as Control), this.GetType(), "Popup", "ShowPopup('" + message + "');", true);
    }

can you please use the dialog code inside a button click, and i assume your request was not Jquery AJax. 你可以在点击按钮内使用对话框代码,我假设你的请求不是Jquery AJax。 so on page load/or Dynamically after page Load place our message in HTML DOM and take that message on button click, something like this, 因此在页面加载时/或在页面加载后动态地将我们的消息放入HTML DOM中,然后在单击按钮时获取该消息,类似这样,

    $(function () {
    $('.yourButton').click(function () {
        var message = $('#messageDiv').attr('data-message');
        $("#dialog").dialog({
            height: 400,
            width: 500,
            modal: true,
            position: {my: 'top', at: 'top+350'},
            title: message,
            buttons: {
                Close: function () {
                    $(this).dialog("close");

                }
            }
        }
        );
    });
});

then place your message in html DOM, i prefer it as data attribute. 然后将您的消息放在html DOM中,我更喜欢它作为数据属性。

html HTML

<div id="messageDiv" data-message="yourMessage"></div>

thank you and let me know if it works as expected. 谢谢,让我知道它是否按预期工作。

if you dont have ScriptManager in your page, you can create a function in C# code, and then call this function each time you want to show the message. 如果您的页面中没有ScriptManager ,则可以使用C#代码创建一个函数,然后在每次要显示消息时调用此函数。 This is my example: 这是我的例子:

protected void Message(string str)
{
  StringBuilder stringBuilder = new StringBuilder();
  stringBuilder.Append("<script type = 'text/javascript'>");
  stringBuilder.Append("window.onload=function(){");
  stringBuilder.Append("alert('");
  stringBuilder.Append(str);
  stringBuilder.Append("')};");
  stringBuilder.Append("</script>");
  this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", stringBuilder.ToString());
}

Hope this works for you. 希望这对你有用。

It turnout to be a duplicating Div, which was part of master page. 原来是重复的Div,它是母版页的一部分。 Mentioning unique name solved this issue. 提及唯一名称即可解决此问题。 Sorry being an ignorant. 抱歉,我是个无知的人。

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

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