Buttons on click event, inside an update panel, rendered through a custom server control not firing

泪湿孤枕 提交于 2019-12-25 02:55:31

问题


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

回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!