【问题标题】:Web User Control postback IssuesWeb 用户控制回发问题
【发布时间】:2011-12-05 09:15:25
【问题描述】:

我是 ASP.net 的新手,我有一些关于回发的问题。

我有一个这样的 Senario:

1) 我在网络上有一个网格,里面有一个面板。

2) 我通过调用这个来“插入”带有 Web 用户控件的面板

 Control ctlControl;
 ctlControl = LoadControl("~/UserControls/ChequeCreation.ascx");
 pnlTransaction.Controls.Add(ctlControl);

3) Web 用户控件提供两个按钮。一个是“更新”,一个是“重置”。

问题是这样的:

我想要实现的是当按下“更新”按钮时,它会将某些内容更新回我的数据库?但似乎在我按下“更新”或“重置”按钮后。 Web 用户控件已消失或丢失。对于我的客人是因为回发问题?对吗?

我试过 if(!postback) 还是不行。

我将如何克服这个问题?我已经摸不着头脑一天了?

非常感谢。

问候

梁克:

PS:对不起我的英语水平,请不要犹豫,说出我的错误或错误。

【问题讨论】:

  • 您是否在网格或 uct 页面上使用 AJAX/更新面板
  • 尝试在项目模板中添加而不是动态添加
  • @RashmiKantShrivastwa 不……建议这样做吗?
  • @Ali 我的项目模板是什么意思?是指直接将控件添加到网格中吗?或者?抱歉 == 我对 asp.net 很陌生

标签: c# asp.net ajax postback webusercontrol


【解决方案1】:

您可以将任何数据列转换为模板列,然后将您的网络用户控件拖放到其中

这将导致类似于以下代码检查“uc1:webUserControle1”在代码中的位置

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDB">
            <Columns>
                <asp:TemplateField HeaderText="ID" SortExpression="ID">
                    <EditItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                        <uc1:webUserControle1 ID="WebUserControle1_1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            </Columns>
        </asp:GridView>

【讨论】:

    【解决方案2】:

    如果您使用 AJAX,请尝试在您的 UCT 设计页面上添加更新面板

    【讨论】:

      【解决方案3】:

      ASP.NET 不会在回发之间保留动态添加的用户控件。这就是它正在消失的原因。每次创建页面时都需要添加控件。但是,如果您希望触发事件,则需要在初始化控件树时添加它并恢复原始控件 ID。这些链接提供了完整的解释 http://www.4guysfromrolla.com/articles/092904-1.aspxhttp://avinashsing.sunkur.com/2011/02/24/dynamic-controls-viewstate-and-postback/

      【讨论】:

        【解决方案4】:

        您必须每次在 Page_Init 上重新加载用户控件或 页面加载。然后你可以得到按钮点击事件和用户之后 控制不会丢失。

        private void LoadUserControl(){
        
           string controlPath = LastLoadedControl;
        
            if (!string.IsNullOrEmpty(controlPath)) {
                PlaceHolder1.Controls.Clear();
                UserControl uc = (UserControl)LoadControl(controlPath);
                PlaceHolder1.Controls.Add(uc);
            }
        }
        
        protected void Page_Load(object sender, EventArgs e) {  
           LoadUserControl();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多