Accessing child control in ASP.NET - c#

I am trying to find out if it's possible to have a control inside another control in ASP, like this:
<asp:FormView ID="FormView1" runat="server" Width="630px" Height="496px">
<ItemTemplate>
<asp:Literal ID="ID" runat="server">Idnumber: </asp:Literal><%#Eval("ID") %>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:HyperLink ID="ID" runat="server"> <%# Eval("FILE") %> </asp:HyperLink>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:FormView>
Can I access DataList1 control? I have been trying, but I can't figure it out, I should be able to access nested controls, but I cant get it to do it.

Controls inside Template tags cannot be directly accessed in code behind. Instead you should use FindControl method:
var dataList1 = (DataList)FormView1.FindControl("DataList1");
Note that this might not work in early stages of page life cycle (not until Page_Load I believe).

Related

Set value for HTML control attribute marked as run at server

Asp.Net Web Forms stops rendering value of <%#: Item %> for HTML control attribute once control is marked as run at server. Herewith snippet of Repeater ItemTemplate:
<a id="RepeaterElement" runat="server" href="?code=<%#: Item %>"><%#: Item %></a>
The resulting HTML code instead of href="?code=MyValue" becomes literally what it is behind the scenes href="?code=<%#: Item %>".
How can I manipulate with attributes of HTML control marked as run at server from .aspx file within Repeater Item?
You are close, but you need the ?code= in the databinding expression.
<asp:Repeater ID="Repeater1" runat="server" ItemType="System.String">
<ItemTemplate>
<a id="RepeaterElement" runat="server" href='<%# "?code=" + Item %>'><%# Item %></a>
</ItemTemplate>
</asp:Repeater>

Can't reference Image ID in C# Code. Why?

I would like to know, why I can't reference "asp:Image" (Image1) control in my code, but I can reference datalist control (DataList1) which contains asp:Image. Here is my code:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="imageHolder">
<asp:DataList ID="DataList1" runat="server" RepeatColumns="4" Width="100%">
<ItemTemplate>
<asp:Image ID="Image1" OnDataBinding="DataList1_DataBinding" Width="80%" Height="100px"
CssClass="datalistImages" runat="server" ImageUrl='<%# "http://mywebsite.com/" + Eval("url") %>' />
</ItemTemplate>
</asp:DataList>
</asp:Content>
And I'm 100% sure I'm working in the correct class. Any suggestions?
Because it's in the item collection.
If you want to reach that image control at real time you have to use ItemCreated or ItemDataBound events.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.items%28v=vs.110%29.aspx
Datalist events.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist_events%28v=vs.110%29.aspx

Strongly Typed Model Binding in multiple Web User Controls on a single Formview

I'm trying to use the ASP.NET 4.5 strongly typed model binding on multiple web user controls that are added to a single Formview inside the Edit and Item Templates so I can update the data.
i.e. I have an aspx page like this-ish
<asp:FormView ID="FormView1" runat="server"
ItemType="MyDataLayer.MyDataItem"
SelectMethod="FormView1_GetItem"
UpdateMethod="FormView1_UpdateItem"
DataKeyNames="MyDataItemID" >
<EditItemTemplate>
<asp:LinkButton Text="Update" runat="server" CommandName="Update"/>
<uc1:WebUserControl1 runat="server" id="WebUserControl1" />
<uc1:WebUserControl1 runat="server" id="WebUserControl2" />
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton Text="Edit" runat="server" CommandName="Edit" />
<uc1:WebUserControl1 runat="server" id="WebUserControl1" />
<uc1:WebUserControl1 runat="server" id="WebUserControl2" />
</ItemTemplate>
</asp:FormView>
With ascx user controls like this(simplified version)
<%# Control Language="C#" AutoEventWireup="false"
CodeBehind="WebUserControl1.ascx.cs"
Inherits="WebApplicationTests.WebUserControl1" %>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# BindItem.Name%>'></asp:TextBox>
I'm able to get it to work if I move the guts of the user control to the page with the formview, but it will not work in the user control.
In other words, I want the user control fields bound to the parent page Formview data with the new "BindItem", and use the formview ItemType, so I will have a single item of data, bound to multiple controls that hold different pieces of the same item within the formview.
(The project is based on a monolithic table with everything, but we want to separate the data to the user for clarity)
If there are any idea's on how to get this to work, or anyone knows why it doesn't work that would be great!

ASP.Net Repeater Eval in an If statement

So I've been looking around for a good answer to this question, but haven't really found anything useful. Hopefully someone can shed some light on this for me.
Basically, I have a repeater that is backed by a database table. Inside the ItemTemplate for this repeater, I have some HTML elements that are populated with properties from each item in the list. Pretty standard stuff. However, there is a possibility that one of the items could be null. In that case, it would make sense for me to put some sort of if (blah != null) logic around the offending code. The only problem is, when I've tried to do so, ASP throws up on me, telling me that I can't use an if statement inside of <%# %>.
My question to the masses is this: if you can't use an if statement inside of <%# %>, then how are you supposed to do conditional logic based on the values of each item?
I know that you can call your own methods inside the repeater, but that won't work for what I'm trying to do.
Below is what I'm trying to accomplish, to better illustrate my point.
<asp:Repeater runat="server" ID="repeater">
<ItemTemplate>
<div class="item-wrap">
<% if(Eval("imageUrl") != null) { %>
<div class="plan-img">
<asp:Image runat="server" ImageUrl='<%# Eval("imageUrl") %>'/>
</div>
<% } %>
</div>
</ItemTemplate>
</asp:Repeater>
inside your ItemTemplate write the markup like this:
<asp:Panel runat="server" Visible='<%# Eval("imageUrl") != null %>'>
<asp:Image runat="server" ImageUrl='<%# Eval("imageUrl") %>'/>
</asp:Panel>
Basically you can't mix code <% with databinding constructs <%#.
My advice would be to add the following property in your CodeBehind:
protected YourClass DataItem
{
get
{
return (YourClass)this.Page.GetDataItem();
}
}
and then write the markup without Eval():
<asp:Image runat="server" ImageUrl='<%# DataItem.imageUrl %>'/>
You're supposed to generate the same content for every item in the template. If you don't need to use it for a particular item just set it's visibility to false in the binding events.

c# code in control attribute in aspx file

I'd like to change the text attribute of a button according to the session landuage. So I write:
<asp:Button id="btOptIn" runat="server"
Text="<% = UI.Instance.TxtSubmit %>"
onclick="btOptIn_Click" />
This does not work. I tried several other versions (like Text='<%# Eval(UI.Instance.TxtSubmit) %>') but could not succeed.
The same code (<% = UI.Instance.TxtSubmit %>) works outside the quotes of the attribute. What is the syntax to make it work within an attribute of a control?
Thank you for your time.
<asp:textbox id="tbName" runat="server" Text='<%# Eval("test") %>' />
<%= %> is a shortened response.Write() and is never valid as an attribute, for any server tag.
<%# %> can be used, only if the conatainer is databound (the page in your case).
<%$ > can be used to access data in resources files.
In the Page_Load of you will have to make a call to Page.DataBind() for this to work.

Categories