UserControl 中的 UpdatePanel(嵌套表单问题)

发布于 2024-08-16 10:27:02 字数 2304 浏览 2 评论 0原文

我的用户控件有问题。它位于 UpdatePanel 内部,但必须将其放置在标记内才能工作(否则,PostBack 不是异步的)

问题是,如果我将该 UserControl 放在例如 Default.aspx 页面中,则会出现错误 “一个页面只能有一个服务器端表单标签”

如何处理?

[编辑]

UserControl.ascx 代码:

 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PodmiotyZwolnioneCtrl.ascx.cs" Inherits="FormularzPodatkowy.PodmiotyZwolnioneCtrl" %>
 <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>

<link href="AjaxStyle.css" rel="stylesheet" type="text/css" />

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<body>
<form id="form1" runat="server">
<div>
        <asp:UpdatePanel ID="UpdatePanel1" 
                         UpdateMode="Conditional"
                         runat="server">
            <ContentTemplate>
               <fieldset>
               <legend>UpdatePanel content</legend>
                <!-- Other content in the panel. -->
                <%=DateTime.Now.ToString() %>
                <br />
                <asp:Button ID="Button1" 
                            Text="Refresh Panel" 
                            runat="server" />
                </fieldset>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
</form>
</body>
</html>

WebForm1.aspx 代码:

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="FormularzPodatkowy.WebForm1" %>

 <%@ Register src="PodmiotyZwolnioneCtrl.ascx" tagname="PodmiotyZwolnioneCtrl" tagprefix="uc1" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <uc1:PodmiotyZwolnioneCtrl ID="PodmiotyZwolnioneCtrl1" runat="server" />
</form>
 </body>
</html>

I have problem with my UserControl. It has inside the UpdatePanel, but it must be placed inside the tag to get worked (otherwise, the PostBack is not asynchronous)

The problem is, if I put that UserControl in e.g. Default.aspx page, the error appears
"A page can have only one server-side Form tag"

how to cope with that ?

[edit]

the UserControl.ascx code:

 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PodmiotyZwolnioneCtrl.ascx.cs" Inherits="FormularzPodatkowy.PodmiotyZwolnioneCtrl" %>
 <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>

<link href="AjaxStyle.css" rel="stylesheet" type="text/css" />

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<body>
<form id="form1" runat="server">
<div>
        <asp:UpdatePanel ID="UpdatePanel1" 
                         UpdateMode="Conditional"
                         runat="server">
            <ContentTemplate>
               <fieldset>
               <legend>UpdatePanel content</legend>
                <!-- Other content in the panel. -->
                <%=DateTime.Now.ToString() %>
                <br />
                <asp:Button ID="Button1" 
                            Text="Refresh Panel" 
                            runat="server" />
                </fieldset>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
</form>
</body>
</html>

the WebForm1.aspx code:

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="FormularzPodatkowy.WebForm1" %>

 <%@ Register src="PodmiotyZwolnioneCtrl.ascx" tagname="PodmiotyZwolnioneCtrl" tagprefix="uc1" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <uc1:PodmiotyZwolnioneCtrl ID="PodmiotyZwolnioneCtrl1" runat="server" />
</form>
 </body>
</html>

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

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

发布评论

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

评论(2

半世晨晓 2024-08-23 10:27:02

您的代码应该是:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PodmiotyZwolnioneCtrl.ascx.cs" Inherits="FormularzPodatkowy.PodmiotyZwolnioneCtrl" %>
 <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>

<link href="AjaxStyle.css" rel="stylesheet" type="text/css" />

<div>
        <asp:UpdatePanel ID="UpdatePanel1" 
                         UpdateMode="Conditional"
                         runat="server">
            <ContentTemplate>
               <fieldset>
               <legend>UpdatePanel content</legend>
                <!-- Other content in the panel. -->
                <%=DateTime.Now.ToString() %>
                <br />
                <asp:Button ID="Button1" 
                            Text="Refresh Panel" 
                            runat="server" />
                </fieldset>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>

看起来您将页面转换为控件,但忘记删除页面特定的元素

另外,如果您的回发不是异步的,请查看您的 web.config:

    <xhtmlConformance mode="Legacy"/>

并将其更改为:

    <xhtmlConformance mode="Transitional"/>

我刚刚花了三个几天的工作发现 UpdatePanel 的解决方案总是回发到服务器。

Your code should be:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PodmiotyZwolnioneCtrl.ascx.cs" Inherits="FormularzPodatkowy.PodmiotyZwolnioneCtrl" %>
 <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>

<link href="AjaxStyle.css" rel="stylesheet" type="text/css" />

<div>
        <asp:UpdatePanel ID="UpdatePanel1" 
                         UpdateMode="Conditional"
                         runat="server">
            <ContentTemplate>
               <fieldset>
               <legend>UpdatePanel content</legend>
                <!-- Other content in the panel. -->
                <%=DateTime.Now.ToString() %>
                <br />
                <asp:Button ID="Button1" 
                            Text="Refresh Panel" 
                            runat="server" />
                </fieldset>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>

It looks like you converted a page to a control and forgot to remove the page-specific elements

Also, if your postbacks are not asynchronous, look at your web.config for:

    <xhtmlConformance mode="Legacy"/>

and change it to:

    <xhtmlConformance mode="Transitional"/>

I just spent three days at work finding that solution to UpdatePanel always posting back to the server.

云淡月浅 2024-08-23 10:27:02

您应该检查用户控件和页面,并确保标记中只有一个

标记。拥有多个将会给您带来您所遇到的错误。

祝你好运,希望这对一些人有帮助。

You should check both the usercontrol and the page and make sure that you only have one <form id='form1' runat='server'> tag in your markup. Having more than one will give you the error that you're experiencing.

Good luck, and hope this helps some.

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