简体   繁体   English

使用JavaScript在asp.net网站中基于弹出窗口的用户选择来操纵网页?

[英]Manipulate webpage based on user selection of popup window in asp.net website using JavaScript?

I have an asp.net website. 我有一个asp.net网站。 I want to open a popup asking for user confirmation in code behind page. 我想在页面后面的代码中打开一个要求用户确认的弹出窗口。

I have a drop down with some values. 我有一些值下拉列表。 "cancel ticket" is one of those values. “取消票”是这些值之一。 So when user selects this item, I want to display a confirmation box asking user "Are you sure you want to cancel the ticket"? 因此,当用户选择此项目时,我想显示一个确认框,询问用户“您确定要取消该票证”吗?

I've tried some code using JavaScript, 我尝试过使用JavaScript的代码,

HTML: HTML:

<asp:DropDownList ID="ddlStatus" runat="server" CssClass="selectstyle" DataTextField="Name" DataValueField="ID" onchange ="GetSelectedItem(this);" />

JavaScript : JavaScript:

<script type="text/javascript">
    function GetSelectedItem(x) {
        if (x.value == 4) {
            return confirm("Are you sure you want to cancel support ticket ?");
        }
    }

which is displaying a popup as I want. 根据需要显示一个弹出窗口。

Now, I want to make a textbox and a label visible if user clicked on "OK" and reset dropdownlist if user clicked on "Cancel" 现在,如果用户点击“确定”,我想让文本框和标签可见,如果用户点击“取消”,则重置下拉列表

You would use 你会用的

ClientScriptManager.RegisterClientScriptBlock ClientScriptManager.RegisterClientScriptBlock

https://msdn.microsoft.com/en-us/library/bahh2fef%28v=vs.110%29.aspx https://msdn.microsoft.com/en-us/library/bahh2fef%28v=vs.110%29.aspx

The right way to approach this is by checking the value and prompting the user in the client side via javascript ( this will probably help you). 解决此问题的正确方法是检查值并通过javascript在客户端提示用户( 可能会对您有所帮助)。

But if you insist on going through the server, you can either: 但是,如果您坚持要通过服务器,则可以:

  • Do an ajax call and then prompt the user for confirmation, 进行ajax调用,然后提示用户进行确认,
  • or create another form/view to do that. 或创建另一个表单/视图来做到这一点。

Try adding a bootstrap modal and injecting script like ClientScriptManager.RegisterClientScriptBlock (Type, String,"$(modal).modal('show')") 尝试添加引导模式并注入脚本,如ClientScriptManager.RegisterClientScriptBlock (Type, String,"$(modal).modal('show')")

Do remember to add the bootstrap js and css 记得添加bootstrap js和css

My suggestion is: 我的建议是:

1- Create a invisible div with your cancel button (and/or others elements/controls you want) 1-使用取消按钮(和/或您想要的其他元素/控件)创建一个不可见的div

<div id="div_dialog" style="display: none;" >
    <h3>Do you want to cancel?</h3>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button"  onclick="Button1_Click"/>
</div>

2- Use jquery to use your drop down as a trigger and dialog your hidden div 2-使用jquery将您的下拉列表用作触发器并对话您的隐藏div

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/start/jquery-ui.css"
        rel="stylesheet" type="text/css" />


<script type="text/javascript">
    $(document).ready(function () {
      // this will open the popup when drop donw change 
      //(aply your filter by selected item)
        $("#<%=DropDownList1.ClientID%>").change(function () {
            $("#div_dialog").dialog({
                dialogClass: 'DynamicDialogStyle',
                height: 160,
                width: 220,
                title: "My modal Dialog Popup",               
                modal: true
            });
            return false;
        });
        // this function will do the postback and fire the event in the popup button
       $('#<%=Button1.ClientID%>').click(
         function() {
            <%=this.Page.ClientScript.GetPostBackEventReference(new PostBackOptions(this.Button1))%>;
       });
    });

</script>

3- Instead of using ddlStatus_Tickets_SelectedIndexChanged event, handle your confirmation code in the confirmation button event click 3-而不是使用ddlStatus_Tickets_SelectedIndexChanged事件,在确认按钮事件中处理您的确认代码

    protected void Button1_Click(object sender, EventArgs e)
    {
        // you code
    }

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

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