简体   繁体   English

如何在asp.net循环期间显示确认警报消息框

[英]How to show confirmation alert message box during loop in asp.net

During a loop of adding to new datarow in datatable, I want to show a confirmation box on specific condition.在添加到数据表中的新数据行的循环中,我想在特定条件下显示一个确认框。 So if user select "yes", the row will add to table or skip and loop will continue.因此,如果用户选择“是”,该行将添加到表中或跳过并继续循环。 For this I used ModalPopupExtender but problem is that modal is popup only after loop is completed, which useless in my case.为此,我使用了 ModalPopupExtender,但问题是只有在循环完成后才会弹出模态,这对我来说是无用的。

Here my code: Loop:这是我的代码:循环:

for(int i=0;i<dt2.Rows.count;i++)
{
 ....
 ....
 ....


        if (l > 0)
        {
             DataRow row = datarow1(dt2,dt3,i);                        
             dt3.Rows.Add(row);
        }
        else
        {
             ModalPopupExtender1.Show();
                if ((bool)ViewState["cnfirm"] == true)
                {
                     DataRow row = datarow1(dt2, dt3, i);
                     dt3.Rows.Add(row);
                }
                
        }
}

And

protected void Decision_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandArgument == "Yes")
        {
            ViewState["cnfirm"] = true;
        }
        else
        {
            ViewState["cnfirm"] = false;
        }

    }

Please help to resolve this issue or suggest another/easiest way.请帮助解决此问题或提出另一种/最简单的方法。

As noted, you need to let the page render - you can't interact with user DURING a page create process - only after, and only after the page travels down to user.如前所述,您需要让页面呈现 - 您不能在页面创建过程中与用户交互 - 只有在页面向下传递给用户之后,并且仅在页面向下传播之后。

You are much better off to do this:你最好这样做:

Say our gride - drop in a check box.说我们的网格 - 放入一个复选框。

So, markup is this:所以,标记是这样的:

<div style="width:50%;padding:25px; margin-left: 40px;">

<asp:GridView ID="GVHotels" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="ID" CssClass="table"  >
    <Columns>
        <asp:BoundField DataField="FirstName"   HeaderText="FirstName" />
        <asp:BoundField DataField="LastName"    HeaderText="LastName"  />
        <asp:BoundField DataField="City"        HeaderText="City" />
        <asp:BoundField DataField="HotelName"   HeaderText="HotelName"  />
        <asp:BoundField DataField="Description" HeaderText="Description"  />

        <asp:TemplateField HeaderText="To Process" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:CheckBox ID="chkSel" runat="server" CssClass="bigcheck" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

    <asp:Button ID="cmdProcess" runat="server" Text="Process selected" CssClass="btn" Width="153px"  />
    <br />
    <br />

And code to fill is this:要填写的代码是这样的:

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadGrid();
    }

    void LoadGrid()
    {
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            using (SqlCommand cmdSQL = new SqlCommand("SELECT * from tblHotels ORDER by HotelName", conn))
            {
                conn.Open();
                DataTable rstData = new DataTable();
                rstData.Load(cmdSQL.ExecuteReader());
                GVHotels.DataSource = rstData;
                GVHotels.DataBind();
            }
        }
    }

Output is now this:输出现在是这样的:

在此处输入图片说明

And now it is a simple process to check for the check box like this:现在检查复选框是一个简单的过程,如下所示:

   protected void cmdProcess_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gRow in GVHotels.Rows)
        {
            CheckBox ckSel = (CheckBox)gRow.FindControl("chkSel");

            if (ckSel.Checked)
            {
                int PKID = (int)GVHotels.DataKeys[gRow.RowIndex]["ID"];

                // code here to process data based on database PKID
                Debug.Print("to process database PK ID = " + PKID);                    
            }
        }
    }

So, you can't interact with the user DURING the loop - that loop is running server side, and the browser is waiting for the WHOLE page to come back.因此,您无法在循环期间与用户交互 - 该循环正在服务器端运行,而浏览器正在等待整个页面返回。 Only AFTER all code behind runs does the whole copy of the web page travel back to the client.只有在所有代码运行后,网页的整个副本才会返回给客户端。

You can certainly replace the check box with a button, and pop a cool dialog box to ask for yes/no, or delete this for each row - but there not a practical way to do this in that loop.您当然可以用按钮替换复选框,并弹出一个很酷的对话框来询问是/否,或者为每一行删除它 - 但在该循环中没有实用的方法来做到这一点。 So, code behind MUST finish, MUST exit, and then and only then does the WHOLE page get send down to client side.所以,后面的代码必须完成,必须退出,然后整个页面才会被发送到客户端。 Once that has occurred, then you can start to interact with the user.一旦发生这种情况,您就可以开始与用户进行交互。 As noted, in place of a check box, you could drop in a button - delete this, or remove this?如前所述,代替复选框,您可以放入一个按钮 - 删除它,还是删除它? And that can be nice looking dialog box (say bootstrap, or better jQuery.UI dialog).这可以是漂亮的对话框(比如引导程序,或者更好的 jQuery.UI 对话框)。 So you can conditional prompt the user - but only after the page has rendered client side.所以你可以有条件地提示用户 - 但只有在页面呈现客户端之后。

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

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