This post will provide necessary instructions you need to know to Implement WebParts with UI Controls and associated events
Step 1 : First you need to declare Class inherited from WebPart
public class MyUIWebPart: WebPart
{
// Your stuff here
}
Step 2 : Now, You need to override CreateChildControls method to render UI Controls you need on the WebPart.
public class MyUIWebPart: WebPart
{
// Your stuff here
protected override void CreateChildControls()
{
// Create UI controls
// Bind Events for UI Controls
}
}
That's it. Lets create one webpart to understand how this works?
Sample WebPart with UI:
Lets create webpart having one lable and one button. We will implement click event on button to execute simple text update. Following code implements very simple framework and associates events to handle Click event.
public class MyUIWebPart: WebPart
{
Label lbl_msg = new Label();
Button cb_submit;
protected override void CreateChildControls()
{
cb_submit = new Button();
//Add Controls to WebPart
lbl_msg.Text = "Click Me";
cb_submit.Text = "Submit";
//Attach event Handler for click event
cb_submit.Click+=new EventHandler(cb_submit_Click);
//Add Controls to WebPart
this.Controls.Add(lbl_msg);
this.Controls.Add(cb_submit);
}
void cb_submit_Click(object sender, EventArgs e)
{
// Lets change Lable message to Clicked
lbl_msg.Text = "Clicked";
}
}
Fantastic!!!. I am sure that you are excited and take it from here to create very complex webparts with other available UI elements.
My Profile: https://mcp.support.microsoft.com/profile/PATEL1