开发者

ASP.Net User Control Event Bubbling (C#)

开发者 https://www.devze.com 2022-12-19 12:03 出处:网络
Many thanks in advance.I\'ve done my due diligence and researched for 2 days, but can\'t seem to wrap my mind around what online tutorials describe.

Many thanks in advance. I've done my due diligence and researched for 2 days, but can't seem to wrap my mind around what online tutorials describe.

The situation: Lead Programmer ups and quits. Owner of firm knows I've done some (novice) VB.NET on the side and asks me to wire up functionality between 2 custom user controls on his intranet app (written in C#. Small consideration for answering pros -- I'm fluent in Visual Studio...but still learning C#).

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

UserControl_1 contains multiple linkbuttons that, when clicked, should change the value of the SelectParameter being used by an 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'd like开发者_Go百科 to do pass a new default value to the SelectParameter of the the ObjectDataSource in UserControl_2. I'm looking forward to a personalized explanation from the pros! Your help is greatly, greatly appreciated!


I would expose a property in UserControl2 that will, in turn, 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, in the page create public method that will set the property in the control

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

From here, in the linkbutton click event, you can just set the public property using the page's method:

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

This method will get the job done but it will couple the control with the linkbuttons to this specific page.

If you want to remove the coupling then you could implement an Interface with the method

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

Then in your page you would implement it and do a check in the code for the control page

  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");
      }
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消