Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Dec 23, 2012 06:26 AM by h.levy
Member
2 Points
12 Posts
Dec 22, 2012 07:20 PM|LINK
Hi,
I have this code:
<asp:DetailsView ID="DetailsView1" DataKeyNames="PB_ID" runat="server" Height="50px" Width="560px" AutoGenerateRows="False" BackColor="White" BorderColor="Black" BorderStyle=Solid BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" GridLines="Horizontal" > <Fields> <asp:TemplateField HeaderText="Hello World" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" ControlStyle-Width="222" > <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("Colum1") %>'></asp:Label> <div style="display:<%#IIF(ISDBNULL(Eval("Img_Name")), "none", "block")%>"> <asp:Image ID="Image2" ImageUrl='<%# DataBinder.Eval(Container, "DataItem.Img_Name")%>' runat="server" /> </div> <div style="display:<%#IIF(ISDBNULL(Eval("Img_Name")), "block", "none")%>"> <asp:Image ID="Image1" ImageUrl="~/Images/imgnotfound.jpg" runat="server" Width="100" /> </div> </ItemTemplate> </asp:TemplateField> </Fields> </asp:DetailsView>
and I want to change "Image2" through .VB (Image2.ImageUrl = "Something else.jpg"). The problem is that the compiler cannot find the variable. (Page.FindControl(String) is not working either).
Someone can help me out?
Contributor
4820 Points
891 Posts
Dec 22, 2012 07:30 PM|LINK
From what event you want to do that? Load, Binding etc.? It differs a lot you know
Dec 22, 2012 07:33 PM|LINK
I want to do it from .vb functions.
From "Page Load" and few buttons.
Participant
1380 Points
287 Posts
Dec 22, 2012 08:40 PM|LINK
For each row that you want to access the Image2 properties you will have to:
Image img = (Image)row.FindControl("Image2");
if(img != null) { Image.url = some url; Image.AlternateText = "Alternate text"; }
You could add this logic to the row created event so that when you are binding the gridview each row is evaluated.
Check this below..
protected void grdACMList_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // Retrieve the Image control from the first column. Image img = (Image)e.Row.Cells[1].Controls[0]; //Rest is you logic.. } }
All-Star
31055 Points
6356 Posts
Dec 22, 2012 10:13 PM|LINK
Dec 23, 2012 06:26 AM|LINK
I cant believe its so simple. Thanks!
h.levy
Member
2 Points
12 Posts
How to access an Image within a DetailsView?
Dec 22, 2012 07:20 PM|LINK
Hi,
I have this code:
<asp:DetailsView ID="DetailsView1" DataKeyNames="PB_ID" runat="server" Height="50px" Width="560px" AutoGenerateRows="False" BackColor="White" BorderColor="Black" BorderStyle=Solid BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" GridLines="Horizontal" > <Fields> <asp:TemplateField HeaderText="Hello World" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" ControlStyle-Width="222" > <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("Colum1") %>'></asp:Label> <div style="display:<%#IIF(ISDBNULL(Eval("Img_Name")), "none", "block")%>"> <asp:Image ID="Image2" ImageUrl='<%# DataBinder.Eval(Container, "DataItem.Img_Name")%>' runat="server" /> </div> <div style="display:<%#IIF(ISDBNULL(Eval("Img_Name")), "block", "none")%>"> <asp:Image ID="Image1" ImageUrl="~/Images/imgnotfound.jpg" runat="server" Width="100" /> </div> </ItemTemplate> </asp:TemplateField> </Fields> </asp:DetailsView>and I want to change "Image2" through .VB (Image2.ImageUrl = "Something else.jpg").
The problem is that the compiler cannot find the variable.
(Page.FindControl(String) is not working either).
Someone can help me out?
Kulrom
Contributor
4820 Points
891 Posts
Re: How to access an Image within a DetailsView?
Dec 22, 2012 07:30 PM|LINK
From what event you want to do that? Load, Binding etc.? It differs a lot you know
My Blog: ASP.NET Stuff
h.levy
Member
2 Points
12 Posts
Re: How to access an Image within a DetailsView?
Dec 22, 2012 07:33 PM|LINK
I want to do it from .vb functions.
From "Page Load" and few buttons.
Madhu1234
Participant
1380 Points
287 Posts
Re: How to access an Image within a DetailsView?
Dec 22, 2012 08:40 PM|LINK
For each row that you want to access the Image2 properties you will have to:
Image img = (Image)row.FindControl("Image2");
if(img != null)
{
Image.url = some url;
Image.AlternateText = "Alternate text";
}
You could add this logic to the row created event so that when you are binding the gridview each row is evaluated.
Check this below..
protected void grdACMList_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the Image control from the first column.
Image img = (Image)e.Row.Cells[1].Controls[0];
//Rest is you logic..
}
}
oned_gk
All-Star
31055 Points
6356 Posts
Re: How to access an Image within a DetailsView?
Dec 22, 2012 10:13 PM|LINK
h.levy
Member
2 Points
12 Posts
Re: How to access an Image within a DetailsView?
Dec 23, 2012 06:26 AM|LINK
oned_gk,
I cant believe its so simple.
Thanks!