From personal experience, I nearly killed myself over trying to sort out problems that I had with adding ListItems to a Drop Down List in a repeater. The code seemed to work perfectly fine outside of the repeater with the ListItems being persisted and the
_SelectedIndexChanged event being raised and other what not. I finally figured out that instead of having the Drop Down List in the markup and adding the ListItems to it, that I needed to just have an asp : placeholder in the content and the create the entire
Drop Down List from start to finish in the code behind via the _ItemDataBound event. I had checked everything including EnableViewState on the control, on the Repeater on the Page.
Of course now that its working, the whole thing has to be scrapped given that I didn't factor in how the number of items in the repeater and the Drop Down List would impact the load time of the page
I am creating one tabcontainer and 13 tabpanels dynamically in page_init.I have also created iframe in each tabpanel dynamically in page_init.Now how can i access first iframe's id.I am getting only last iframe's id once page loads.
The mypage.aspx.cs code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using AjaxControlToolkit;
using System.Configuration;
public partial class Courses : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlConnection conWeb = new SqlConnection(ConfigurationManager.ConnectionStrings["MainConnectionString"].ConnectionString);
protected UpdatePanel upanel, MainPanel;
public TabContainer MainTabcontainer, tabcontainer;
public TabPanel tabpanel;
public System.Web.UI.HtmlControls.HtmlGenericControl frame;
public Label lblpage=new Label();
int counter = 0;
<script type="text/javascript">
function getfun(index)
{
var div=$get("<%= div1.ClientID%>",document);
var Tabcontainer=$get("<%= MainTabcontainer.ClientID%>",div);
var frame=$get("<%= frame.ClientID%>",Tabcontainer.ActiveTabIndex);
Is it possible that this approach doesn't work for custom events? I've got a ul to which I'm programmatically adding (ASP.net user control) lis. I'm re-adding these user controls in the Page_Load event and 'ordinary' events (that is: LinkButton.Clicks) work
fine - only my custom events somehow lose their handlers during the postback.
Here is how I'm adding the controls to the list (the IDs are persistent over postbacks):
Newsletter_TextPart control = (Newsletter_TextPart)LoadControl("~/Newsletter/TextPart.ascx");
control.ID = "part_" + value.PartID;
control.Deleted += new Newsletter_TextPart.DeletedEventHandler(part_Deleted);
AsyncPostBackTrigger t2 = new AsyncPostBackTrigger();
t2.ControlID = control.ID;
t2.EventName = "Deleted";
upnlText.Triggers.Add(t2);
ulTextParts.Controls.Add(control);
control.Part = value;
Here is the relevant code in the UserControl:
public delegate void DeletedEventHandler(object sender, PartEventArgs e);
public event DeletedEventHandler Deleted;
protected void lbtDelete_Click(object sender, EventArgs e)
{
if (Deleted != null)
Deleted(this, new PartEventArgs(Part)); //'Part' is a property of the control
}
const bool CREATE_SELECTED = true;
const bool VIEW_SELECTED = true;
const bool CURRENT_SELECTED = true;
//store property value in viewstate so that it will survive postbacks
private bool p_Create
{
set
{
ViewState["CREATE_SELECTED"] = value;
}
get
{
return Convert.ToBoolean(ViewState["CREATE_SELECTED"]);
}
}
private bool p_View
{
set
{
ViewState["VIEW_SELECTED"] = value;
}
get
{
return Convert.ToBoolean(ViewState["VIEW_SELECTED"]);
}
}
private bool p_Current
{
set
{
ViewState["CURRENT_SELECTED"] = value;
}
get
{
return Convert.ToBoolean(ViewState["CURRENT_SELECTED"]);
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (p_Create)
{
this.CreateWasClicked();
}
if (p_View)
{
this.ViewWasClicked();
}
if (p_Current)
{
this.CurrentWasClicked();
}
}
/// <summary>
/// button used to view the view Articles
/// </summary>
protected void btnView_Click(object sender, EventArgs e)
{
//if the add article interface is already visible then hide it
if (PlaceHolder_View.Visible == true)
{
PlaceHolder_View.Visible = false;
btnView.Text = "View";
//VERY IMPORTANT -> remember that we created these controls for the next postback
this.p_View = false;
}
//if the add article interface is not visible then show it
else
{
//displaying the add/create arcticle interface
PlaceHolder_View.Visible = true;
//changing button text to hide the create article interface.
btnView.Text = "Hide View";
//VERY IMPORTANT -> remember that we created these controls for the next postback
this.p_View = true;
}
}
/// <summary>
/// for keeping view visible after post back if it was clicked
/// </summary>
protected void ViewWasClicked()
{
PlaceHolder_View.Visible = true;
}
This is the jist of using viewsates to keep post back variables in C#. This is from my code, it is showing you how to use the functionality. Manipulate this how you want, you can use any type (does not have to be bool etc).
dch3
Member
447 Points
638 Posts
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
Sep 29, 2009 06:58 PM|LINK
From personal experience, I nearly killed myself over trying to sort out problems that I had with adding ListItems to a Drop Down List in a repeater. The code seemed to work perfectly fine outside of the repeater with the ListItems being persisted and the _SelectedIndexChanged event being raised and other what not. I finally figured out that instead of having the Drop Down List in the markup and adding the ListItems to it, that I needed to just have an asp : placeholder in the content and the create the entire Drop Down List from start to finish in the code behind via the _ItemDataBound event. I had checked everything including EnableViewState on the control, on the Repeater on the Page.
Of course now that its working, the whole thing has to be scrapped given that I didn't factor in how the number of items in the repeater and the Drop Down List would impact the load time of the page
girish dagdi...
Member
45 Points
49 Posts
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
Oct 08, 2009 06:30 AM|LINK
hi,
nice article.But i have different problem here.
I am creating one tabcontainer and 13 tabpanels dynamically in page_init.I have also created iframe in each tabpanel dynamically in page_init.Now how can i access first iframe's id.I am getting only last iframe's id once page loads.
The mypage.aspx.cs code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using AjaxControlToolkit;
using System.Configuration;
public partial class Courses : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlConnection conWeb = new SqlConnection(ConfigurationManager.ConnectionStrings["MainConnectionString"].ConnectionString);
protected UpdatePanel upanel, MainPanel;
public TabContainer MainTabcontainer, tabcontainer;
public TabPanel tabpanel;
public System.Web.UI.HtmlControls.HtmlGenericControl frame;
public Label lblpage=new Label();
int counter = 0;
protected void Page_Load(object sender, EventArgs e)
{
Label myLable, myLable1;
myLable = (Label)Master.FindControl("Label1");
myLable.Text = "Courses ";
myLable1 = (Label)Master.FindControl("Label2");
myLable1.Text = "";
}
protected void Page_Init(object sender, EventArgs e)
{
Label myLable, myLable1;
myLable = (Label)Master.FindControl("Label1");
myLable.Text = "";
myLable1 = (Label)Master.FindControl("Label2");
myLable1.Text = "";
funShowData();
}
public void funShowData()
{
SqlCommand cmd = new SqlCommand("SELECT query to access ids", con);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
int i = 0;
MainPanel = new UpdatePanel();
MainTabcontainer = new TabContainer();
MainTabcontainer.Width = 660;
while (sdr.Read())
{
tabpanel = new TabPanel();
tabpanel.ID = "tab" + i.ToString();
tabpanel.EnableViewState = true;
tabpanel.HeaderText = sdr[1].ToString();
tabpanel.Enabled = true;
string val = sdr[0].ToString();
if (val.Length == 1)
{
val = "0" + val;
}
tabpanel.TabIndex = Convert.ToSByte(val);
upanel = new UpdatePanel();
upanel.ID = "upanel";
tabcontainer = new TabContainer();
tabcontainer.EnableViewState = true;
upanel.ContentTemplateContainer.Controls.Add(tabcontainer);
tabpanel.Controls.Add(upanel);
frame = new System.Web.UI.HtmlControls.HtmlGenericControl("iframe");
frame.ID = "frame"+counter;
frame.EnableViewState = true;
frame.Attributes.Add("frameborder", "0");
frame.Attributes.Add("src","Tabs/pr_tab.aspx");
frame.Attributes.Add("width", "630px");
frame.Attributes.Add("height", "385px");
tabpanel.Controls.Add(frame);
Label1.Text = "Counter "+counter;
MainTabcontainer.Tabs.Add(tabpanel);
i++; counter++;
}
sdr.Close();
con.Close();
MainPanel.ContentTemplateContainer.Controls.Add(MainTabcontainer);
div1.Controls.Add(MainPanel);
}
protected void ImageButton_download_Click(object sender, ImageClickEventArgs e)
{
if (MainTabcontainer.ActiveTabIndex == 0)
{
Label myLable, myLable1;
myLable = (Label)Master.FindControl("Label1");
myLable.Text = "Course > ";
myLable1 = (Label)Master.FindControl("Label2");
myLable1.Text = "DVLSI > e-brochure ";
ScriptManager.RegisterStartupScript(Page, this.GetType(), "getfun", "javascript:getfun(" + MainTabcontainer.ActiveTabIndex + ");", true);//To register client script
}
}
}
mypage.aspx:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript">
function getfun(index)
{
var div=$get("<%= div1.ClientID%>",document);
var Tabcontainer=$get("<%= MainTabcontainer.ClientID%>",div);
var frame=$get("<%= frame.ClientID%>",Tabcontainer.ActiveTabIndex);
alert(frame.id);
}
</script>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<div id="div2" runat="server" style="float:left">
<table>
<tr>
<td style="border: 1px solid #3399FF; height: 435px; width: 70px; "
valign="top">
<br />
<br />
<asp:ImageButton ID="ImageButton_download" runat="server" ImageUrl="~/Images/arrows.gif"
OnClick="ImageButton_download_Click" />
<br />
<br />
</td>
</tr>
</table>
</div>
<div id="div1" runat="server" style="float:right">
</div>
</asp:Content>
In function getfun(index),how can i access first tabpanels iframe.
dynamic controls
batool
Member
32 Points
100 Posts
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
Oct 14, 2009 08:22 AM|LINK
I tried to convert this code to C#, but its not working... Can anybody convert it?
miteshsura
Member
41 Points
26 Posts
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
Oct 14, 2009 04:47 PM|LINK
try this link .. http://www.developerfusion.com/tools/convert/vb-to-csharp/
batool
Member
32 Points
100 Posts
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
Oct 14, 2009 07:51 PM|LINK
I have already tried this site... I got some problems in the "DynamicControlSelection" Method
rlnd
Member
6 Points
8 Posts
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
Oct 25, 2009 06:05 PM|LINK
Is it possible that this approach doesn't work for custom events? I've got a ul to which I'm programmatically adding (ASP.net user control) lis. I'm re-adding these user controls in the Page_Load event and 'ordinary' events (that is: LinkButton.Clicks) work fine - only my custom events somehow lose their handlers during the postback.
Here is how I'm adding the controls to the list (the IDs are persistent over postbacks):
Newsletter_TextPart control = (Newsletter_TextPart)LoadControl("~/Newsletter/TextPart.ascx"); control.ID = "part_" + value.PartID; control.Deleted += new Newsletter_TextPart.DeletedEventHandler(part_Deleted); AsyncPostBackTrigger t2 = new AsyncPostBackTrigger(); t2.ControlID = control.ID; t2.EventName = "Deleted"; upnlText.Triggers.Add(t2); ulTextParts.Controls.Add(control); control.Part = value;Here is the relevant code in the UserControl:
public delegate void DeletedEventHandler(object sender, PartEventArgs e); public event DeletedEventHandler Deleted; protected void lbtDelete_Click(object sender, EventArgs e) { if (Deleted != null) Deleted(this, new PartEventArgs(Part)); //'Part' is a property of the control }User Control event ASP.net 3.5 event handler
prakashshama...
Member
4 Points
2 Posts
Re: dynamic control help plzzzzzzzzzzzzzzz.....
Oct 28, 2009 09:07 AM|LINK
girish dagdi...
Member
45 Points
49 Posts
Re: dynamic control help plzzzzzzzzzzzzzzz.....
Oct 29, 2009 05:11 AM|LINK
hi,
you have written -- having error with find control part
but where is the code for finding control.
dynamic controls
mausimo
Member
118 Points
132 Posts
Re: dynamic control help plzzzzzzzzzzzzzzz.....
Nov 05, 2009 04:36 PM|LINK
For everyone that wanted viewstate code in C#....
const bool CREATE_SELECTED = true; const bool VIEW_SELECTED = true; const bool CURRENT_SELECTED = true; //store property value in viewstate so that it will survive postbacks private bool p_Create { set { ViewState["CREATE_SELECTED"] = value; } get { return Convert.ToBoolean(ViewState["CREATE_SELECTED"]); } } private bool p_View { set { ViewState["VIEW_SELECTED"] = value; } get { return Convert.ToBoolean(ViewState["VIEW_SELECTED"]); } } private bool p_Current { set { ViewState["CURRENT_SELECTED"] = value; } get { return Convert.ToBoolean(ViewState["CURRENT_SELECTED"]); } }protected void Page_Load(object sender, EventArgs e) { if (p_Create) { this.CreateWasClicked(); } if (p_View) { this.ViewWasClicked(); } if (p_Current) { this.CurrentWasClicked(); } }/// <summary> /// button used to view the view Articles /// </summary> protected void btnView_Click(object sender, EventArgs e) { //if the add article interface is already visible then hide it if (PlaceHolder_View.Visible == true) { PlaceHolder_View.Visible = false; btnView.Text = "View"; //VERY IMPORTANT -> remember that we created these controls for the next postback this.p_View = false; } //if the add article interface is not visible then show it else { //displaying the add/create arcticle interface PlaceHolder_View.Visible = true; //changing button text to hide the create article interface. btnView.Text = "Hide View"; //VERY IMPORTANT -> remember that we created these controls for the next postback this.p_View = true; } } /// <summary> /// for keeping view visible after post back if it was clicked /// </summary> protected void ViewWasClicked() { PlaceHolder_View.Visible = true; }This is the jist of using viewsates to keep post back variables in C#. This is from my code, it is showing you how to use the functionality. Manipulate this how you want, you can use any type (does not have to be bool etc).
Good-Luck
-Mike
jamesdeepak
Member
112 Points
59 Posts
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
Mar 11, 2010 02:43 PM|LINK
that code was really understandbable , had no problem converting that to C# .So this is why my Controls started disappearing Lol.
thanks a bunch for the explanation.