FormView 插入帮助后将控件添加到面板

发布于 2024-08-01 16:29:30 字数 2350 浏览 1 评论 0原文

这是我的 HTML

<asp:UpdatePanel runat="server" ID="panel1" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:FormView runat="server" ID="formViewUno" DataSourceID="odsBob" DefaultMode="Insert">
                                    <InsertItemTemplate>
                    <span>Name:</span>
                    <asp:Literal ID="Literal4" runat="server" Text="&nbsp;&nbsp;&nbsp;" />
                    <asp:TextBox runat="server" ID="tbxName" Text='<%# Bind("Name") %>' />
                    <br />
                    <span>Age:</span>
                    <asp:Literal ID="Literal5" runat="server" Text="&nbsp;&nbsp;&nbsp;" />
                    <asp:TextBox runat="server" ID="tbxAge" Text='<%# Bind("Age") %>' />
                    <br />
                    <span>City:</span>
                    <asp:Literal ID="Literal6" runat="server" Text="&nbsp;&nbsp;&nbsp;" />
                    <asp:TextBox runat="server" ID="tbxCity" Text='<%# Bind("City") %>' />
                    <br />
                    <asp:Button ID="Button1" runat="server" CommandName="Insert" Text="Insert" />
                </InsertItemTemplate>
            </asp:FormView>
              <asp:Panel runat="server" ID="msgs">

            </asp:Panel>
        </ContentTemplate>
    </asp:UpdatePanel>

这是我的 C#

 private void odsBob_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
    {

            var p = e.ReturnValue as Person;
            if (p != null)
            {
                var msg = new Label
                              {
                                  Text =
                                      String.Format("{0} [Age:{1}, City:{2}] was successfully added", p.Name, p.Age,
                                                    p.City)
                              };

                var br = new LiteralControl { Text = "<br/>" };
                msgs.Controls.Add(br);
                msgs.Controls.Add(msg);


            }

    }

如何保留(插入后添加一个新的)标签控件? 它正在被消灭。 每次添加的新内容都会正确添加。 如何保持控件集合完好无损? 谢谢你的帮助。

干杯, 〜ck

Here's my HTML

<asp:UpdatePanel runat="server" ID="panel1" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:FormView runat="server" ID="formViewUno" DataSourceID="odsBob" DefaultMode="Insert">
                                    <InsertItemTemplate>
                    <span>Name:</span>
                    <asp:Literal ID="Literal4" runat="server" Text="   " />
                    <asp:TextBox runat="server" ID="tbxName" Text='<%# Bind("Name") %>' />
                    <br />
                    <span>Age:</span>
                    <asp:Literal ID="Literal5" runat="server" Text="   " />
                    <asp:TextBox runat="server" ID="tbxAge" Text='<%# Bind("Age") %>' />
                    <br />
                    <span>City:</span>
                    <asp:Literal ID="Literal6" runat="server" Text="   " />
                    <asp:TextBox runat="server" ID="tbxCity" Text='<%# Bind("City") %>' />
                    <br />
                    <asp:Button ID="Button1" runat="server" CommandName="Insert" Text="Insert" />
                </InsertItemTemplate>
            </asp:FormView>
              <asp:Panel runat="server" ID="msgs">

            </asp:Panel>
        </ContentTemplate>
    </asp:UpdatePanel>

Here's my C#

 private void odsBob_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
    {

            var p = e.ReturnValue as Person;
            if (p != null)
            {
                var msg = new Label
                              {
                                  Text =
                                      String.Format("{0} [Age:{1}, City:{2}] was successfully added", p.Name, p.Age,
                                                    p.City)
                              };

                var br = new LiteralControl { Text = "<br/>" };
                msgs.Controls.Add(br);
                msgs.Controls.Add(msg);


            }

    }

How can I persist (add a new one after the insert) the label controls? It is being wiped out. The new one added is added each time correctly. How can I keep the control collection in tact? Thanks for any help.

Cheers,
~ck

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

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

发布评论

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

评论(1

你与清晨阳光 2024-08-08 16:29:30

看起来您正在事件处理程序期间动态创建标签对象。

动态控件是有问题的,因为它们需要在每次回发时重新创建。 请记住,回发会创建 Page 对象的一个​​新实例 - 这意味着您添加到上一页的控件消失了 - 您的面板会随着每个新请求而初始化为空,因此只会添加最新的文字/标签对。

一种解决方案可能是将所有必要的文本信息添加到会话中,并让面板在预渲染期间从会话中的任何内容生成动态标签和文字。

另一种解决方案会更复杂,但您可以让面板在初始化阶段动态添加标签和文字。 如果您可以确保在 Init 期间以相同的顺序添加相同数量的控件,那么这些控件的 ViewState 将在每次回发时正确跟踪。 您基本上需要将最近添加的标签和文字存储到会话中,并让面板在下一个请求时将其取出,以确保它在初始化期间重新添加回来。 您还需要存储一个计数器,以便面板知道在初始化期间要添加多少组控件。

It looks like you're dynamically creating a label object during the event handler.

Dynamic controls are problematic because they need to be recreated on every postback. Remember that a postback creates a new instance of your Page object - which means that the controls you added to your last page are gone - your Panel is initialized as empty with each new request, so only the latest literal/label pair will be added.

One solution may be to add all the necessary textual information to Session, and have your Panel generate dynamic labels and literals from whatever is in Session during Prerender.

Another solution would be more complex, but you could have the Panel add labels and literals dynamically during the Init phase. If you can ensure that the same number of controls is added in the same order during Init, then the ViewState for those controls will be properly tracked on each PostBack. You would basically need to store the most recently added label and literal into Session, and have the Panel fetch it out on the next request to ensure it got added back in during Init. You'd also need to store a counter so that the Panel knew how many sets of controls to add during Init.

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