Hi counterkin,
You need to add the controls inside a UpdatePanel ContentTemplateContainer instead of PlaceHolder.
Then assign AsyncPostBackTrigger to the RadioButtonList.
Finally, add the AsyncPostBackTrigger to the UpdatePanel and add the UpdatePanel to the PlaceHolder.
Refer below sample.
HTML
<asp:ScriptManager runat="server" />
<asp:PlaceHolder runat="server" ID="ContainerRBL" />
Code
C#
protected void Page_Load(object sender, System.EventArgs e)
{
this.createRBLcontrols();
}
private void createRBLcontrols()
{
Label lbl_variant = new Label();
lbl_variant.ID = "lbl_variant_option_selected";
lbl_variant.Text = "Red";
Panel titleDiv = new Panel();
titleDiv.ID = "titleDiv";
titleDiv.CssClass = "wrap";
titleDiv.Controls.Add(lbl_variant);
RadioButtonList rbl = new RadioButtonList();
rbl.ID = "rbl_product_option1";
rbl.Items.Add("Red");
rbl.Items[0].Attributes.Add("variant_name", "Red");
rbl.Items.Add("Blue");
rbl.Items[1].Attributes.Add("variant_name", "Blue");
rbl.Items[0].Selected = true;
rbl.RepeatDirection = RepeatDirection.Horizontal;
rbl.AutoPostBack = true;
rbl.SelectedIndexChanged += SelectedIndexChanged_rbl;
AsyncPostBackTrigger asyncPost = new AsyncPostBackTrigger();
asyncPost.ControlID = rbl.ID;
asyncPost.EventName = "SelectedIndexChanged";
UpdatePanel updatePanel = new UpdatePanel();
updatePanel.ContentTemplateContainer.Controls.Add(titleDiv);
updatePanel.ContentTemplateContainer.Controls.Add(rbl);
updatePanel.Triggers.Add(asyncPost);
this.ContainerRBL.Controls.Add(updatePanel);
}
private void SelectedIndexChanged_rbl(object sender, EventArgs e)
{
RadioButtonList rbl = (this.ContainerRBL.FindControl("rbl_product_option1") as RadioButtonList);
if ((rbl) != null)
{
(this.ContainerRBL.FindControl("lbl_variant_option_selected") as Label).Text = rbl.SelectedItem.Attributes["variant_name"];
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
createRBLcontrols()
End Sub
Private Sub createRBLcontrols()
Dim lbl_variant As Label = New Label()
lbl_variant.ID = "lbl_variant_option_selected"
lbl_variant.Text = "Red"
Dim titleDiv As Panel = New Panel()
titleDiv.ID = "titleDiv"
titleDiv.CssClass = "wrap"
titleDiv.Controls.Add(lbl_variant)
Dim rbl As RadioButtonList = New RadioButtonList()
rbl.ID = "rbl_product_option1"
rbl.Items.Add("Red")
rbl.Items(0).Attributes.Add("variant_name", "Red")
rbl.Items.Add("Blue")
rbl.Items(1).Attributes.Add("variant_name", "Blue")
rbl.Items(0).Selected = True
rbl.RepeatDirection = RepeatDirection.Horizontal
rbl.AutoPostBack = True
AddHandler rbl.SelectedIndexChanged, AddressOf SelectedIndexChanged_rbl
Dim asyncPost As AsyncPostBackTrigger = New AsyncPostBackTrigger()
asyncPost.ControlID = rbl.ID
asyncPost.EventName = "SelectedIndexChanged"
Dim updatePanel As UpdatePanel = New UpdatePanel()
updatePanel.ContentTemplateContainer.Controls.Add(titleDiv)
updatePanel.ContentTemplateContainer.Controls.Add(rbl)
updatePanel.Triggers.Add(asyncPost)
Me.ContainerRBL.Controls.Add(updatePanel)
End Sub
Private Sub SelectedIndexChanged_rbl(ByVal sender As Object, ByVal e As EventArgs)
Dim rbl As RadioButtonList = (TryCast(Me.ContainerRBL.FindControl("rbl_product_option1"), RadioButtonList))
If (rbl) IsNot Nothing Then
TryCast(Me.ContainerRBL.FindControl("lbl_variant_option_selected"), Label).Text = rbl.SelectedItem.Attributes("variant_name")
End If
End Sub
Screenshot