ASP.Net User Control Event Bubbling (C #)

Thank you very much in advance. I did my due diligence and researched for 2 days, but I can't seem to imagine what the online tutorials describe.

Situation: A lead programmer takes off and leaves. The company owner knows what I did (newbie) VB.NET on the side and asked to connect functionality between two user controls in my intranet application (written in C #. A little consideration for answering questions - I am fluent in Visual Studio .. but still learning C #).

Here's a simplified description of the controls, but hopefully enough to get us moving in the right direction:

UserControl_1 contains several link buttons that, when clicked, should change the value of the SelectParameter parameter used by the ObjectDataSource in UserControl_2:

<asp:LinkButton ID="OpenBtn1" runat="server" Text="Show Open Requests" />
<asp:LinkButton ID="ClosedBtn1" runat="server" Text="Show Closed Requests" />

UserControl_2 (GridView with DataSource):

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
    SelectMethod="GetDataBy_Status" 
    TypeName="adhoc_TblAdptrs.adhoc_TblAdptr">
    <SelectParameters>
       <asp:Parameter DefaultValue="All" Name="status" Type="String" />
    </SelectParameters>
</asp:ObjectDataSource>

<asp:GridView1 ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" />

What I would like to do is pass the new default value for the SelectParameter parameter of the ObjectDataSource to UserControl_2. I look forward to an individual explanation from professionals! Your help is much appreciated!

+3
source share
1 answer

I would set the property in UserControl2, which in turn would set the parameter value

public string ParamValue { get; set; }

protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e) {
    if (ParamValue.Length > 0){
        e.InputParameters["status"] = ParamValue;
    }
}

Also, on the page, create a public method that sets the property in the control

public void SetParamValue(string Value){
    UserControl_2.ParamValue = Value;
}

, clickbutton click, :

  void OpenBtn1_Click(Object sender, EventArgs e){
     ((YourPageClassHere)this.Page).SetParamValue("Open");
  }

, .

,

public interface IParameterValuePage{
    public void SetParamValue(string Value)
}

  void OpenBtn1_Click(Object sender, EventArgs e){
      if (this.Page is IParameterValuePage){
          ((IParameterValuePage)this.Page).SetParamValue("Open");
      }
      else{
          throw new Exception("Page must implement IParameterValuePage");
      }
  }
+3

Source: https://habr.com/ru/post/1731067/


All Articles