|
-
Mar 21st, 2007, 08:20 AM
#1
Thread Starter
Addicted Member
[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
-
Mar 21st, 2007, 02:27 PM
#2
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.
-
Mar 21st, 2007, 11:01 PM
#3
Thread Starter
Addicted Member
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
-
Mar 23rd, 2007, 12:39 PM
#4
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.
-
Mar 26th, 2007, 12:29 AM
#5
Thread Starter
Addicted Member
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
-
Mar 27th, 2007, 05:23 AM
#6
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);
-
Mar 27th, 2007, 06:28 AM
#7
Thread Starter
Addicted Member
Re: [2005] Web custom control - calling Button Event?
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|