ASP.Net 用户控制事件冒泡 (C#)

发布于 2024-08-19 16:19:56 字数 1094 浏览 7 评论 0原文

非常感谢。我已经尽职调查并研究了两天,但似乎无法理解在线教程所描述的内容。

情况:首席程序员辞职。公司老板知道我兼职做了一些(新手)VB.NET,并要求我在他的 Intranet 应用程序(用 C# 编写)上的 2 个自定义用户控件之间连接功能。回答专业人士的小考虑 - 我很流利在 Visual Studio 中...但仍在学习 C#)。

以下是控件的简化描述,但希望足以让我们朝着正确的方向前进:

UserControl_1 包含多个链接按钮,单击这些链接按钮时,应更改 UserControl_2 中的 ObjectDataSource 使用的 SelectParameter 的值:

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

UserControl_2 (带有 DataSource 的 GridView):

<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" />

我想要将新的默认值传递给 UserControl_2 中 ObjectDataSource 的 SelectParameter。期待专业人士的个性化解释!非常感谢您的帮助!

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 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘味果▽酱 2024-08-26 16:19:56

我将在 UserControl2 中公开一个属性,该属性将依次设置参数值

public string ParamValue { get; set; }

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

此外,在页面中创建将在控件中设置属性的公共方法

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

从这里,在链接按钮单击事件中,您可以使用设置公共属性页面的方法:

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

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");
      }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文