简体   繁体   English

LinkBut​​ton回发但未点击事件

[英]LinkButton postback but click event not fired

Hi everyone 嗨,大家好

This is my first post on stackoverflow (which btw is by far my favourite site for finding answers). 这是我在stackoverflow上的第一篇文章(顺便说一句,这是我最喜欢的网站,用于寻找答案)。 After finally admitting defeat, I'm hoping someone can help me with this problem... 在最终承认失败之后,我希望有人可以帮我解决这个问题......

The question has already been asked several times, but none of the suggested solutions I found has helped. 这个问题已经被多次提出过,但我找到的建议解决方案都没有提供帮助。 Apologies in advance for a lengthy post, but I want to avoid anyone wasting their time on suggesting things I tried already. 提前为一篇冗长的帖子道歉,但我想避免任何人浪费时间建议我已经尝试过的事情。

The code below worked until recently, and its structure has not been touched since (although unrelated changes were made to the child page). 下面的代码一直工作到最近,其结构从未被触及(尽管对子页面进行了无关的更改)。 In any event, it suddenly stopped working. 无论如何,它突然停止了工作。 Now even the most simplified button won't fire. 现在即使是最简化的按钮也不会触发。

Setup 设定

  • VS 2008 C#, IIS 7 (no changes in setup since way before it stopped working) VS 2008 C#,IIS 7(在设置停止工作之前没有设置更改)

  • nested masterpage (main + 1 child) 嵌套母版页(主要+ 1个孩子)

  • dynamically loaded ucl with datalist in child page 在子页面中使用datalist动态加载ucl
    (ie MP => nested MP => child page => ucl => datalist => linkbutton) (即MP =>嵌套MP =>子页=> ucl => datalist => linkbutton)

  • linkbutton click event also resides in ucl linkbutton click事件也驻留在ucl中


Problem 问题

On LB click, the postback occurs ok, but the server-side click event never gets hit. 在LB上单击,回发发生正常,但服务器端点击事件永远不会被命中。

Code

page: 页:

var ctrlX = base.LoadControl("~/somedir/someucl.ascx");

=> loads fine



ascx file (datalist stripped of all but the button): ascx文件(除了按钮之外的所有数据删除):

<asp:DataList ID="dlX" RepeatLayout="Table" runat="server">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
    <tr>
        <td>
            <asp:LinkButton ID="btnX" OnClick="btnX_Click" CausesValidation="false" runat="server" />
        </td>
    </tr>               
</ItemTemplate>
<FooterTemplate></FooterTemplate>



codebehind: 代码隐藏:

protected void btnX_Click(object sender, EventArgs e)
{
// do something  
}



things tried 事情尝试过

  • cleaning the solution 清洁解决方案
  • digging out a working backup and checking for code changes 挖掘出有效的备份并检查代码更改
    (found none affecting the structure or user control) (发现没有影响结构或用户控制)

  • setting LinkButton CausesValidation true/false 设置LinkBut​​ton CausesValidation true / false

  • stripping datalist and LinkButton down to bare essentials 剥离datalist和LinkBut​​ton到最基本的东西
  • adding AddressOf="btnX.click" to LinkButton 将AddressOf =“btnX.click”添加到LinkBut​​ton
  • wrapping UpdatePanel one at a time with varying settings around 使用不同的设置一次包装一个UpdatePanel
    usercontrol 用户控件
    datalist 数据列表
    linkbutton LinkBut​​ton的

  • reattaching eventhandler in usercontrol init / load event: 在usercontrol init / load事件中重新连接eventhandler:

     IEnumerable<Control> controls = Utils.FlattenChildren(dlX); foreach (var button in controls.OfType(LinkButton)()) { if (button.ID.Contains("btnX")) button.Click += new EventHandler(btnX_Click); } 

    Wherever I add above code, all buttons are found and the event is attached, but click event doesn't fire ((LinkButton) = LinkButton inside <>; couldn't get it to display right, still struggling a bit with the editor) . 无论我在哪里添加上面的代码,都会找到所有按钮并附加事件,但是click事件不会触发((LinkBut​​ton)= LinkBut​​ton里面<>;无法让它显示正确,仍然与编辑器有点挣扎) 。

  • adding PostBackUrl manually in page load event 在页面加载事件中手动添加PostBackUrl

  • comparing the client ids between load/postback events => they always match 比较加载/回发事件之间的客户端ID =>它们总是匹配



That's it. 而已。 Right now, I can't think of what else to try or check (maybe something in the breakpoint context menu on postback?). 现在,我想不出还有什么可以尝试或检查(也许在回发的断点上下文菜单中有什么东西?)。

Because the ucl loads fine, and the postback is working ok, I suspect the problem is somewhere in the ucl, rather than the child page loading it. 因为ucl加载正常,并且回发工作正常,我怀疑问题是在ucl中的某个地方,而不是加载它的子页面。 But maybe not. 但也许不是。

If at all possible, I want to avoid workarounds (I need the button command argument, not shown above, and am not keen on solutions such as jquery with hidden field or query string). 如果可能的话,我想避免使用变通方法(我需要上面没有显示的按钮命令参数,并且不喜欢使用隐藏字段或查询字符串的jquery等解决方案)。

Apart from anything, I would really like to understand what causes this. 除了任何事情,我真的想了解是什么导致了这一点。

Obviously, I'm missing something... Thanks to anyone taking their time reading/helping with this! 显然,我错过了一些东西......感谢任何人花时间阅读/帮助这个!



======== as requested additional codebehind ======= ========按要求添加代码隐藏=======

I've simplified the parent page code to a minimum, dropping all method calls, and as per Eoins suggestion moved the ucl load in the page's init event. 我已将父页面代码简化为最小化,删除所有方法调用,并根据Eoins建议将ucl加载移动到页面的init事件中。 The ucl code remains as shown above. ucl代码仍然如上所示。 The DL and LB show up fine, and on click the postback is triggered, ucl page load event is hit, but as before, the server click event is not hit. DL和LB显示正常,点击后会触发回发,点击ucl页面加载事件,但和之前一样,服务器点击事件没有被点击。

    protected override void OnInit(EventArgs e)
    {
        var ctrlX = base.LoadControl("~/someucl.ascx");

        if (ctrlX == null)
            return;

        DataList dlX = (DataList)ctrlX.FindControl("dlX");

        if (dlX == null)
            return;

        DummyCollection x = new DummyCollection();

        x.Add(null); // ok since the test LB does not draw on the datasource
        dlX.DataSource = x;
        dlX.DataBind();
        pnlX.Controls.Add(ctrlX);

        base.OnInit(e);
    }
var ctrlX = base.LoadControl("~/somedir/someucl.ascx");

Where exactly does this code live on your page? 这些代码到底在哪里?

You'll need to make sure you're creating it and adding it to the control collection early enough in the page lifecycle so that on subsequent postbacks its created & wired up in time. 您需要确保创建它并在页面生命周期中尽早将其添加到控件集合中,以便在随后的回发中及时创建和连接。 Ie in the oninit event. 即在oninit事件。

protected override OnInit(...)
{
   base.OnInit(...);

   var ctrlX = base.LoadControl("~/somedir/someucl.ascx");

   this.Controls.Add(ctrlX);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM