Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Events not firing

  Asked By: Jerry    Date: Jul 02    Category: Sharepoint    Views: 1552

I am encountering a strange problem. Till now I have not faced this one. The
child control events are not getting fired in a web part, i.e., Button Click
event, SelectedIndexChanged event, etc. Till now whichever web part I built,
the events fired without any problem. I tried debugging the web part and
confirmed that when we do a button click, the methods OnLoad,
CreateChildControls, RenderWebPart are called but not the button_click method.
is there any settings that controls this.

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Ross Watkins     Answered On: Jul 02

Simple question: Did you wire up the event?

 
Answer #2    Answered By: Jeremiah Wallace     Answered On: Jul 02

I am attaching the code for u guys to help...


public class Data2XML : Microsoft.SharePoint.WebPartPages.WebPart

{

private TextBox txtForename;

private TextBox txtSurname;

private RadioButtonList rblSex;

private TextBox txtDOB;

private TextBox txtDept;

private button  cmdSubmit;

private Label lblError;

private Label lblXML;

protected override void CreateChildControls()

{

lblError = new Label();

lblError.ForeColor = System.Drawing.Color.Red;

lblError.Text = "";

lblError.Visible = false;

Controls.Add(lblError);

txtForename = new TextBox();

Controls.Add(txtForename);



txtSurname = new TextBox();

Controls.Add(txtSurname);



rblSex = new RadioButtonList();

ListItem item = new ListItem("Male", "Male");

rblSex.Items.Add(item);

item = new ListItem("Female", "Female");

rblSex.Items.Add(item);

Controls.Add(rblSex);



txtDOB = new TextBox();

Controls.Add(txtDOB);



txtDept = new TextBox();

Controls.Add(txtDept);



cmdSubmit = new Button();

cmdSubmit.Click += new EventHandler(cmdSubmit_Click);

cmdSubmit.Text = "Submit";

Controls.Add(cmdSubmit);



lblXML = new Label();

lblXML.ForeColor = System.Drawing.Color.RoyalBlue;

lblXML.Text = "";

Controls.Add(lblXML);

}

/// <summary>

/// Render this Web part  to the output parameter specified.

/// </summary>

/// <param name="output"> The HTML writer to write out to </param>

protected override void RenderWebPart(HtmlTextWriter output)

{

EnsureChildControls();

output.AddAttribute("Width", "100%");

output.RenderBeginTag("Table");

output.RenderBeginTag("Tr");

output.AddAttribute("Width", "30%");

output.RenderBeginTag("Td");

output.Write("Forename: ");

output.RenderEndTag();

output.RenderBeginTag("Td");

txtForename.RenderControl(output);

output.RenderEndTag();

output.RenderEndTag();

output.RenderBeginTag("Tr");

output.RenderBeginTag("Td");

output.Write("Surname: ");

output.RenderEndTag();

output.RenderBeginTag("Td");

txtSurname.RenderControl(output);

output.RenderEndTag();

output.RenderEndTag();

output.RenderBeginTag("Tr");

output.RenderBeginTag("Td");

output.Write("Sex: ");

output.RenderEndTag();

output.RenderBeginTag("Td");

rblSex.RenderControl(output);

output.RenderEndTag();

output.RenderEndTag();

output.RenderBeginTag("Tr");

output.RenderBeginTag("Td");

output.Write("Date of Birth: ");

output.RenderEndTag();

output.RenderBeginTag("Td");

txtDOB.RenderControl(output);

output.RenderEndTag();

output.RenderEndTag();

output.RenderBeginTag("Tr");

output.RenderBeginTag("Td");

output.Write("Department: ");

output.RenderEndTag();

output.RenderBeginTag("Td");

txtDept.RenderControl(output);

output.RenderEndTag();

output.RenderEndTag();

output.RenderBeginTag("Tr");

output.RenderBeginTag("Td");

cmdSubmit.RenderControl(output);

output.RenderEndTag();

output.RenderBeginTag("Td");

lblError.RenderControl(output);

output.RenderEndTag();

output.RenderEndTag();

output.RenderBeginTag("Tr");

output.AddAttribute("Colspan", "2");

output.RenderBeginTag("Td");

lblXML.RenderControl(output);

output.RenderEndTag();

output.RenderEndTag();

output.RenderEndTag();

}

private void cmdSubmit_Click(object sender, EventArgs e)

{

lblXML.Text = "<Data>" +

"<Forename>" + txtForename.Text + "</Forename>" +

"<Surname>" + txtSurname.Text +"</Surname>" +

"<Sex>" + rblSex.SelectedValue +"</Sex>" +

"<DOB>" + txtDOB.Text +"</DOB>" +

"<Department>" + txtDept.Text +"</Department>" +

"</Data>";

}

}

 
Answer #3    Answered By: Tony Freeman     Answered On: Jul 02

Your code creates the controls  and links the submit button  to the event  handler in the CreateChildControls method. This is called  in the ASP.NET Render phase. The control  event phase has already passed. This is fine for the initial display, but on user postback ("submit"), the web  part loads fresh and no events  are yet wired. Execution progresses through Init, Load, "Control Events", PreRender, and finally Render. Again, your CreateChildControl method  creates controls and wires them up for events which will not get called.

Since you know what ASP.NET controls you will build, build them and wire them up for events during the init or load phase. After the all LOAD phases complete, ASP.NET performs a "second-chance" control state load, that is, if the new controls exist and there is data submitted by the user in those controls, ASP.NET will load those controls with data from the postback. Then ASP.NET will start processing your control events.

I believe the control "auto-wireing" happens in the init phase (control constructor).

I suggest removing the CreateChildControl and renaming it to a private method (i.e. CreateMyControls) and placing a call to that method in either the control constructor or a method that is called during the load phase.

public Data2XML // <--- Web part  constructor
{
this.Load += new EventHandler(Load_Data2XML);
}

public void Load_Data2XML(object source, eventargs e) // <-- Exact syntax escapes me right now
{
CreateMyControls(); // <--- this is the renamed CreateChildControls
}

See if that works.

 
Answer #4    Answered By: Danny Shaw     Answered On: Jul 02

The problem  i was facing was because i was debugging  the webpart in the web
part preview mode. in this mode the web  part's child methods  do not fire. when
i put the web part  in the site and tested it it worked fine. I use the preview
mode because it gives a meaningfull error rather than redirecting to the Web
Parts maintenance page as it does from the site.

but still ur suggestion could be used for the SelectedIndexChanged event  for the
List based controls  - DropDownList, ListBox, etc.

 
Didn't find what you were looking for? Find more on Events not firing Or get search suggestion and latest updates.




Tagged: