Answered by:
WebPart event problem.

Question
-
Hi everyone.
I have this code:protected override void CreateChildControls() { first = new LinkButton(); first.Text = "blabla"; first.Click += new EventHandler(first_Click); this.Controls.Add(first); } void first_Click(object sender, EventArgs e) { //doing something.... }
Problem is, if i hit a link nothing is happen, i trying set brakepoint to first_click event, but program never jump to this part of code.
Where is the problem?
Tuesday, December 9, 2008 5:05 PM
Answers
-
Lukas,
try this code. you need to have the eventhandler binding in the OnInit Function.
I have tested this code and it works well.
Build your code with the help of this sample -
namespace
TestWebPart{
public class TestWebPart : WebPart{
LinkButton first; protected override void OnInit(EventArgs e){
first =
new LinkButton();first.Click +=
new EventHandler(first_Click);}
void first_Click(object sender, EventArgs e){
Context.Response.Write(
"first_Click vent is fired");}
protected override void CreateChildControls(){
first.Text =
"blabla"; this.Controls.Add(first);}
protected override void Render(System.Web.UI.HtmlTextWriter writer){
base.Render(writer);}
}
}
Hope this helps...
________________________________________ Gautham S Pai [MCAD]- Edited by Gautham Shantharam Pai Thursday, December 11, 2008 5:01 AM removed commented portions
- Proposed as answer by Gautham Shantharam Pai Thursday, December 11, 2008 5:02 AM
- Marked as answer by Matt Burnett Friday, December 12, 2008 11:07 PM
Thursday, December 11, 2008 5:00 AM
All replies
-
Lukas,
Your code needs some modification
protected override void CreateChildControls() { first = new LinkButton(); first.Text = "blabla"; first.OnClick += new EventHandler(first_Click); this.Controls.Add(first); }
void first_Click(object sender, EventArgs e) { //doing something.... }
You need to handle the OnClick event,... and since your Linkbutton is not bound to the correct event, it is not entering the first_Click method.
Hope this helps...
________________________________________ Gautham S Pai [MCAD]- Proposed as answer by Gautham Shantharam Pai Wednesday, December 10, 2008 9:06 AM
- Unproposed as answer by Lukas Toman Wednesday, December 10, 2008 3:04 PM
Wednesday, December 10, 2008 9:06 AM -
I am not sure, I understand you...
There is only one event to click: linkButton.Click
I am tryed do this:first.OnClientClick = "firstClick()"; //or this first.Attributes.Add("onClick","firstClick()"); private void firstClick() { //SOME CODE }
But it doesnt work, but if I tryed this:first.OnClientClick = "alert()"; //or this first.Attributes.Add("onClick","alert()");
I think its some problem with handler of onClick event, I dont know how to add handler for this event.....
Wednesday, December 10, 2008 3:54 PM -
Lukas,
try this code. you need to have the eventhandler binding in the OnInit Function.
I have tested this code and it works well.
Build your code with the help of this sample -
namespace
TestWebPart{
public class TestWebPart : WebPart{
LinkButton first; protected override void OnInit(EventArgs e){
first =
new LinkButton();first.Click +=
new EventHandler(first_Click);}
void first_Click(object sender, EventArgs e){
Context.Response.Write(
"first_Click vent is fired");}
protected override void CreateChildControls(){
first.Text =
"blabla"; this.Controls.Add(first);}
protected override void Render(System.Web.UI.HtmlTextWriter writer){
base.Render(writer);}
}
}
Hope this helps...
________________________________________ Gautham S Pai [MCAD]- Edited by Gautham Shantharam Pai Thursday, December 11, 2008 5:01 AM removed commented portions
- Proposed as answer by Gautham Shantharam Pai Thursday, December 11, 2008 5:02 AM
- Marked as answer by Matt Burnett Friday, December 12, 2008 11:07 PM
Thursday, December 11, 2008 5:00 AM -
Ok, I am sorry but this code is not runnig well...
I copy exactly this code to my project and runtime never jump to firs_Click function.....
Are you using:
Microsoft.SharePoint.WebPartPages.WebPart
System.Web.UI.WebControls.WebParts.WebPart
to inherit on main class??
But I of course tryed use both, nothing happend.... Have you any idea where is the problem???
Friday, December 12, 2008 7:47 PM -
i am not sure Lukas, as far as the linkbutton issue is concerned, to my knowledge, i dont think it has anything to do with inheritance of Web part from SharePoint or ASP.NEt
________________________________________ Gautham S Pai [MCAD]Saturday, December 13, 2008 5:47 AM