Saturday, April 30, 2011

whats the best way to pass data to user components in asp.net?

its .net 2.0 here, not MVC, and im crap at asp forms :(.

So yeah, ive got a page with user controls on it. When i click on something in the page, i want to load the usercontrol based on a parameter from the page.

Problem is i cant do it.

In my page's FaultTree_Clicked, i get the value, then ive tried a variety of things:

  • I tried exposing a property on the child user control to set the value, which i set in FaultTree_Clicked, it gets forgotten.

  • I tried saving it to Session["mykey"], and loading Session["mykey"] in the control's Page_init... the value is blank.

  • I tried saving it to ViewState["mykey"], and loading ViewState["mykey"] in the control's Page_init... the value is blank.

EDIT: ok more specific info: Heres a cut down version of what the page(MyFault) looks like:

<form id="form" runat="server">
 <div id="faulttree">
   <asp:TreeView ID="FaultTree" ......>
 </div>
 <uc1:_DefectDetail ID="DefectDetail" runat="server" Visible="true" EnableViewState="true" />
</form>

And theres a method on the pages codebehind "FaultTree_SelectedNodeChanged()".

When that method is hit, I want to load/show the DefectDetail control. The DefectControl requires a faultid, which comes off the Tree, which i successfully get in the SelectedNodeChanged method. I cant get the faultid into the defect control.

From stackoverflow
  • This has to do with ASP.NET page lifecycle. By the time the click event fires, the control's init event has already happened.

    In order to better assist you, please provide a more detailed explanation of what the FaultTree control is, what is the desired result and some sample code.

    UPDATE:

    Instead of a public property, you can simply create a public method in the control that does the desired action and invoke it from the FaultTree_SelectedNodeChangeEvent.

    Example (for a public method named Refresh):

    _DefectDetail.Refresh(object data);
    
    dalyons : I updated with an example to explain what im trying to do.
    dalyons : argh! thats all i needed to do!! haha. Thanks heaps. If i set the fault property within the Refresh function, i noticed it is blank by the time i call another method on that control. So i put the faultid in a hidden field, that is updated by the Refresh. is this the way to go? I think it might be beacuse my fault object is not serializeable.
    Jose Basilio : If the hidden field works that's great.
  • Basically you have to use EventHandlers....

            1. Add a event handler to your user control (I had a search bar UscSearchCriteriaBar1)   
    
            public event EventHandler CriteriaChanged;
    
            +
    
    
            private void InternalOnCriteriaChanged()
            {
            OnCriteriaChanged();
            }
            +
            protected virtual void OnCriteriaChanged()
            {
            // If there are registered clients raise event
            if (CriteriaChanged != null)
            CriteriaChanged(this, EventArgs.Empty);
            }
            +
            Example
            public int EmployeeID
            {
            get
            {
            f (Session["EmployeeID"] != null)
            {
            ViewState["EmployeeID"] = Convert.ToInt32(Session["EmployeeID"]);
            }
            if (ViewState["EmployeeID"] == null)
            ViewState["EmployeeID"] = 0;
            return int.Parse(ViewState["EmployeeID"].ToString());
            }
            set
            {
            ctlEmployee.SelectedValue = value.ToString();
            ViewState["EmployeeID"] = value;
            Session["EmployeeID"] = value;
            }
            }
    
    
            In your page or other control
    
    
            override protected void OnInit(EventArgs e)
            {
            InitializeComponent();
            UscSearchCriteriaBar1.CriteriaChanged += new EventHandler(this.CriteriaChanged);    
            base.OnInit(e);
            }
    
            private void CriteriaChanged(object sender, EventArgs e)
            {
            try
            {
    RefreshData();
            }
            catch (Exception ex)
            {
            ExceptionManager.Publish(ex);
            }
            }
    
        You can get UscSearchCriteriaBar1.EmployeeID
    

    This code should give you some ideas...was done for 1.1 should work on 2.

0 comments:

Post a Comment