Can I populate list items in a ASP.NET dropdown list from a ASP.NET Placeholder? - asp.net

I'm designing my own custom control that contains a .NET dropdownlist. What I'm wondering is if it is possible to populate my dropdownlist with listitems placed in a placeholder?
For example:
<asp:DropDownList ID="ddlFilter" runat="server" >
<asp:PlaceHolder ID="ListItemPlaceholder" runat="server"/>
</asp:DropDownList>
This doesn't work because the DropDownList control only allows ListItems as child controls. But, I want to do something similar to this so when the user includes my control on a page, they can do something like this:
<mytag:MyControl Mode="DropDown" runat="server">
<ListItemTemplate>
<asp:ListItem Text="C" Value="c"></asp:ListItem>
<asp:ListItem Text="E" Value="e"></asp:ListItem>
<asp:ListItem Text="B" Value="b"></asp:ListItem>
</ListItemTemplate>
</myTag:MyControl>
Any suggestions or ideas? I know I can do this by dynamically adding the ListItems in the page code behind, but I'd like to avoid that if possible. Thanks!

One approach to this would be to give your control a [ParseChildren(true, "ListItemTemplate")] attribute.
If you then had a property called "ListItemTemplate", which was an array of ListItems, your markup would be parsed into that property. At runtime in your control, you could simply hand your dropdownlist the contents of that property.
Some non-tested example code:
[ParseChildren(true, "ListItemTemplate")]
public class MyControl: Control
{
private ArrayList employees = new ArrayList();
private DropDownList myDropDownList = new DropDownList();
public ArrayList ListItemTemplate
{
get
{
return employees;
}
}
protected override void CreateChildControls()
{
myDropDownList.Items.AddRange(ListItemTemplate):
Controls.Add(myDropDownList);
}
}

Related

How to hide link button based on a result returned by a class ?

I am bit new to C# and got a question.
I have a class as below that simply return false ( this is just to test)
public class SetAuthority
{
public SetAuthority()
{
//
// TODO: Add constructor logic here
//
}
public static Boolean AuthorizedToAddEdit()
{
return false;
}
}
I have a DetailsView with two link buttons to Edit and add New record. I want to hide the link buttons based on the above class method returning value.
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" visible='<%# SetAuthority.AuthorizedToAddEdit() %>'
CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" visible='<%# SetAuthority.AuthorizedToAddEdit() %>'
CommandName="New" Text="New"></asp:LinkButton>
</ItemTemplate>
Above works file and Edit and New link buttons are hidden when I run the program.
But the question is, I have a separate link button outside of the DetailsView. It is just a link to navigate to another page. I want to hide this in similar way using the same logic. I have the below code in my webform.
<asp:LinkButton ID="LinkButton5" runat="server" CausesValidation="False" visible='<%# SetAuthority.AuthorizedToAddEdit() %>'
CommandName="OpenAdminPage" Text="Open Admin Page"></asp:LinkButton>
But the link button is always visible and seems it is not calling the class and not getting the value back. It appeared to be the class not return any value and can someone help me to identify what is the different between having this and working in DetailsView and not working for a simple link button.
Note: have a workaround where I can call the same method in Page Load event that works fine without any issue. Code is below
protected void Page_Load(object sender, EventArgs e)
{
Boolean myAllowAdd;
myAllowAdd = SetAuthority.AuthorizedToAddEdit();
if (myAllowAdd == false)
{
LinkButton1.Visible = false;
}
}
The reason is that this is for databinding expressions only: <%# Since the DetailsView is databound it works there.
If you would DataBind the page it worked also for the LinkButton outside of the DetailsView:
protected void Page_Load(object sender, EventArgs e)
{
Page.DataBind();
}
inline asp.net tags... sorting them all out (<%$, <%=, <%, <%#, etc.)
Side-note: be careful with static in ASP.NET. The static method does not yet hurt. But if you'd also use static fields you'd enter a minefield since it would be shared across all requests. Your current code-behind "work-around" is the better approach anyway.

Populating Drop down dynamically using ASP.NET

In one of my form there were two Dropdown fields. The second dropdown has to be populated from the database, dynamically from the selection of first dropdown.
Any help is appreciated.
In the SelectedIndexChanged event of the first DropDownList, add code to populate the second DropDownList based on the selected value of the first list
Like this:
<asp:DropDownList runat="server" ID="from" AutoPostBack="true" CausesValidation="false" OnSelectedIndexChanged="from_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="to" />
protected void from_SelectedIndexChanged(object sender, EventArgs e)
{
var selectedValue = this.from.SelectedValue;
this.to.DataSource = /*get the data of the second list based on: selectedValue*/;
this.to.DataBind();
}

Set Custom ASP.NET UserControl variables when its in a Repeater

<%# Register Src="~/Controls/PressFileDownload.ascx" TagName="pfd" TagPrefix="uc1" %>
<asp:Repeater id="Repeater1" runat="Server" OnItemDataBound="RPTLayer_OnItemDataBound">
<ItemTemplate>
<asp:Label ID="LBLHeader" Runat="server" Visible="false"></asp:Label>
<asp:Image ID="IMGThumb" Runat="server" Visible="false"></asp:Image>
<asp:Label ID="LBLBody" Runat="server" class="layerBody"></asp:Label>
<uc1:pfd ID="pfd1" runat="server" ShowContainerName="false" ParentContentTypeId="55" />
<asp:Literal ID="litLayerLinks" runat="server"></asp:Literal>
</ItemTemplate>
</asp:Repeater>
System.Web.UI.WebControls.Label lbl;
System.Web.UI.WebControls.Literal lit;
System.Web.UI.WebControls.Image img;
System.Web.UI.WebControls.HyperLink hl;
System.Web.UI.UserControl uc;
I need to set the ParentItemID variable for the uc1:pdf listed inside the repeater.
I thought I should be able to find uc by looking in the e.Item and then setting it somehow. I think this is the part where I'm missing something.
uc = (UserControl)e.Item.FindControl("pfd1");
if (uc != null) { uc.Attributes["ParentItemID"] = i.ItemID.ToString(); }
Any thoughts would be appreciated.
Also tried this with similar results... when I debug inside my usercontrol (pfd1) the parameters I am trying to set have not been set.
uc = (UserControl)e.Item.FindControl("pfd1");
if (uc != null)
{
uc.Attributes.Add("ContainerID", _cid.ToString());
uc.Attributes.Add("ParentItemId", i.ItemID.ToString());
}
UPDATE: It looks like my controls are not connected by a namespace. I've wrapped by the parent control (Layer) and the PressFileDownlad control in a namespace "MyControls". Also updated their Inherits reference on the aspx to read "MyControls.xxxxx". I'm able to type "MyControls.Layer" inside the code on layer.aspx.cs but I'm not able to get "MyControls.PressFileDownload"
If you implement ParentItemID as a public property in your user control, then you should be able to set it declaratively, e.g:
<asp:Repeater id="Repeater1" ...>
<ItemTemplate>
<uc1:pfd ID="pfd1" runat="server" ParentItemId='<%# Eval("ItemID") %>' ... />
Martin is right you should be able to set it in declarative way (in case your property is public) .
But your way should also work (just cast it properly)
((PressFileDownload)e.Item.FindControl("pfd1")).ParentItemId = 0;
The best way is to implement the OnDataBinding event for the user control. I try to stay away from putting code inline in the aspx using webforms if possible.
When the repeater gets bound, for each item that is bound, the OnDataBinding will fire for your user control and your handler can do what it needs. You don't have to go searching for the controls.
Here is an example:
// in your aspx
<uc1:pfd ID="pfd1" runat="server" ShowContainerName="false" ParentContentTypeId="55"
OnDataBinding="pfd1_DataBinding" />
// in your codebehind implement the OnDataBinding event
protected void pfd1_DataBinding(object sender, System.EventArgs e)
{
pfd uc = (pfd)(sender);
uc.ContainerID = _containerID.ToString();
uc.ParentItemID = Eval("ItemID");
// Here you can do more like access other items like hidden fields
// or cached objects or even other controls etc... Skys the limit.
}
EDIT: Notice from your comment you require more data than what is found in the datasource. In this case what I usually do is just make private member variables in the .cs that I store data in. So when you have the container ID just store it in a variable that will be accessible.
Eg in your .cs for your page:
public partial class _TestPage : System.Web.UI.Page
{
private int _containerID { get; set; }
Then when you load the data just set the _containerID property and it will be accessible in the OnDataBinding event. Just make sure you are binding after you have set the _containerID.

Creating Dynamic controls based on the selected value of a static control

I have a drop down list holding some choices. Based on user selection I need to create some dynamic controls and render them on the form.
My understanding is that dynamic controls need to be created in OnInit or in CreateChildControls so that the ViewState for these dynamic controls is restored correctly by the runtime.
The problem is, I am unable to get the SelectedValue of the dropdown in OnInit or CreateChildControls since the ViewState has not been restored for the dropdown as yet.
Is there any way to obtain the current selection so that I can create the dynamic controls based on the current user selection and add them the page correctly
EDIT:
The markup looks as follows:
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddl" runat="server" AutoPostBack="true" AppendDataBoundItems="true">
<asp:ListItem Text="(Select Color)" Value="" />
<asp:ListItem Text="Red" Value="Red" />
<asp:ListItem Text="Green" Value="Green" />
<asp:ListItem Text="Blue" Value="Blue" />
</asp:DropDownList>
<asp:PlaceHolder ID="plHolder" runat="server" />
</div>
</form>
and here is the code behind:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void CreateChildControls()
{
base.CreateChildControls();
TextBox tb = new TextBox();
if (ddl.Text != "")
{
tb.Text = ddl.Text;
if (Session["id"] != null)
{
string id = Session["id"].ToString();
tb.ID = id;
}
else
{
Session["id"] = tb.ID = Guid.NewGuid().ToString().Replace("-", "");
}
plHolder.Controls.Add(tb);
}
}
}
On the line "tb.Text = ddl.Text;" I'm hoping to get the current selection and based on that set the value of the text property for the dynamic control. But the current selection has not been set yet since it's in OnInit.
If the controls really need to be created in OnInit or CreateChildControls then one thing you can do is get the value of your static control from the Request.Form[] collection during OnInit.
So instead of doing:
string selected = myDropDown.SelectedValue;
you do
string selected = Request.Form[myDropDownUniqueID];
... where myDropDownID is the 'unique id' assigned to myDropDown. Note that usually this will be the same as the 'id' assigned to the control, unless it is inside a control container.
This is effectively pulling the value straight out of the HTML Form data that gets sent to the server, rather than waiting for ASP.NET to unpack it into the properties of the control.
In one of my projects I add controls dynamically in Page_Load.
I use SaveControlState and LoadControlState to manually save and load the control view state.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.loadcontrolstate.aspx
Shay.

ASP.NET linkbutton visible property issue

I'm using a public variable called IsAdmin in the code behind of an aspx page.
public partial class _news : System.Web.UI.Page
{
public bool IsAdmin = false;
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.Name.Contains("admin"))
{
IsAdmin = true;
}
else
{
IsAdmin = false;
}
}
And i use the property Visible='<%#IsAdmin%>' to assign to panels which i want to show if the user is an admin in the aspx design of the page. Strangely it works for the linkbuttons i've put on the repeater.
<asp:Panel ID="Panel1" runat="server" Visible='<%#IsAdmin%>'>
<asp:LinkButton ID="LinkButton2" runat="server" PostBackUrl='<%# "news_edit.aspx? Action=edit&id=" + Convert.ToString( Eval("news_id")) %>Edit</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" PostBackUrl='<%# "news.aspx?Action=delete&id=" + Convert.ToString( Eval("news_id")) %>'>Delete</asp:LinkButton>
</asp:Panel>
and it works fine, however outside the repeater i've put another linkbutton without a panel
<asp:LinkButton ID="LinkButton4" runat="server" PostBackUrl="~/news_edit.aspx?action=new" Visible='<%#IsAdmin%>'>Add New Item</asp:LinkButton>
but the visible property doesn't work on it! I tried putting it inside a panel too and setting it's visible property but that too didn't work.
So i have following doubts
1)what is the issue?
2)what is the technical name when we use references like '<%#IsAdmin%>' in the design page
3)Does page load happen before page is rendered of after page is rendered?
Thanks
<%# %> is the syntax used for accessing databound fields. Since you are likely databinding the Repeater control at some point, these expressions will be evaluated.
Since you are likely not calling databind on the Panel and the Linkbuttons outside of the Repeater, these expressions will not be processed. You can probably change them to something like
<%= IsAdmin.ToString() %>
and get the result you want.
Check this great blog entry for more information on the differences.
Also, Page Load happens before the page is rendered. Rendering the page is the last thing that happens in the ASP.Net page lifecycle.

Resources