ASP PlaceHolder control

S

studio60podcast

I have an ASP:placeholder and I'm trying to populate with Label
controls from the code behind after a button click, essentially
creating a list of selections. But each time I click the button, it's
replacing the label in the placeholder rather than appending to it.

Is this the way the Placeholder control is supposed to work or is there
a way around this?

protected void btnApplyFilter_Click(object sender, EventArgs e)
{
Label lblAttribute = new Label();
lblAttribute.Text = ddlAttribute.SelectedItem.ToString() + ":";
lblAttribute.CssClass = "textGrey10";
phAttribute.Controls.Add(lblAttribute);

Label lblValue = new Label();
lblValue.Text = ddlValue.SelectedItem.ToString();
lblValue.CssClass = "textGrey10";
phValue.Controls.Add(lblValue);
}

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left">
<asp:Label ID="lblFilterHeader" runat="server" Text="Please select a
report filter" CssClass="textGrey10" />
</td>
</tr>
<tr>
<td align="left">
<asp:DropDownList ID="ddlAttribute" runat="server"
CssClass="textGrey10"
OnSelectedIndexChanged="ddlAttribute_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem Text="" Value="" />
<asp:ListItem Text="Model" Value="Model" />
<asp:ListItem Text="Name" Value="Name" />
<asp:ListItem Text="Item Type" Value="Item Type" />
<asp:ListItem Text="Vendor" Value="Vendor" />
<asp:ListItem Text="Location" Value="Location" />
<asp:ListItem Text="Department" Value="Department" />
<asp:ListItem Text="Description" Value="Description" />
<asp:ListItem Text="Work Authorization" Value="Work Authorization"
/>
<asp:ListItem Text="PO Date" Value="PO Date" />
<asp:ListItem Text="Warranty Expires" Value="Warranty Expires" />
<asp:ListItem Text="Invoice Number" Value="Invoice Number" />
<asp:ListItem Text="Invoice Date" Value="Invoice Date" />
<asp:ListItem Text="Date Entered" Value="Date Entered" />
<asp:ListItem Text="Serial Number" Value="Serial Number" />
<asp:ListItem Text="Purchase Cost" Value="Purchase Cost" />
<asp:ListItem Text="Active" Value="Active" />
</asp:DropDownList>
<asp:DropDownList ID="ddlValue" runat="server" CssClass="textGrey10"
/>
<asp:Button ID="btnApplyFilter" runat="server" CssClass="textGrey10"
Text="Apply Filter" OnClick="btnApplyFilter_Click" />
</td>
</tr>
</table>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" colspan="2">
<asp:Label ID="Label1" runat="server" Text="Current Filters Applied"
CssClass="textGrey10" />
</td>
</tr>
<tr>
<td align="left" width="50%">
<asp:placeHolder ID="phAttribute" runat="server" />
</td>
<td align="left" width="50%">
<asp:placeHolder ID="phValue" runat="server" />
</td>
</tr>
</table>
 
S

Sarat Pediredla

This looks like a scenario where the viewstate is not being retained
for the Placeholder or that something is happening in Page_Load which
is not being checked for this.IsPostBack

Sarat
 
K

Ken Cox [Microsoft MVP]

Yes the problem is viewstate:

"Problem:
ASP.NET gives a developer the opportunity to programmatically add controls
to a web form using ParentControl.Controls.Add(new Control());
However, these controls are not persisted in any way thus having to be
recreated for each subsequent request. "

"I have created a custom control called DynamicControlsPlaceholder that
derives from Placeholder and overrides Load- and SaveViewState.
In SaveViewState, the control hierarchy is recursively traversed and the
control type and ID persisted to a string
In LoadViewState the persisted information is used to recreate the control
tree to the state before."


http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

Let us know if this works for you?

Ken
Microsoft MVP [ASP.NET]
 
K

Ken Cox [Microsoft MVP]

Yes the problem is viewstate:

"Problem:
ASP.NET gives a developer the opportunity to programmatically add controls
to a web form using ParentControl.Controls.Add(new Control());
However, these controls are not persisted in any way thus having to be
recreated for each subsequent request. "

"I have created a custom control called DynamicControlsPlaceholder that
derives from Placeholder and overrides Load- and SaveViewState.
In SaveViewState, the control hierarchy is recursively traversed and the
control type and ID persisted to a string
In LoadViewState the persisted information is used to recreate the control
tree to the state before."


http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

Let us know if this works for you?

Ken
Microsoft MVP [ASP.NET]
 
S

studio60podcast

I decided to go a different route for the solution, but thank you for
the suggestion.
Yes the problem is viewstate:

"Problem:
ASP.NET gives a developer the opportunity to programmatically add controls
to a web form using ParentControl.Controls.Add(new Control());
However, these controls are not persisted in any way thus having to be
recreated for each subsequent request. "

"I have created a custom control called DynamicControlsPlaceholder that
derives from Placeholder and overrides Load- and SaveViewState.
In SaveViewState, the control hierarchy is recursively traversed and the
control type and ID persisted to a string
In LoadViewState the persisted information is used to recreate the control
tree to the state before."


http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

Let us know if this works for you?

Ken
Microsoft MVP [ASP.NET]


Can you make any suggestions on how to circumvent this problem?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top