Results 1 to 7 of 7

Thread: [2005] Web custom control - calling Button Event?

  1. #1

    Thread Starter
    Addicted Member senthilkumartd's Avatar
    Join Date
    Feb 2005
    Posts
    206

    [2005] Web custom control - calling Button Event?

    I am creating a custom control by inheriting the Gridview
    In the "rendercontents" method, I gave like below,
    Code:
            protected void btn_Click()
            {
                SqlConnection conn = new SqlConnection(ConnectionString);
                SqlCommand cmd = new SqlCommand(InsertText, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                this.DataBind();
                this.ShowAddButton();
            }
    
    protected override void RenderContents(HtmlTextWriter output)
            {
                    base.RenderContents(output);
    
                    StringBuilder sb = new StringBuilder();
                    sb.Append("<asp:Button ID='btn' runat='server' Text='Addnew' onClick=btn_Click/>");
                    
                   output.Write(sb.ToString());
    
            }
    Issues:
    1. It won't display the button in the output screen
    2. If I use <input type=Button value ='add' onClick='btn_click'> (html button) instead of asp.net servercontrol, in the above coding, it won't call the btn_click, which reside in the custom control's one of the procedure.

    How to solve the above problem? help needed
    God has been pleased to place as a king or cobbler do the work sincerely

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Web custom control - calling Button Event?

    When you do your output.write, you are actually writing HTML directly to the response stream to the browser. You cannot treat it like another ASP.NET form waiting to be compiled.

    If you want an ASP.NET button on the form, you must place it there in the control's designer.

  3. #3

    Thread Starter
    Addicted Member senthilkumartd's Avatar
    Join Date
    Feb 2005
    Posts
    206

    Re: [2005] Web custom control - calling Button Event?

    mendhak,
    Could you explain clearly. I can't understand the control's designer. control's designer means what?. And how could i call a event from rendering method. Please clear the second issue of my post that I already written.
    If any examples it will be appreciated.
    God has been pleased to place as a king or cobbler do the work sincerely

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Web custom control - calling Button Event?

    Quite simply, do you know how when you create an ASP.NET page, you can drag and drop buttons on to it?

    Or alternatively how you can create a dynamic button and add it to the Page's control collection?

    Well those are your two options.

    You cannot do this:

    Code:
    sb.Append("<asp:Button ID='btn' runat='server' Text='Addnew' onClick=btn_Click/>");
    But if you were to dynamically generate a textbox and then assign an event handler to its click event, you could manage something similar, although it'll be more complex.

  5. #5

    Thread Starter
    Addicted Member senthilkumartd's Avatar
    Join Date
    Feb 2005
    Posts
    206

    Re: [2005] Web custom control - calling Button Event?

    Code:
    But if you were to dynamically generate a textbox and then assign an event handler to its click event, you could manage something similar, although it'll be more complex.
    Could you explain with some code? I wish to create dynamically.
    God has been pleased to place as a king or cobbler do the work sincerely

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Web custom control - calling Button Event?

    This is just code I'm writing out without Visual Studio. It's not meant to be copy pasted, just to be understood:
    Code:
    Button btn1 = new Button();
    btn1.Click += new System.EventHandler(this.MethodToHandleClick);

  7. #7

    Thread Starter
    Addicted Member senthilkumartd's Avatar
    Join Date
    Feb 2005
    Posts
    206

    Re: [2005] Web custom control - calling Button Event?

    See the full source code of cutom control

    http://www.vbforums.com/showthread.php?t=459904
    God has been pleased to place as a king or cobbler do the work sincerely

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width