.net c#单击事件上的按钮,在更新面板内,通过自定义服务器控件呈现而不触发

Buttons on click event, inside an update panel, rendered through a custom server control not firing
2021-11-25
  •  译文(汉语)
  •  原文(英语)

我正在开发一个呈现 (a) textbox(es)的自定义控件,在其中textchange event我想将值异步发送到控件,在该控件中它将搜索并使数据在公开的属性中可用,以便稍后获取page load.但我的问题是,如何异步调用在我的控制范围内编码的方法来执行搜索功能.现在,textbox textchangebuttons click events没有触发,按钮点击加载页面,在文本框中写入什么都不做.这是代码.

    public int Count { get; set; }
    public List<object> Selection { get; set; }
    public object SelectedObject { get; set; }
    public Label label;
    public TextBox textBox;
    protected override void RenderContents(HtmlTextWriter output)
    {
        textBox = new TextBox();
        textBox.TextChanged += new EventHandler(Method);
        UpdatePanel up = new UpdatePanel();
        up.ID = "UpdatePanel1";
        up.ChildrenAsTriggers = true;
        up.UpdateMode = UpdatePanelUpdateMode.Conditional;
        up.ContentTemplateContainer.Controls.Add(a);
        this.label = new Label();
        this.label.Text = "Initialized Text.";
        up.ContentTemplateContainer.Controls.Add(this.label);
        Button button = new Button();
        button.Text = "Say Hello";
        button.Click += new EventHandler(HandleButtonClick);
        up.ContentTemplateContainer.Controls.Add(button);
        this.Controls.Add(up);
        foreach (Control c in this.Controls)
        {
            c.RenderControl(output);
        }
    }


    private List<object> Get(string searchValue)
    {
        throw new NotImplementedException();
    }
    public void Method(object sender, EventArgs e) {
        (sender as TextBox).Text += " ";
        //Selection = Get((sender as TextBox).Text);
    }
    private void HandleButtonClick(object sender, EventArgs e)
    {
        this.label.Text = "Hello " + this.textBox.Text;
        //Selection = Get(this.textBox.Text);
    }
解决过程1

发现你必须从 IPostBackDataHandler 继承你的控件,而且,异步问题就在那里.我使用了一种解决方法,我将控件放在 updatepanel 中(将 updatepanel 放在 aspx 上),通过绑定的控件 onclick 注册了一个按钮,在 jquery/javascript 上我触发了按钮的 click 事件以异步发送调用.一切正常,但在触发该事件后,javascript 代码不再运行,尝试通过再次在预渲染中注册客户端脚本来调用该函数,但没有奏效.

I am developing a custom control which renders (a) textbox(es), in its textchange event I want to asynchronously send the value to the control where it would search and make the data available in an exposed property to be fetched later on page load. But my problem is, HOW TO ASYNCHRONOUSLY CALL A METHOD CODED INSIDE MY CONTROL to do the search functionality. For now, the textbox textchange and the buttons click events are not firing, the button click loads the page and writing in the textbox does nothing. Here is the code.

    public int Count { get; set; }
    public List<object> Selection { get; set; }
    public object SelectedObject { get; set; }
    public Label label;
    public TextBox textBox;
    protected override void RenderContents(HtmlTextWriter output)
    {
        textBox = new TextBox();
        textBox.TextChanged += new EventHandler(Method);
        UpdatePanel up = new UpdatePanel();
        up.ID = "UpdatePanel1";
        up.ChildrenAsTriggers = true;
        up.UpdateMode = UpdatePanelUpdateMode.Conditional;
        up.ContentTemplateContainer.Controls.Add(a);
        this.label = new Label();
        this.label.Text = "Initialized Text.";
        up.ContentTemplateContainer.Controls.Add(this.label);
        Button button = new Button();
        button.Text = "Say Hello";
        button.Click += new EventHandler(HandleButtonClick);
        up.ContentTemplateContainer.Controls.Add(button);
        this.Controls.Add(up);
        foreach (Control c in this.Controls)
        {
            c.RenderControl(output);
        }
    }


    private List<object> Get(string searchValue)
    {
        throw new NotImplementedException();
    }
    public void Method(object sender, EventArgs e) {
        (sender as TextBox).Text += " ";
        //Selection = Get((sender as TextBox).Text);
    }
    private void HandleButtonClick(object sender, EventArgs e)
    {
        this.label.Text = "Hello " + this.textBox.Text;
        //Selection = Get(this.textBox.Text);
    }
Solutions1

found out the you have to inherit your control from IPostBackDataHandler, also, the asynchronous problem is there. I have used a workaround, I have put the control in updatepanel (put updatepanel on aspx), registered a button through control onclick of which is bound, on jquery/javascript I am firing the click event of the button to send the calls asynchronously. Things work fine, but after firing that event javascript code doesnt run again, tried calling the function by registering client script in prerender again, didnt work.

转载于:https://stackoverflow.com/questions/15240094/buttons-on-click-event-inside-an-update-panel-rendered-through-a-custom-server

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入