如何从 C# 打开对话框(模式)

发布于 2024-10-28 07:45:36 字数 6465 浏览 1 评论 0原文

我的 javascript:

/* Modal */

function DefaultModal() {
    $(".ModalBox").dialog("open");
}

$(function () {
    // Modal.
    $(".ModalBox").dialog({
        autoOpen: false,
        height: 410,
        resizable: false,
        draggable: false,
        width: 602,
        modal: true,
        open: function (type, data) {
            // include modal into form
            $(this).parent().appendTo($("form:first"));
        },
        buttons: {
            "Confirm": function () {

            }
        },
        close: function () {
//clear all text.
            $(this).find(':text').val('');
        }
    });
});

我有两种方法来调用我的模态:

这一种工作完美:

<li><a href="javascript:void(0)" onclick="DefaultModal();">ADD contact</a></li>

而这一种不行。 我需要一个按钮来从我的临时表编辑到我的模态。


            <asp:GridView ID="gvContato" runat="server" AutoGenerateColumns="False" OnRowCommand="RunComm"
                    DataKeyNames="IDCliente" AllowPaging="True" PageSize="10" Width="600px">
                    <Columns>
                <asp:BoundField DataField="IDCliente" Visible="false" />
                        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                        <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
                        <asp:BoundField DataField="Email" HeaderText="E-mail" SortExpression="Email" />

                        <asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit">
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:ButtonField>
                    </Columns>
                </asp:GridView>
protected void RunComm(object sender, GridViewCommandEventArgs e)
        {
            int IDSelecionado = (int)gvContato.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[0];

            switch (e.CommandName)
            {
                case "Edit":
                    if (IDSelecionado > 0)
                    {
                        ScriptManager.RegisterClientScriptBlock(this.upModalContato, this.upModalContato.GetType(), "Modal", "DefaultModal();", true);
                    }
                    break;
            }

        }

我的编辑按钮正在执行以下操作:

他打开我的对话框,而且还在我的正文中显示它(不仅作为模态,而且还作为我的 gridview 下面的 DIV)。除此之外,如果我继续打开和关闭对话框(使用编辑按钮)而不保存信息,他就会继续在我的所有文本框中创建逗号;就像你在 javascript 中做一些数组一样。

我不知道为什么!?

有什么想法吗?我不确定我是否在这里让自己可以被理解,如果没有,请告诉我我失踪了。

谢谢,问候。

编辑:

PS:我正在使用 UpdatePanel!

编辑2:错字

编辑3:我为什么要这样做?

我的目标:

  • 使用我的数据库中的数据(真实数据)加载一个网格视图。
  • 放入从我的模态保存的 gridview 临时数据(临时数据)。
  • 永久排除/编辑(在模式中)我的临时数据。
  • 暂时排除/编辑(在模式中)我的真实数据。

完成所有更改后,将最终数据(临时数据和实际数据)保存在我的数据库中。

为了实现这一点,我将数据保存在我的视图状态中(我知道很糟糕,但我现在没有想法)

遵循 @NerdFury 解决方案,我做了这个:(这仍然不能按我的需要工作!他打开模式,但不会加载字段中的数据。此代码还可以在我的html正文中打开我的表格(模式) ,带有数据。)

CSharp:


        protected void btnEditModal_Click(object sender, EventArgs e)
        {
            this.ddlDepartamentoContato.SelectedIndex = 1;
            this.ddlCargoContato.SelectedIndex = 2;
            this.ckAprovadorContato.Checked = true;

        }

html:

                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Button ID="btnEditModal" runat="server" Text="Edit" OnClientClick="DefaultModal();" CausesValidation="false" OnClick="btnEditModal_Click" />
                            </ItemTemplate>
                        </asp:TemplateField>

但我仍然遇到模式问题,对话框打开,但表格也出现在屏幕上。我在按钮事件中使用了一些静态数据,以查看它是否正常工作,然后他将这些数据加载到表中,但不在模态中。

PS:我想(至少我相信)问题是什么。

asp:UpdatePanel

如果我从代码中删除它,我将加载数据并且一切工作都完美无缺,但是它会进行回发,并且我的模态框几乎不会出现!他只是在我的屏幕上“眨眼”。

看看我的实际代码:

<asp:UpdatePanel ID="upModalContact" runat="server">
<ContentTemplate>
    <table>
        <tr>
            <td>
                <ul id="contatoModalButtonList">
                    <li><a href="javascript:void(0)" onclick="DefaultModal();">Add contact</a></li>
                </ul>
            </td>
        </tr>
        <tr>
            <td>
                <asp:GridView ID="gvContact" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="gvContact_PageIndexChanging"
                    DataKeyNames="IDCliente" AllowPaging="True" PageSize="10" Width="600px">
                    <Columns>

                        <asp:BoundField DataField="IDClient" Visible="false" />
                        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                        <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
                        <asp:BoundField DataField="Email" HeaderText="E-mail" SortExpression="Email" />

                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Button ID="btnEditContactmodal" runat="server" Text="Edit" CausesValidation="false" OnClick="btnEditModal_Click" OnClientClick="DefaultModal();" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </td>
        </tr>
    </table>

</ContentTemplate>
</asp:UpdatePanel>

并且

<table class="ModalBox" title="Contact">
<tr>
    <td colspan="2">
        <p class="validateTips"> Fields required (*).</p>
    </td>
</tr>
<tr>
    <td align="left" width="130px">
        <asp:Label Text="Name:" runat="server" ID="lblNameContact" />
    </td>
    <td>
        <asp:TextBox runat="server" ID="txtNameContact" CssClass="classNameContact" Width="400px" MaxLength="50" /> &nbsp; <label>*</label>
    </td>
</tr></table>

My javascript:

/* Modal */

function DefaultModal() {
    $(".ModalBox").dialog("open");
}

$(function () {
    // Modal.
    $(".ModalBox").dialog({
        autoOpen: false,
        height: 410,
        resizable: false,
        draggable: false,
        width: 602,
        modal: true,
        open: function (type, data) {
            // include modal into form
            $(this).parent().appendTo($("form:first"));
        },
        buttons: {
            "Confirm": function () {

            }
        },
        close: function () {
//clear all text.
            $(this).find(':text').val('');
        }
    });
});

i have two ways to call my modal:

This one Works flawless:

<li><a href="javascript:void(0)" onclick="DefaultModal();">ADD contact</a></li>

and this one, not. i need one button that edit from my temporary table, into my modal.


            <asp:GridView ID="gvContato" runat="server" AutoGenerateColumns="False" OnRowCommand="RunComm"
                    DataKeyNames="IDCliente" AllowPaging="True" PageSize="10" Width="600px">
                    <Columns>
                <asp:BoundField DataField="IDCliente" Visible="false" />
                        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                        <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
                        <asp:BoundField DataField="Email" HeaderText="E-mail" SortExpression="Email" />

                        <asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit">
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:ButtonField>
                    </Columns>
                </asp:GridView>
protected void RunComm(object sender, GridViewCommandEventArgs e)
        {
            int IDSelecionado = (int)gvContato.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[0];

            switch (e.CommandName)
            {
                case "Edit":
                    if (IDSelecionado > 0)
                    {
                        ScriptManager.RegisterClientScriptBlock(this.upModalContato, this.upModalContato.GetType(), "Modal", "DefaultModal();", true);
                    }
                    break;
            }

        }

My edit button is doing this:

he opens my dialog, but also, show it in my body (not only as modal, but also as DIV below my gridview). Besides that, if i keep opening and closing my dialog (using my edit button) without save the information, he keep creating commas in all my textbox; Like when you do some array in javascript.

i don't know why!?

Any ideas? I'm not sure if I made ​​myself understandable here, if not, tell me I'm missing.

Thanks, Regards.

EDIT:

PS: i'm using UpdatePanel!

EDIT2: TYPO

EDIT3: Why am i doing this?

My goal:

  • Load one gridview with data from my database (real data).
  • put in this gridview temporary data saved from my modal (temporary data).
  • Exclude/Edit (in the modal), permanently, my temporary data.
  • Exclude/Edit (in the modal), temporarily, my real data.

after all changes, saved the final data (temporary and reals data) in my database.

for achieve this, i'm saving my data in my view state (i know sucks, but i'm out of ideas at moment)

Following @NerdFury solution, i made this: (This still not work as i need! he opens the modal, but does not load the data in the fields. this code also, open my table (modal) in my html body, with the data.)

CSharp:


        protected void btnEditModal_Click(object sender, EventArgs e)
        {
            this.ddlDepartamentoContato.SelectedIndex = 1;
            this.ddlCargoContato.SelectedIndex = 2;
            this.ckAprovadorContato.Checked = true;

        }

html:

                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Button ID="btnEditModal" runat="server" Text="Edit" OnClientClick="DefaultModal();" CausesValidation="false" OnClick="btnEditModal_Click" />
                            </ItemTemplate>
                        </asp:TemplateField>

But i still have the problem with the modal, the dialog opens, but the table also appears on the screen. I used some static data in the button event, to see if it's working, and he load this data in the table, but not in the modal.

PS: I figured (i believe at least), what is the problem.

asp:UpdatePanel

IF i remove it from my code, i'll load the data and everything works flawless, BUT it make the postback, and my Modal almost does not appear! He just "blink" in my screen.

Look at my actual code:

<asp:UpdatePanel ID="upModalContact" runat="server">
<ContentTemplate>
    <table>
        <tr>
            <td>
                <ul id="contatoModalButtonList">
                    <li><a href="javascript:void(0)" onclick="DefaultModal();">Add contact</a></li>
                </ul>
            </td>
        </tr>
        <tr>
            <td>
                <asp:GridView ID="gvContact" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="gvContact_PageIndexChanging"
                    DataKeyNames="IDCliente" AllowPaging="True" PageSize="10" Width="600px">
                    <Columns>

                        <asp:BoundField DataField="IDClient" Visible="false" />
                        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                        <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
                        <asp:BoundField DataField="Email" HeaderText="E-mail" SortExpression="Email" />

                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Button ID="btnEditContactmodal" runat="server" Text="Edit" CausesValidation="false" OnClick="btnEditModal_Click" OnClientClick="DefaultModal();" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </td>
        </tr>
    </table>

</ContentTemplate>
</asp:UpdatePanel>

And

<table class="ModalBox" title="Contact">
<tr>
    <td colspan="2">
        <p class="validateTips"> Fields required (*).</p>
    </td>
</tr>
<tr>
    <td align="left" width="130px">
        <asp:Label Text="Name:" runat="server" ID="lblNameContact" />
    </td>
    <td>
        <asp:TextBox runat="server" ID="txtNameContact" CssClass="classNameContact" Width="400px" MaxLength="50" />   <label>*</label>
    </td>
</tr></table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剪不断理还乱 2024-11-04 07:45:36

在标记中,您将 CommandName 设置为“Editar”,然后在事件处理程序中检查 CommandName“Edit”。

我没有看到您在任何地方使用 id 值,但这可能是因为您试图在最终确定数据/行为之前让弹出窗口先工作。但我的另一个建议是考虑使用模板字段而不是按钮字段,并使用按钮上的 OnClientClick 事件。

In your markup, you are setting the CommandName to 'Editar', then in the event handler, you are checking for the CommandName 'Edit'.

I don't see anywhere that you are using the id value, but that might be because you are trying to get the popup to work first before finalizing the data/behavior. But my other suggestion would be to look into using a template field instead of a buttonfield and use the OnClientClick event on a button instead.

冰雪梦之恋 2024-11-04 07:45:36

我做到了!!!

我使用 2 个更新面板来完成此任务。基于此链接 (谢谢先生!)

<div class="ModalContact" style="display:none;width:375px;" title="Contact">
<asp:UpdatePanel ID="upModalContact" runat="server">
    <ContentTemplate>
        <table>

        </table>
    </ContentTemplate>
</asp:UpdatePanel>

需要注意的是,更新面板嵌套在对话框 div 内。而且用于显示列表的 GridView 也在更新面板内

I DID IT!!!

i use 2 update panels to accomplish this. Based on this link (Thank you sir!)

<div class="ModalContact" style="display:none;width:375px;" title="Contact">
<asp:UpdatePanel ID="upModalContact" runat="server">
    <ContentTemplate>
        <table>

        </table>
    </ContentTemplate>
</asp:UpdatePanel>

It is important to note that the Update Panels are nested inside the dialog div. And also the GridView to display the list is also within an Update Panel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文