1

我正在使用 Ext.Net,但我遇到了问题。

我正在创建动态按钮。它正在工作,但是如果我单击,则按钮事件不起作用。:(

我该如何解决?

我的代码:

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);
}
4

5 回答 5

2

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.

于 2011-02-11T16:41:38.793 回答
0

开始的地方是ASP.NET 页面生命周期概述。如果您以编程方式将控件添加到 asp.net 页面,那么您应该熟悉它,如果您还没有的话 =)

现在按照这个清单工作:

  1. 您是否总是将组件(在此实例中为按钮)添加到页面?如果您不总是添加它们,那么在回发时,它们将不会出现在 EventHandler 被连接到的位置,以便处理程序触发。(这可能不适用于由 DirectEvent 触发的 Ext.Net 按钮,但不会造成伤害)。
  2. 您的 web.config 文件中是否有“通常的”Ext.Net 处理程序/模块注册以启用要处理的直接事件?
  3. 您是否确认您没有收到任何阻止事件处理程序的 javascript 客户端错误?
  4. 使用Fiddler之类的东西来验证事件是否真的触发了 DirectEvent 回到服务器。
于 2011-02-10T14:26:36.470 回答
0

使用 Ext.net DirectMethod 而不是 ASP.NET 回发事件处理程序。

<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());
}

于 2011-04-28T00:08:35.447 回答
0
<ext:Button ID="extBtn1" runat="server" Text="Cancel">
    <DirectEvents>
        <Click OnEvent="extBtn1Click">
            <EventMask ShowMask="true" />
        </Click>
    </DirectEvents>
</ext:Button>

此 extBtn1Click 事件将在服务器端触发。

于 2012-06-01T11:08:11.477 回答
0

触发事件时,该按钮不存在。这就是为什么它不起作用。如果您在 aspx 文件中构建一些测试按钮,它会工作得很好。

解决方案:您必须在 OnInit 事件期间构造按钮,以便在单击按钮后的新页面循环期间出现并绑定到 EventHandler。

于 2015-05-25T15:43:48.247 回答