Web 用户控制回发问题

发布于 2024-12-19 13:29:07 字数 580 浏览 6 评论 0 原文

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

我有一个像这样的 Senario:

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

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

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

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

问题是这样的:

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

我尝试了 if(!postback) 仍然不起作用。

我该如何克服这个问题?我已经挠头大约一天了?

非常感谢你。

问候

LiangCk:

PS:抱歉我的英语水平,请随时指出我的错误或错误。

I am new to ASP.net thing, and i have some question regarding postback.

I have a Senario like this:

1) I have a grid on web with a panel inside.

2) I "Insert" the panel with a Web User Control by calling this

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

3)The Web User Control providing two button. One is "update" and one is "reset".

Problem is like here:

What i wanted to achieve is when press the "update" button, it will update something back to my DB? But seem after i press the button "Update" or "Reset". The web user control is gone or missing. For my guest is because of the postback issues? Is that correct?

I tried if(!postback) still its doesn't work.

How am i going to overcome this? I already scratching my head about a day?

Thanks you so much.

Regards

LiangCk:

PS:Sorry for my english level, and please dont hesitate to voice out my error or mistake.

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

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

发布评论

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

评论(4

幸福%小乖 2024-12-26 13:29:07

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

这将导致类似于以下代码的结果,检查“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>

well you can convert any of your data columns to template column and then drag and drop your web user control to it

this will result in something like the following code check where "uc1:webUserControle1" is located in the code

<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>
永言不败 2024-12-26 13:29:07

如果您使用 AJAX,请尝试在 UCT 设计页面上添加 updatepanel

if you are using AJAX, try add updatepanel on your UCT design page

古镇旧梦 2024-12-26 13:29:07

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

ASP.NET will not preserve a dynamically added user control between postbacks. This is why it is dissapearing. You will need to add the control each time the page is created. However you will need to add it when the control tree is being initialized and restore the original control ID if you want your events to fire. These links provide a full explanation https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx and http://avinashsing.sunkur.com/2011/02/24/dynamic-controls-viewstate-and-postback/

荆棘i 2024-12-26 13:29:07

您必须每次在 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();
}

You have to Every Time Reload the user control on Page_Init or
Page_Load. Then you can get the Button Click Event and After tha User
Control will not lost.

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