ASP.Net回发手册没有从动态创建LinkBut​​ton的工作 [英] ASP.Net manual postback not working from dynamically created linkbutton

查看:173
本文介绍了ASP.Net回发手册没有从动态创建LinkBut​​ton的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现手动回发来触发code没有连接到一个控制。我不想使用AJAX PageMethods,因为我需要回发过程中引用某些控制,并希望结果更新页面上的GridView。

I'm trying to achieve a manual postback to trigger code not attached to a control. I don't want to use AJAX PageMethods as I need to reference certain controls during the postback, and want the result to update to a gridview on the page.

我已经有两个其他的工作方法,但由于某些原因,我的第三个不一旦部署,但确实我的dev的机器做工精细。

I already have two other methods that work, but for some reason my third one doesn't once deployed, but does work fine on my dev machine.

code

        switch (Request["__EVENTARGUMENT"]) {
            case "ItemReserved":
                GetAllStock();
                break;
            case "PendingItemRemoved":
                PopulatePending();
                break;
            case "StockInSubmitted":
                SubmitNewStock();    // this doesn't fire
                break;
        }

我插线尽快写入日志文件中的code达到SubmitNewStock()。该日志被写入到我的dev的机器上,但不是在网络服务器。无论其他回传正常工作。

I inserted a line to write to a log file as soon as the code reached "SubmitNewStock()". The log was written to on my dev machine, but not on the webserver. Both other postbacks work as expected.

页脚本

    function ctxMenu_Reserve() {
        PageMethods.SetItemReserved(CGRefForMenu);
        __doPostBack('', 'ItemReserved');

        HideMouseMenu();
    }

    function ctxMenuPending_Remove() {
        PageMethods.RemovePendingItem(CGRefForPendingMenu);
        __doPostBack('', 'PendingItemRemoved');
    }

    function StockINSubmit(){
        SubmitPlaceHolder = document.getElementById('<%=PlaceholderStockInSubmit.ClientID %>');
        SubmitPlaceHolder.innerHTML = "Processing...";

        __doPostBack('', 'StockInSubmitted');  //Causes postback, but relevant code is not triggered once on my live server
    }

有谁知道这是为什么不工作,一旦我的网站部署到我的实时Web服务器?

Does anyone know why this is not working once I deploy the site to my live webserver?

我要指出,StockINSubmit()是从一个放置在一个模式弹出框内动态创建客户端链接按钮启动。

I should mention that StockINSubmit() is initiated from a dynamically created client link button which is placed inside a modal popup box.

        PlaceholderStockInSubmit.Controls.Add(
            new LinkButton() {
                OnClientClick = "StockINSubmit()",
                Text = "Submit",
                ID = "lnkStockInSubmit"
            });

StockINSubmit()取代包含该按钮的DIV的HTML内容。 (所以我点击提交,并将其变为处理标签)。我不能明白了一个道理,为什么会导致任何问题,但认为这是值得一提。

StockINSubmit() replaces the HTML content of the DIV containing that button. (So I click "Submit", and it changes to a "Processing" label). I cant see a reason why that would cause any issues, but thought it was worth mentioning.

推荐答案

我终于想通了,发生了什么事......

I eventually figured out what was happening...

动态创建LinkBut​​ton的是执行回发作为其服务器点击(的onClick)事件的一部分(虽然我没有结合无论如何它)。在我的dev的机器,该事件的OnClientClick回发是跳动的OnClick回传,所以所有的预期code的运行是正常的。

The dynamically created linkbutton was performing a postback as part of its server click (onClick) event (although I had not bound any event to it). On my dev machine, the OnClientClick events postback was beating the OnClick postback, so all the expected code was running as normal.

由于某些原因,一旦部署到服务器,服务器OnClick事件被殴打事件的OnClientClick,所以完全回发是存在的没有额外__EVENTARGUMENT资料说的OnClientClick事件提供。

For some reason, once deployed to the server, the servers OnClick event was beating the OnClientClick event, so the full postback was occuring without the additional __EVENTARGUMENT information that the OnClientClick event was providing.

要解决这个问题,我发现另一篇关于这个网站了解释了如何取消默认回传,但仍执行客户端的JavaScript。

To get around this, i found another post on this site that explained how to cancel the default postback, but still execute client side javascript.

我的服务器端code现在更新如下:

My server side code is now updated as follows:

    LinkButton newButton = new LinkButton() {
        OnClientClick = "StockINSubmit()",
        Text = "Submit",
        ID = "lnkStockInSubmit"
    };
    newButton.Attributes.Add("onClick", "return false;");
    PlaceholderStockInSubmit.Controls.Add(newButton);

这是现在的工作就像一个魅力:)

Which is now working like a charm :)

感谢您的帮助球员。

这篇关于ASP.Net回发手册没有从动态创建LinkBut​​ton的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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