开发者

ASP.NET Ajax - Buttons 'postbacking' but functionality not happening

开发者 https://www.devze.com 2023-02-25 12:53 出处:网络
Here\'s the thing I added a button to a webpart which saves some fields to MS-Excel. var btn = new Button { Text = title, CssClass = css };

Here's the thing I added a button to a webpart which saves some fields to MS-Excel.

var btn = new Button { Text = title, CssClass = css };
btn.Click += (sender,args)=>action();
Container.Controls.Add(btn);

This code is located inside OnInit() method in the WebPart. actionis the Action which does stuff, in this case:

Something.FillExcel(MyData);
Something.SaveExcel();

So what happens is, I click the button, it acts like doing the usual partial postback, and does absolutely nothing. I thought it could be something in the Fill/Save code, then I tried forcing an error just to see if it gets there. It seems the click event never fires, or, if it does, it doesn't run my code (?).

Here's the html, just in case:

<input type="submit" name="ctl00$m$g_b4af4370_c016_4712_9d60_fc8ca077a068$ctl359" value="Enviar Formulário" onclick="j开发者_StackOverflow中文版avascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$m$g_b4af4370_c016_4712_9d60_fc8ca077a068$ctl359&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" class="button" />

What could be happening?

Thanks in advance!

EDIT: After Cos Callis answered, I put my code inside OnLoad() instead of OnInit(). Here's new code:

protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        var btn = new Button { Text = title, CssClass = css };
        btn.Click += new EventHandler((sender,args)=>{
            if (myform.Page.IsPostBack)
            {
                excel.FillExcel(); excel.SaveFile();
            }
        });
        container.Controls.Add(btn);            
    }

I'm now getting a javascript error: SCRIPT5022: Sys.WebForms.PageRequestManagerServerErrorException: The given key was not present in the dictionary.


Because you are attempting to act on the INIT rather than the load, the data has not yet been attached at the server.

You should find this review of the life cycle of a web request in ASP.NET useful: http://msdn.microsoft.com/en-us/library/ms178472.aspx.

Here is the relevant extract:

Initialization

During page initialization, controls on the page are available and each control's UniqueID property is set. A master page and themes are also applied to the page if applicable. If the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state.

Load

During load, if the current request is a postback, control properties are loaded with information recovered from view state and control state.

During initialization the control "exists" but has not yet been loaded with data from the postback. If you you move your code to "OnLoad" you should achieve the disired results. (Don't forget to wrap that in "if(IsPostback)")

Cheers,

CEC

//added resource: After posting my answer I thought you might find this article useful as well: http://encosia.com/2007/10/24/are-you-making-these-3-common-aspnet-ajax-mistakes/


Sorry guys found out it was an error inside FillExcel(). I thought I would get that asp yellow page but instead it gives me a script error.

Maybe another common mistake for ajax/jquery/sharepoint newbies.

I'm accepting Cos Callis' reply as answer because he did answer everything I asked.

0

精彩评论

暂无评论...
验证码 换一张
取 消