防止用户控制重复加载 [英] Prevent User control from repetative loading

查看:74
本文介绍了防止用户控制重复加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件,带有一些文本框和一个按钮(提交)。我的问题是

当我加载aspx页面时我注册了ascx控件'的按钮点击事件

aspx页面。如果单击ascx控件上的提交按钮它工作正常

但如果我把页面加载事件放在if(!ispostback)下,那么按钮点击

事件无法正常工作。

由于页面已经加载所以再次重新加载控件是不明确的

来获取ascx控件点击活动。



请帮助我实现那个。我不想再加载控件了。



我的aspx代码如下



I have a user control with some text box and a button(submit).My problem is that
when I load the aspx page I register the ascx control''s button click event on the
aspx page.If click the submit button on the ascx control it is working fine
but if I put the page load events under if(!ispostback),then the button click
event not working.
As the page is already loaded so it is ambiguous to reload the control again
to get the ascx controls click event.

Kindly help me to achive that one.I don''t want to load the control again.

My aspx code below

protected void Page_Load(object sender, EventArgs e)
    {
       
            if (Request.QueryString["mode"] == "createTck")
            {
                UserControl_CreateTicket uccreateTicket = LoadControl("~/UserControl/CreateTicket.ascx") as UserControl_CreateTicket;
                plcHolderTicket.Controls.Add(uccreateTicket);
                uccreateTicket.evnt_btnSubmitClk += new delgbtnSubmit_Ticket(ucCreateTicket_btnSubmitClk);
            }
            else if (Request.QueryString["mode"] == "TcktHistory")
            {
                UserControl_TicketHistory ucTicketHistoey = LoadControl("~/UserControl/TicketHistory.ascx") as UserControl_TicketHistory;
                plcHolderTicket.Controls.Add(ucTicketHistoey);
            }
        

    }

    #region ButtonClick(ContentPages)
    void ucCreateTicket_btnSubmitClk(object sender, EventArgs e)
    {
        UserControl_CreateTicket ucCrateTicketControl = ((UserControl_CreateTicket)(sender));
        Response.Write(ucCrateTicketControl.Remark);

    } 
    #endregion

推荐答案

你必须在每个帖子上重新加载控件,因为它是一个动态添加的控件。

如果你在XML标记中包含控件,你可以避免加载。然后,您可以将两个控件添加到XML文件中,并仅根据查询字符串显示所需的控件。

You have to reload the control on every post back, because its a dynamically added control.
You can avoid the loading if you include the control in the XML markup. Then you could add both controls to the XML file and only display the one you want based on the query string.
<my_control id="createTicket">......</my_control>
<my_control2 id="historyTicket">......


if (Request.QueryString["mode"] == "createTck")
{
    createTicket.Visible = true;
    historyTicket.Visible = false;        
}
else if (Request.QueryString["mode"] == "TcktHistory")
{
    createTicket.Visible = false;
    historyTicket.Visible = true;       
}

</my_control2>



此链接将说明页面提交时会发生什么。

http://msdn.microsoft.com/en-us /library/ms178472(v=vs.100).aspx [ ^ ]


这篇关于防止用户控制重复加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆