English 中文(简体)
Ext.Net Button Event not firing
原标题:

I am using Ext.Net and I have a problem.

I am creating dynamic buttons. It is working but if i click, the button event is not working.:(

How can I fix it?

My code:

foreach (var events in eventsInformation)
{
    Ext.Net.Button btn = new Ext.Net.Button();
    btn.ID = events.EvtId.ToString();
    btn.Text = events.EvtName;
    btn.Click += new EventHandler(Tickets_click);
    ViewPort1.Controls.Add(btn);
}
最佳回答

Use Ext.net DirectMethod instead of ASP.NET postback event handler.

<ext:Button ID="Button1" runat="server" Text="Click Me" Icon="Lightning">
    <Listeners>
        <Click Handler="Ext.net.DirectMethods.SetTimeStamp();" />
    </Listeners>
</ext:Button>            


<script runat="server">
[DirectMethod]
public void SetTimeStamp()
{
    this.Label1.Text = string.Concat("Server Time: ", DateTime.Now.ToLongTimeString());
}

问题回答

There are a couple things that require correction in the original sample:

  1. By default, Ext.NET Button Components do not AutoPostBack (ie, reload the entire page). It is encouraged to use DirectEvents (Ajax call) if you want to communicate with the server and avoid a complete page reload.
  2. Ext.NET Components should be added to the parent .Items Collection, instead of the .Controls Collection.

Here s a complete demo with both these corrections.

Example

<%@ Page Language="C#" %>

<%@ Register assembly="Ext.Net" namespace="Ext.Net" tagprefix="ext" %>

<script runat="server">
    protected override void OnInit(EventArgs e)
    {
        Ext.Net.Button btn = new Ext.Net.Button();

        btn.Text = "Submit (AutoPostBack)";
        btn.Click += Button1_Click;

        // 1. Set to AutoPostBack, default is "false"
        btn.AutoPostBack = true;

        // 2. Add Button to .Items Collection    
        this.ViewPort1.Items.Add(btn);

        base.OnInit(e);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        X.Msg.Notify("Server Time", DateTime.Now.ToLongTimeString()).Show();
    }
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Ext.NET Example</title>
</head>
<body>
    <form runat="server">
        <ext:ResourceManager runat="server" />

        <ext:Viewport ID="ViewPort1" runat="server" />
    </form>
</body>
</html>

Now, I d recommend changing your AutoPostBack Button Click event to a DirectEvent Click. That would require making the following three revisions to the code-behind.

Example

<script runat="server">
    protected override void OnInit(EventArgs e)
    {
        Ext.Net.Button btn = new Ext.Net.Button();

        btn.Text = "Submit (DirectEvent)";

        // 2. CHANGE to .DirectClick 
        btn.DirectClick += Button1_Click;

        // 3. REMOVE btn.AutoPostBack = true;

        this.ViewPort1.Items.Add(btn);

        base.OnInit(e);
    }

    // 3. CHANGE "EventArgs" to "DirectEventArgs"    
    protected void Button1_Click(object sender, DirectEventArgs e)
    {
        X.Msg.Notify("Server Time", DateTime.Now.ToLongTimeString()).Show();
    }
</script>

Hope this helps.

The place to start is the ASP.NET Page Life Cycle Overview. If you re programatically adding controls to an asp.net page then you should familiarise yourself with it, if you re not already =)

Now work your way down this checklist:

  1. Are you always adding the components (buttons in this instance) to the page? If you don t add them *always, then on post-back they won t be there for the EventHandler to be wired up to, for the handler to fire. (This may not apply to an Ext.Net button triggered by a DirectEvent, but it can t hurt).
  2. Do you have the "usual" Ext.Net handler/module registrations in your web.config file to enable the direct events to be handled?
  3. Have you verified that you re not receiving any javascript client-side errors that are inhibiting the event handler?
  4. Use something like Fiddler to verify that the event is actually triggering a DirectEvent back to the server.
<ext:Button ID="extBtn1" runat="server" Text="Cancel">
    <DirectEvents>
        <Click OnEvent="extBtn1Click">
            <EventMask ShowMask="true" />
        </Click>
    </DirectEvents>
</ext:Button>

This extBtn1Click event will fire in server-side.

The button is not present when Event is fired. That is why it doesnt work. If you construct some testing button in aspx file, it would work just fine.

Solution: You must construct the button during OnInit event so it will be present, and bound to EventHandler, during the new page cycle after clicking the button.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签