在 AsyncPostBackTrigger 上从 ASP UpdatePanel 中的文本框检索值

Retrieve Value from Textbox in ASP UpdatePanel on AsyncPostBackTrigger

我在 JQuery 对话框中嵌入用户控件时遇到问题。在这个用户控件中是一个简单的联系表单,它利用 UpdatePanel 异步访问服务器,并提交数据而不会导致整个页面回发。我面临的问题是,当到达服务器时,我无法从更新面板的文本框中获取值。请参阅下面的代码片段,如果您有任何问题,请告诉我。谢谢。

ASCX 更新面板

<asp:UpdatePanel ID="contactPanelID" runat="server"> 
<ContentTemplate>
    <div>
        <h2>CONTACT US</h2>
        <p type="Name:"><asp:TextBox id="nameText" runat="server" cssclass="contactInput" placeholder="Write your name here.."/></p>             
        <p type="Email:"><asp:TextBox id="emailText" runat="server" cssclass="contactInput" placeholder="Let us know how to contact you back.."/></p>
        <p type="Message:"><asp:TextBox id="messageText" runat="server" cssclass="contactInput" placeholder="What would you like to tell us.."/></p>
        <asp:LinkButton id="ContactSubmit" cssclass="contactBtn" runat="server" Text="Send Message" />
        <asp:label runat="server" id="responseLabel" cssclass="contactInput"></asp:label>
    </div>
</ContentTemplate>
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="ContactSubmit" />
</Triggers>   
</asp:UpdatePanel>  

VB 用户控制

Protected Sub lnk_ContactSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContactSubmit.Click
    responseLabel.Text = nameText.Text ' WHY IS THIS VALUE BLANK?
End Sub

Web.Master 嵌入式用户控件

    <div id="dialog" title="Basic dialog" style="display: none" >
        <web:ContactUs runat="server" ></web:ContactUs>   
    </div>

Web.Master Javascript 打开对话框

$("#dialog").dialog({
            dialogClass: 'contactDialog'
        });    

为阿尔伯特编辑 从下图中可以看出,当我附加到表单时,它几乎禁用了我现在添加的对话框的模式选项。对不起,如果我只是遗漏了一些简单的东西,但我很困惑为什么对话框没有打开。

var myDialog = $("#EditRecord");
        myDialog.dialog({
            title: "Edit Hotel Information",
            appendTo: "form1",
            modal: true,
            width: 860 
        });

为阿尔伯特编辑 2

这是我的 Web.Master 的示例(简化)

<head id="Head1" runat="server">
  <script type="text/javascript" language="javascript">
    function OpenDialog(){
        $("#dialog").dialog({
            width: 500,
            height: 550,
            modal: true,
            dialogClass: "myTitleClass"
        });
  }
  </script>
  <body style=" margin-top:1px; text-align:center;" id="body_master" runat="server">
    <form id="form1" runat="server">
       <!-- All kinds of unrelated content -->
      <div id="dialog" style="display: none;" title="Contact Us">
        <web:ContactUs runat="server" ></webrg:ContactUs>  <!-- This is my user control --> 
      </div>
    </form>
  </body>
</head>

已更新用户控件 ascx 代码

<asp:UpdatePanel ID="contactPanelID" runat="server"> 
<ContentTemplate>
    <div>
        <p type="Name:"><asp:TextBox id="nameText" runat="server" cssclass="contactInput" placeholder="Write your name here.."/></p>             
        <p type="Email:"><asp:TextBox id="emailText" runat="server" cssclass="contactInput" placeholder="Let us know how to contact you back.."/></p>
        <p type="Message:"><asp:TextBox id="messageText" runat="server" cssclass="contactInput" placeholder="What would you like to tell us.."/></p>
        <asp:LinkButton id="ContactSubmit" cssclass="contactBtn" runat="server" Text="Send Message" />
        <asp:label runat="server" id="responseLabel" cssclass="contactInput"></asp:label>
    </div>
</ContentTemplate>
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="ContactSubmit" />
</Triggers>   

在 VAST 的大多数情况下,来自更新面板的 post-back 将导致对话框折叠。但是,我发现如果你把 div OUT 放在更新面板的一侧,并让 jquery.ui 弹出 div,然后 div 里面就是你的更新面板,然后 jquery.ui 对话框可以存活并有 post-backs。请记住,虽然更新面板仅 post 支持更新面板中的部分?它实际上是我们所说的“部分”post-back,甚至网页加载甚至会触发,并且每次您从该更新面板内部 post-back 时都会触发。移动div你弹出包围起来。

如果UP里面的按钮有事件,那么就不需要触发设置。您也没有显示 #Dialog div,但如前所述,您希望这个 div 包围 up,而不是将 UP 嵌套在 pop div.[=25 中=]

下一个:

YES 你经常会发现当你在 UP 中有那个 post-back 时,这些值是看不到的,或者正确地放回页面 DOM.

因此,将此添加到您的 jquery.UI 对话框中:

    <script>
        function PopEdit() {
            var myDialog = $("#EditRecord");
            myDialog.dialog({
                title: "Edit Hotel Information",
                modal: true,
                width: 860,
                appendTo: "form"
            });
        }
    </script>

注意上面的非常小心。 appendTo:“表单”。对于您弹出的大多数对话框,您通常不需要它,但是如果该对话框用于编辑数据,并且您要查看后面代码的更改,那么您需要 appendTo。

因此,请记住,虽然 UP 执行了部分 post-back,但您确实得到并看到了标准页面生命周期 post-back 发生在此处。

并且当您单击按钮时,视图状态也会 post 返回到 UP 中。所以,这不像你不会得到零 post-back。使用 UP 面板时,您会得到 post-back、页面加载和完整页面循环。

通常,由于 post-back 确实会折叠 UP,因此这可能是您设计的一部分。如果我弹出一个窗口,并有一个保存按钮(即 post-back),那就太好了,因为 post-back 会为我折叠(关闭)面板(这就是我想要的)。

所以,我经常使用 UP 中的 post-back 来关闭面板,因为作为一般规则,任何 post 返回都会使 UP 崩溃。但是,如果将“div”移到外面并包围 UP,则面板可以存活 post-backs,而不会崩溃。

所以,使用这个简单的网格 - 使用 pop jquery.UI dailog 来编辑一行。

我们有这个简单的网格:

    <div style="width:40%">
        <asp:GridView ID="GHotels" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="ID" CssClass="table">
            <Columns>
                <asp:BoundField DataField="FirstName" HeaderText="FirstName"  />
                <asp:BoundField DataField="LastName" HeaderText="LastName"    />
                <asp:BoundField DataField="HotelName" HeaderText="HotelName"  />
                <asp:BoundField DataField="Description" HeaderText="Description"  />
                <asp:TemplateField HeaderText="Edit">
                    <ItemTemplate>
                        <asp:Button ID="cmdEdit" runat="server" Text="Edit" CssClass="btn" OnClick="cmdEdit_Click" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>

加载网格的代码:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        LoadGrid
    End If

End Sub

Sub LoadGrid()

    Dim cmdSQL As New SqlCommand("SELECT * FROM tblHotelsA ORDER BY HotelName")
    GHotels.DataSource = MyrstP(cmdSQL)
    GHotels.DataBind()

End Sub

现在我们有了这个:

此外,您为什么要使用 link 按钮???? (我想没问题,但应该有充分的理由在常规按钮上做出选择)。

所以,在上面,我们有观点。那只是一个飞机简纽扣。

而我们点击时的代码是这样的:

Protected Sub cmdEdit_Click(sender As Object, e As EventArgs)

    Dim btn As Button = sender
    Dim gRow As GridViewRow = btn.NamingContainer
    Dim pkID = GHotels.DataKeys(gRow.RowIndex).Item("ID")

    Dim cmdSQL As New SqlCommand("SELECT * from tblHotelsA where ID = @ID")
    cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = pkID
    Dim rstData As DataTable = MyrstP(cmdSQL)

    Call fLoader(Me.EditRecord, rstData.Rows(0)) ' load up hidden div with data
    ViewState("rstData") = rstData
    ' lets call the js routine to pop our hidden edit div
    ClientScript.RegisterStartupScript(Page.GetType(), "PopEditKey", "PopEdit()", True)

End Sub

gv 的简单按钮单击事件获得 PK,加载隐藏的 div(jquery.ui 将弹出)。

客户端,我有一个简单的 jquery.ui 对话框来弹出我们的编辑 div。

代码是这样的:

    <script>
        function PopEdit() {
            var myDialog = $("#EditRecord");
            myDialog.dialog({
                title: "Edit Hotel Information",
                modal: true,
                width: 860,
                appendTo: "form"
            });
        }
    </script>

所以,现在,当我点击一行时,我得到这个:

现在,在上面,我有一个取消按钮。 (我使用了 html 按钮,因为我想要按钮的图标。但是,使用 id 和 运行at=server,它确实与标准 asp.net 按钮一样工作。

对于上面的 3 个按钮,我有这个:

        <button id="cmdSave" runat="server" class="btn" onserverclick="cmdSave_ServerClick" >
            <span aria-hidden="true" class="glyphicon glyphicon-floppy-saved"> Save</span> 
        </button>

        <button id="cmdCancel" runat="server" class="btn" style="margin-left:15px">
            <span aria-hidden="true" class="glyphicon glyphicon-arrow-left"> Back/Cancel</span>
        </button>

        <button id="cmdDelete" runat="server" class="btn" style="margin-left:15px">
            <span aria-hidden="true" class="glyphicon glyphicon-trash"> Delete</span>
        </button>

请注意取消按钮是如何使用零代码的。我只是让它 post-back,没有代码隐藏事件,但正如我所指出的,任何 post-back 都会关闭对话框,我很高兴并且很好。

保存按钮?当然,它在存根后面有一个代码,并且有这个:

Protected Sub cmdSave_ServerClick(sender As Object, e As EventArgs)

    Dim rstData As DataTable = ViewState("rstData")
    Call fWriterW(EditRecord, rstData.Rows(0))
    Call SaveTable(rstData, "tblHotelsA")
    LoadGrid()   ' re-display grid with changes

End Sub

再次说明,不需要关闭对话框的代码。

和删除按钮 - 理论上可以弹出另一个 jquery.ui 对话框来询问用户是或否。

但是,因为我确实允许编辑那个 pop div,然后再次注意 appendTo: 设置——这会将对话框推回“dom”,这样你后面的代码就会看到变化。

而且,我想我可以 post 流行 div 标记(但并不是那么重要)。但是,是这样的:

    <div id="EditRecord" runat="server" style="float:left;display: none">
        <style>
            .iForm label {display:inline-block;width:90px}
            .iForm input {border-radius:8px;border-width:1px;margin-bottom:10px}                
            .iForm textarea {border-radius:8px;border-width:1px;margin-bottom:10px}     
            .iForm input[type=checkbox] {margin-right:8px}
        </style>

        <div style="float:left" class="iForm">
                <label>HotelName</label><asp:TextBox ID="txtHotel" runat="server" f="HOtelName" width="280"></asp:TextBox> <br />
                <label>First Name</label><asp:TextBox ID="tFN" runat="server" f="FirstName" Width="140"></asp:TextBox> <br />
                <label>Last Name</label><asp:TextBox ID="tLN" runat="server" f="LastName" Width="140"></asp:TextBox> <br />
                <label>City</label><asp:TextBox ID="tCity" runat="server" f="City" Width="140"></asp:TextBox> <br />
                <label>Province</label><asp:TextBox ID="tProvince" runat="server" f="Province" Width="75"></asp:TextBox> <br />
        </div>
        <div style="float:left;margin-left:20px" class="iForm">
            <label>Description</label> <br />
            <asp:TextBox ID="txtNotes" runat="server" Width="400" TextMode="MultiLine" 
                Height="150px" f="Description" ></asp:TextBox> <br />
            <asp:CheckBox ID="chkActive" f="Active" Text=" Active" runat="server" TextAlign="Right" />
            <asp:CheckBox ID="chkBalcony" f="Balcony" Text=" Has Balcony" runat="server" TextAlign="Right" />
        </div>
        <div style="clear:both"></div>
        <button id="cmdSave" runat="server" class="btn" onserverclick="cmdSave_ServerClick" >
            <span aria-hidden="true" class="glyphicon glyphicon-floppy-saved"> Save</span> 
        </button>

        <button id="cmdCancel" runat="server" class="btn" style="margin-left:15px">
            <span aria-hidden="true" class="glyphicon glyphicon-arrow-left"> Back/Cancel</span>
        </button>

        <button id="cmdDelete" runat="server" class="btn" style="margin-left:15px">
            <span aria-hidden="true" class="glyphicon glyphicon-trash"> Delete</span>
        </button>
   </div>

所以,这就是我们弹出的 div。现在,我可以将内容放在 UP 中,因此面板中没有 post-backs 不会导致它关闭。 (但这意味着取消按钮、保存按钮,甚至删除按钮代码将负责关闭对话框。

编辑:div 与 运行at=server

不清楚您是否为 div 使用代码隐藏。您可以删除 div 的“id”和 运行at=server。但是,我经常需要(并且想要)代码来使用 div.

因此,要使 jquery 正常工作,请将其添加到 div:

 <div id="EditRecord" runat="server" 
    style="float:left;display: none" clientidmode="Static"  >

但是,话又说回来,如果你的后台代码不需要 div 作为后台代码中的控件,那么就不需要 运行at=server.

但是,如果您在 jquery.UI 中选择的 div 确实有一个 运行at=server 标签,您可以使用 clientIDmode="static&uot;,或者您可以将 jQuery 更改为:

var MyDialog = $('#<%= EditRecord.ClientID %>').dialog

因此,我不推荐或建议为 div 添加 运行at=server,但只有在您需要时才这样做(在我的示例中,我确实需要它)。

Edit2:母版页示例:

所以,假设我有这个:

            <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
            <asp:ScriptReference Name="WebFormsBundle" />
            <%--Site Scripts--%>
        </Scripts>
    </asp:ScriptManager>

            <script src="Scripts/jquery-ui.js"></script>
            <link href="Scripts/jquery-ui-1.12.1.css" rel="stylesheet" />

(我最喜欢 - 所以我为 jQuery.ui

添加了 jquery.ui 和 .css

然后再往下,菜单(又被删掉了大部分)。

我们有这个:

                    <li><a runat="server" href="~/About">About</a></li>
                    <li><a runat="server" href="~/Contact">Contact</a></li>
                    <li><a onclick="HotelEdit();">Edit Hotel</a>
                    </li>
                </ul>
            </div>
        </div>
    </div>

    <div id="HotelDialog" style="display:none">
        <uc1:HotelEd1 runat="server" id="HotelEd1" />
    </div>

    <script>
        function HotelEdit() {
                $("#HotelDialog").dialog({
                    width: 740,
                    height: 340,
                    modal: true,
                    title: "Edit Hotel",
                    appendTo: "form"
                });
            } 
    </script>
    

请注意我是如何将追加添加回的。

所以,当我们 运行 时,我们得到:

因此,我的用户控件弹出。并注意我如何 re-introduced appendTo: "form"。由于站点管理员实际上是“表单”页面,因此可能需要 appendTo 才能使弹出窗口内的按钮正常工作。