清除用户控制值 [英] Clear User Control Value

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

问题描述

你好,



我在我的代码中动态添加了usercontrol。我还在pre init状态下添加它来处理它的事件。我的用户控件包含一个网格视图。我可以在网格视图上执行编辑和删除操作。此用户控件绑定到树视图节点已更改。意味着它将在网格中显示适当的选定节点信息。但是我的prblm是当我要点击树视图的另一个链接时,它将显示我的两个输出...新的树视图链接点击信息和以前的树视图点击信息。

我的代码如下

Hello,

I have added usercontrol dynamically in my code. I have also added it on pre init state to handle its events. My user control contains one grid view. I can able to perform edit and delete operations on grid view. This user control is bind to treeview Node changed. Means It will show appropriate selected node information in the grid. But my prblm is when i am going to click on another link of treeview it will show me both ouputs...the new treeview link click information and previous treeview click information.
my code is as follows

  #region Maintain User Control
    protected Control GetPostBackControl(Page page)
    {
            Control control = null;
            try
            {
                string ctrlName = page.Request.Params.Get("__EVENTTARGET");

                if (ctrlName != null && ctrlName != String.Empty)
                {
                    control = page.FindControl(ctrlName);
                }
                else
                {
                    ContentPlaceHolder cph = (ContentPlaceHolder)page.FindControl("Main");
                    for (int i = 0, len = page.Request.Form.Count; i < len; i++)
                    {
                        string[] ctl = page.Request.Form.AllKeys[i].Split('$');
                        if (ctl.Length > 2)
                        {
                            control = cph.FindControl(ctl[2]) as System.Web.UI.WebControls.Button;
                        }

                        if (control != null) break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return control;

    }

    protected override void OnPreInit(EventArgs e)
     {
       
            base.OnPreInit(e);

            Control control = GetPostBackControl(this.Page);

            // Check if the postback is caused by the button 
            // Titled "Click to Create a Dynamic Control"
            // OR
            // createAgain field is true 
            // which means a call is made to the server while the 
            // user control is active  
            if (_flag==1)
            {
                _flag = 0;
                UpdateSite2.ContentTemplateContainer.Controls.Clear();
            }
            else
            {

            
            if ((control != null && control.ClientID == PID.ToString()) || createAgain)
            {
                //should be set before the CreateUserControl method
                createAgain = true;
                UpdateSite2.ContentTemplateContainer.Controls.Clear();
                CreateUserControl(PID.ToString());
               
            }
            }
    }

    protected void CreateUserControl(string controlID)
    {
        // createAgain field is set to true in the OnPreInit method
        // when the 'Create User Control' button is clicked 

        // the createAgain field is used to check if the
        // user control is on the page before the call 
        // if so create the control again and add it to the
        // Control Hierarchy again
     
        try
        {
            if (createAgain != null)//&& PlaceHolder1
            {
                if (Session.Count > 0)
                {
                    UpdateSite2.ContentTemplateContainer.Controls.Clear();
                    
                    for (int i = 0; i < Session.Count; i++)
                    {
                        switch (Session[i].ToString())
                        {
                            case "ASP.usercontrols_site_siteuc_ascx":
                                {
                                    // Create instance of the UserControl SimpleControl
                                   
                                    UserControls_Site_SiteUC ucSimpleControl = LoadControl("~/UserControls/Site/SiteUC.ascx") as UserControls_Site_SiteUC;
                                    
                                    // Set the Public Properties
                                    ucSimpleControl.SiteID = ((UserControls_Site_SiteUC)(Session[i])).SiteID;
                                    //ucSimpleControl.LastName.Text = ((UserControls_Site_SiteUC)(Session[i])).LastName.Text;
                                   
                                    //Create Event Handler for btnPost Click 
                                    //ucSimpleControl.btnPostClk += new btnPost_Click(ucSimpleControl_btnPostClk);

                                    //Add the SimpleControl to Placeholder
                                    UpdateSite2.ContentTemplateContainer.Controls.Add(ucSimpleControl);
                                  
                                    break;
                                }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    #endregion  

if (TreeView1.SelectedNode.ToolTip == "SiteName")
           
 {
 UserControls_Site_SiteUC usercntl = (UserControls_Site_SiteUC)Page.LoadControl("~/UserControls/Site/SiteUC.ascx");
                usercntl.SiteID = int.Parse(PID.ToString());

                UpdateSite2.ContentTemplateContainer.Controls.Add(usercntl);
                UpdateSite2.Visible = true;
 
                SitePanel.Visible = true;
             
                // Add the instance of the SimpleControl to Session Variable
                Session.Add((Session.Count+1).ToString(), usercntl);

                // Set createAgain = true
                createAgain = true;
  }



如何处理这种情况


how to handle such situations

推荐答案

');
if (ctl.Length > 2
{
control = cph.FindControl(ctl [ 2 ]) as System.Web.UI.WebControls.Button;
}

if (control!= null 断裂;
}
}
}
catch (例外情况)
{
throw ex;
}
返回控制;

}

受保护 覆盖 void OnPreInit(EventArgs e)
{

base .OnPreInit(e) ;

控制控制= GetPostBackControl( this .Page);

// 检查回发是否由按钮引起
// 标题为点击创建动态控件
//
// createAgain字段为真
// 表示调用
// 用户控件处于活动状态时的服务器
if (_flag == 1
{
_flag = 0 ;
UpdateSite2.ContentTemplateContainer.Controls.Clear();
}
else
{


if ((control!= null && control.ClientID == PID.ToString())|| createAgain)
{
// 应在CreateUserControl方法之前设置
createAgain = ;
UpdateSite2.ContentTemplateContainer.Controls.Clear();
CreateUserControl(PID.ToString());

}
}
}

受保护 void CreateUserControl( string controlID)
{
// 在OnPreInit方法中将createAgain字段设置为true
// b

// createAgain字段用于检查
// 用户控件是否打开调用前的页面
// 如果是,再次创建控件并将其添加到
// C再次使用ontrol层次结构

尝试
{
if (createAgain!= null // &安培;&安培; PlaceHolder1
{
if (Session.Count > < span class =code-digit> 0

{
UpdateSite2.ContentTemplateContainer.Controls.Clear();

for int i = 0 ; i < Session.Count; i ++)
{
switch (Session [i] .ToString())
{
case ASP.usercontrols_site_siteuc_ascx
{
// 创建UserControl的实例SimpleControl

UserControls_Site_SiteUC ucSimpleControl = LoadControl( 〜/ UserControls / Site / SiteUC.ascx as UserControls_Site_SiteUC;

// 设置公共属性
ucSimpleControl.SiteID = ((UserControls_Site_SiteUC)(会话[I]))SITEID。
// ucSimpleControl.LastName.Text =((UserControls_Site_SiteUC)(Session [i]))。LastName。文字;

// 为btnPost创建事件处理程序点击
// ucSimpleControl.btnPostClk + = new btnPost_Click(ucSimpleControl_btnPostClk);

// 将SimpleControl添加到占位符
UpdateSite2.ContentTemplateContainer.Controls.Add( ucSimpleControl);

break ;
}
}
}
}
}
}
catch (例外情况ex)
{
throw ex;
}
}
#endregion

if (TreeView1.SelectedNode.ToolTip == SiteName

{
UserControls_Site_SiteUC usercntl =(UserControls_Site_SiteUC)Page.LoadControl( 〜/ UserControls / Site /SiteUC.ascx);
usercntl.SiteID = int .Parse(PID.ToString());

UpdateSite2.ContentTemplateContainer.Controls.Add(usercntl);
UpdateSite2.Visible = true ;

SitePanel.Visible = true ;

// 将SimpleControl的实例添加到会话变量
Session.Add((Session.Count + 1).ToString(),usercntl);

// 设置createAgain = true
createAgain = ;
}
'); if (ctl.Length > 2) { control = cph.FindControl(ctl[2]) as System.Web.UI.WebControls.Button; } if (control != null) break; } } } catch (Exception ex) { throw ex; } return control; } protected override void OnPreInit(EventArgs e) { base.OnPreInit(e); Control control = GetPostBackControl(this.Page); // Check if the postback is caused by the button // Titled "Click to Create a Dynamic Control" // OR // createAgain field is true // which means a call is made to the server while the // user control is active if (_flag==1) { _flag = 0; UpdateSite2.ContentTemplateContainer.Controls.Clear(); } else { if ((control != null && control.ClientID == PID.ToString()) || createAgain) { //should be set before the CreateUserControl method createAgain = true; UpdateSite2.ContentTemplateContainer.Controls.Clear(); CreateUserControl(PID.ToString()); } } } protected void CreateUserControl(string controlID) { // createAgain field is set to true in the OnPreInit method // when the 'Create User Control' button is clicked // the createAgain field is used to check if the // user control is on the page before the call // if so create the control again and add it to the // Control Hierarchy again try { if (createAgain != null)//&& PlaceHolder1 { if (Session.Count > 0) { UpdateSite2.ContentTemplateContainer.Controls.Clear(); for (int i = 0; i < Session.Count; i++) { switch (Session[i].ToString()) { case "ASP.usercontrols_site_siteuc_ascx": { // Create instance of the UserControl SimpleControl UserControls_Site_SiteUC ucSimpleControl = LoadControl("~/UserControls/Site/SiteUC.ascx") as UserControls_Site_SiteUC; // Set the Public Properties ucSimpleControl.SiteID = ((UserControls_Site_SiteUC)(Session[i])).SiteID; //ucSimpleControl.LastName.Text = ((UserControls_Site_SiteUC)(Session[i])).LastName.Text; //Create Event Handler for btnPost Click //ucSimpleControl.btnPostClk += new btnPost_Click(ucSimpleControl_btnPostClk); //Add the SimpleControl to Placeholder UpdateSite2.ContentTemplateContainer.Controls.Add(ucSimpleControl); break; } } } } } } catch (Exception ex) { throw ex; } } #endregion if (TreeView1.SelectedNode.ToolTip == "SiteName") { UserControls_Site_SiteUC usercntl = (UserControls_Site_SiteUC)Page.LoadControl("~/UserControls/Site/SiteUC.ascx"); usercntl.SiteID = int.Parse(PID.ToString()); UpdateSite2.ContentTemplateContainer.Controls.Add(usercntl); UpdateSite2.Visible = true; SitePanel.Visible = true; // Add the instance of the SimpleControl to Session Variable Session.Add((Session.Count+1).ToString(), usercntl); // Set createAgain = true createAgain = true; }



如何处理此类情况


how to handle such situations


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

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