By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,920 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,920 IT Pros & Developers. It's quick & easy.

ASP PlaceHolder control

P: n/a
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_SelectedIndex Changed"
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>

Aug 20 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

studio60podc...@gmail.com wrote:
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_SelectedIndex Changed"
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>
Aug 20 '06 #2

P: n/a
Can you make any suggestions on how to circumvent this problem?

Aug 20 '06 #3

P: n/a
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/ASPNETCont...aceholder.aspx

Let us know if this works for you?

Ken
Microsoft MVP [ASP.NET]
<st*************@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
>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_SelectedIndex Changed"
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>

Aug 20 '06 #4

P: n/a
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/ASPNETCont...aceholder.aspx

Let us know if this works for you?

Ken
Microsoft MVP [ASP.NET]
<st*************@gmail.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com...
Can you make any suggestions on how to circumvent this problem?

Aug 20 '06 #5

P: n/a
I decided to go a different route for the solution, but thank you for
the suggestion.

Ken Cox [Microsoft MVP] wrote:
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/ASPNETCont...aceholder.aspx

Let us know if this works for you?

Ken
Microsoft MVP [ASP.NET]
<st*************@gmail.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com...
Can you make any suggestions on how to circumvent this problem?
Aug 21 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.