数据绑定到自定义控件参数集合

发布于 2024-12-22 09:51:31 字数 1752 浏览 0 评论 0原文

我正在努力实现一些我确信非常简单的事情,但我只见树木不见森林。我创建了一个简单的控件,它继承自链接按钮,其中包含一个名为 LinkParamaters 的简单对象的列表,其想法是在标记中您可以定义回发 url 以及参数,然后返回 PostBackUrl 属性以包含查询字符串。这按预期工作,直到我想要将参数绑定到转发器内。然后我开始收到以下解析器错误:

只有具有 DataBinding 事件的对象才支持数据绑定表达式。 UI.Controls.LinkParameter 没有 DataBinding 事件。

所以我想我显然需要实现一个数据绑定事件。也许有一个我需要实现的接口,但我找不到。遗憾的是,我刚刚调了工作,所以目前无法访问反射器,因此无法深入研究代码,但我可以看到 Control 有这些事件/方法,从 Control 继承参数对象解决了解析器错误,但毫不奇怪没有解析绑定,无论如何它都没有意义,因为它不是一个控件。

链接按钮代码:

[ParseChildren(true, DefaultProperty = "Parameters")]
public class QueryLinkButton : LinkButton
{

    private String _url = null;

    public QueryLinkButton()
    {
        this.Parameters = new List<LinkParameter>();
    }

    public override String PostBackUrl
    {
        get
        {
            return _url ?? (_url = BuildUrl());
        }
        set
        {
            base.PostBackUrl = value;
        }
    }

    [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
    public List<LinkParameter> Parameters { get; set; }

    private String BuildUrl()
    {
        //Build URL to Include Query String Params           
    }

}

public class LinkParameter 
{

    public String Name { get; set; }

    public String Value { get; set; }

}

然后是 Asp 代码:

 <custom:QueryLinkButton runat="server" ID="EventLink" Text="View Accommodation" PostBackUrl="HotelCriteria.aspx">
       <custom:LinkParameter runat="server" Name="EventID" Value='<%# Eval("EventID")%>'/>
  </custom:QueryLinkButton>

我现在通过处理这个服务器端解决了这个问题,但这确实否定了我试图创建自定义控件的操作,因为我想纯粹在中定义这些东西标记。即使只是一个让我开始的文章链接也会非常感激,因为我已经用谷歌搜索了很长时间,只能找到类似的问题,但不能找到相同的问题,但这很可能是搜索条件不佳。

预先感谢

保罗

I'm trying to achieve something which I'm sure is pretty simple but I cannot see the wood for the trees. I have created a simple control which inherits from link button which contains a list of a simple object called LinkParamaters, the idea being that in markup you can define a postback url as well as parameters which then returns the PostBackUrl property to include a querystring. This was working as expected until I wanted to data bind the parameters within a repeater. I then started to get the following Parser Error:

Databinding expressions are only supported on objects that have a DataBinding event. UI.Controls.LinkParameter does not have a DataBinding event.

So I figured I obviously needed to implement a data bind event. maybe there is an interface I need to implement, I couldn't find one. Sadly I've just moved jobs so don't have access to reflector at the moment so could not drill down into the code but I could see that Control has these events/methods, inheriting the parameter object from Control solved the parser error but unsurprisingly didn't resolve the binding and it doesn't make sense anyway as it's not a control.

The Link button code :

[ParseChildren(true, DefaultProperty = "Parameters")]
public class QueryLinkButton : LinkButton
{

    private String _url = null;

    public QueryLinkButton()
    {
        this.Parameters = new List<LinkParameter>();
    }

    public override String PostBackUrl
    {
        get
        {
            return _url ?? (_url = BuildUrl());
        }
        set
        {
            base.PostBackUrl = value;
        }
    }

    [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
    public List<LinkParameter> Parameters { get; set; }

    private String BuildUrl()
    {
        //Build URL to Include Query String Params           
    }

}

public class LinkParameter 
{

    public String Name { get; set; }

    public String Value { get; set; }

}

And then the Asp Code :

 <custom:QueryLinkButton runat="server" ID="EventLink" Text="View Accommodation" PostBackUrl="HotelCriteria.aspx">
       <custom:LinkParameter runat="server" Name="EventID" Value='<%# Eval("EventID")%>'/>
  </custom:QueryLinkButton>

I've got around the problem for now by handling this server side but it really kind of negates what I was trying to do creating the custom control as I wanted to define these things purely in the mark-up. Even just a link to an article to get me started will be much appreciated as I've googled for so long and could only find similar problems but not the same thing but that is most likely to poor search criteria.

Thanks in advance

Paul

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

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

发布评论

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

评论(1

德意的啸 2024-12-29 09:51:31

我最终设法用我的大脑解决了这个问题,我不是 100% 相信它是理想的解决方案,但它对我来说确实有意义,更重要的是这个愚蠢的小问题现在正在工作:0)。

我从 DataBoundControl 派生了 LinkParameter 类,然后覆盖自定义控件上的 DataBind 方法,然后告诉每个参数执行其 DataBind 方法:

 public override void DataBind()
    {
        基.DataBind();
        foreach(参数中的LinkParameter param)
        {
            param.DataBind();
        }
    }

如果有人有更清洁、更有效的解决方案,请告诉我。

谢谢

保罗

I managed to fix this in the end by using my brain, I'm not 100% confident it is the ideal solution but it does make sense to me and more importantly this silly little problem is now working :0).

I got my LinkParameter class to derive from DataBoundControl and then overrode the DataBind method on the custom control and then told each parameter to perform it's DataBind method:

    public override void DataBind()
    {
        base.DataBind();
        foreach (LinkParameter param in Parameters)
        {
            param.DataBind();
        }
    }

If anyone has a cleaner, more efficient solution then please let me know.

Thanks

Paul

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