Linkbutton无法在自定义Web控件中正确呈现 [英] Linkbutton not rendering correctly inside a custom web control

查看:57
本文介绍了Linkbutton无法在自定义Web控件中正确呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了以下设置

自定义控件1-MyListControl-MyListItemControl的嵌套集合

Custom Control 1 - MyListControl - Nested Collection of - MyListItemControl

MyListItemControl继承自Panel,所以我可以这样写:

MyListItemControl inherits from a Panel so I can write this:

<cc1:MyListControl ID="MyListControl1" runat="server">
   <ListItems>
      <cc1:MyListItemControl ID="MyListItemControl1" runat="server" CustomProperty="1">
          <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">Edit</asp:LinkButton>
      </cc1:MyListItemControl>
    </ListItems>
</cc1:MyListControl>

我没有收到任何错误,并且控件没有正确渲染.上面的代码如果放在Formview内,则linkbutton不会获得所有显示的PostBack脚本

I get no error and the Control does render, just not correctly. The above code if placed inside a Formview , the linkbutton does not get the PostBack script all that gets rendered is

<a id="LinkButton1">Edit</a>

有人知道为什么会这样并且链接按钮没有按通常的方式呈现吗?

Does anyone know why this is happening and the linkbutton is not being rendered as it normally would?

TIA

安德鲁

推荐答案

在我看来,ASP.Net中的一个错误与它父级控件集合中的LinkBut​​ton顺序有关.

To me, looks like a bug in ASP.Net that has to do with the LinkButton order in its parent's Controls collection.

此代码段:

    protected override void OnLoad(EventArgs e)
    {
        // show some data
        _l = new Label();
        _l.Visible = false;
        _l.Text = "Current Web's Url: " + SPContext.Current.Web.Url;
        Controls.Add(_l);

        // add a button with some processing
        _lb = new LinkButton();
        _lb.Text = "Click here";
        _lb.Click += new EventHandler(_lb_Click);
        Controls.Add(_lb);
    }

生成此HTML:

<span id="ctl00_PlaceHolderMain_ads1"><h1>This is a custom control</h1><span>Current Web's Url: http://arielki03:9999/sites/ads</span><a href="javascript:__doPostBack('ctl00$PlaceHolderMain$ctl01','')">Click here</a></span>

但是此代码段(只是颠倒了我添加上面的控件的顺序,因此,LinkBut​​ton是第一个):

But this snippet (just inverted the order in which I'm adding the controls above, so LinkButton is first):

    protected override void OnLoad(EventArgs e)
    {
        // add a button with some processing
        _lb = new LinkButton();
        _lb.Text = "Click here";
        _lb.Click += new EventHandler(_lb_Click);
        Controls.Add(_lb);

        // show some data
        _l = new Label();
        _l.Visible = false;
        _l.Text = "Current Web's Url: " + SPContext.Current.Web.Url;
        Controls.Add(_l);
    }

不显示任何控件!

问题似乎出在LinkBut​​ton是父级控件集合中的第一个.解决方法:在其前面添加一个空Label,它将起作用:-)

The problem seems to be with LinkButton being the first in the parent's Controls collection. Workaround: add an empty Label before it, it gonna work :-)

这篇关于Linkbutton无法在自定义Web控件中正确呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆