【问题标题】:UpdatePanel in a UserControl (a nested Forms problem)UserControl 中的 UpdatePanel(嵌套表单问题)
【发布时间】:2009-12-23 21:38:45
【问题描述】:

我的 UserControl 有问题。它在 UpdatePanel 内,但必须放在标签内才能工作(否则 PostBack 不是异步的)

问题是,如果我把那个 UserControl 放在例如Default.aspx页面,出现错误 "一个页面只能有一个服务器端的 Form 标签"

如何应对?

[编辑]

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>

【问题讨论】:

  • 你能发布一些示例代码吗?

标签: asp.net asp.net-ajax updatepanel


【解决方案1】:

你的代码应该是:

<%@ 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 的解决方案总是发回服务器。

【讨论】:

  • 我解决了这个问题,我使用了你的代码,然后我将 asp:ScriptManager 从 .ascx 文件移到了我的 Default.aspx 页面。那行得通! :)我会检查你的 web.config 解决方案,听起来很有用谢谢你的帮助!
【解决方案2】:

您应该检查用户控件和页面,并确保您的标记中只有一个&lt;form id='form1' runat='server'&gt; 标记。拥有不止一个会给您带来您遇到的错误。

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

【讨论】:

  • 我知道,但如果只有 1 个表单,UpdatePanel 将无法“工作”
猜你喜欢
  • 1970-01-01
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 1970-01-01
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
相关资源
最近更新 更多