Hi Is it acceptable to override the "Render" method when creating a custom web part in SharePoint? I have a web part which inherits from System.Web.UI.WebControls.WebPart. I add the controls to the controls collection in "CreateChildControls" and then override the "Render" method to render the html and the controls.
I am having some issues when I use a drop down list, but I want to know if my above approach is correct or not before I try to fix the issue with binding a drop down list.
Edited
I initialize my drop down lists and add it to the controls collection in "CreateChildControls". At the end of this method, I call "BindData" which binds my drop down list and if I am editing an existing record, it binds the drop down list data and sets the correct selected index. I then render the controls in the "Render" method. Here is a code snippet
protected override void CreateChildControls()
{
base.CreateChildControls();
this.EnsureUpdatePanelFixups();
ddlClient = new DropDownList();
ddlClient.ID = "ddlClient";
ddlClient.SelectedIndexChanged += new EventHandler(ddlClient_SelectedIndexChanged);
ddlClient.AutoPostBack = true;
ddlClient.ValidationGroup = __VALIDATIONGROUP;
ddlClient.Width = ObjCtrlWidth;
upClient = new UpdatePanel();
upClient.ID = "upClient";
upClient.UpdateMode = UpdatePanelUpdateMode.Conditional;
upClient.ContentTemplateContainer.Controls.Add(ddlClient);
this.Controls.Add(upClient);
//More controls here
BindData();
}
"ddlClient is a "DropDownList", "upClient" is a update panel. Yes, I have one update panel for one drop down list and I update other update panels using the update method.
This is the "Render" method
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
//stuff here
writer.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
upClient.RenderControl(writer);
writer.RenderEndTag();
//stuff here
}
My "BindData" method calls other bind methods which bind different drop down lists and this is how one of them is binded
private void BindData()
{
BindClientTypes();
//binding other drop down lists here
//If an existing record isbeing edited, this method will get the data from db and bind all the controls
BindMEA();
}
private void BindClientTypes()
{
DataTable dt = DB.GetAllClientTypes();
if (dt == null)
{
ltGlobalErrorMsg.Text = GlobalErrorMessage;
ltGlobalErrorMsg.Visible = true;
}
else
{
ddlClient.ClearSelection();
ddlClient.DataSource = dt;
ddlClient.DataValueField = "ID";
ddlClient.DataTextField = "Name";
ddlClient.DataBind();
ddlClient.Items.Insert(0, PleaseSelectItem);
ddlClient.ClearSelection();
}
}
In my BindMEA, I do the following
ddlClient.ClearSelection();
ddlClient.Items.FindByValue(objMea.ClientTypeID.ToString()).Selected = true;
I get an error which says, that a control cannot have two selected indexes. But There is only one at any given time. The state of the drop down changes, but when it comes to Render it bombs out.
I cannot understand why
Thanks.