Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

In Gridview Set Image Column programatically

Posted on 2008-10-17
17
Medium Priority
?
2,043 Views
Last Modified: 2008-10-27
In Gridview I have column representing Thumbnail image which works fine.:
            <asp:ImageField DataImageUrlField="PicFile1"
DataImageUrlFormatString="Images/A009/Thumbs/{0}">
            </asp:ImageField>

I want the fixed 'A009' to be replaced by Session variable - "ClCode"

VB in code-behind I have:
iPicFile = ImageString & Session("ClCode") & "/Thumbs/{0}"
How can "iPicFile" variable be used in DataImageUrlFormatString?
With thanks
0
Comment
Question by:eharford
  • 8
  • 8
17 Comments
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22745451
Inside RowDataBound event handler for the grid view say
dim img as ImageButton  = e.Row.FindControl(imagebuttonid) as ImageButton
img.ImageURL = ImageString & Session("ClCode") & "/Thumbs...."
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22745546
A little modification of TechTiger007's solution:
dim img as ImageButton  = e.Row.Cells(i).Controls(0) as ImageButton 'Change "i" to the actual index of the ImageField in GridView.

0
 

Author Comment

by:eharford
ID: 22747558
Thanks for your suggestion. I am getting "End of Statement expected" on "ImageButton":
Code:
Protected Sub dgPInv_RowDatabound(ByVal sender As Object, ByVal e as System.Web.UI.WebControls.GridViewRowEventArgs) Handles dgPInv.RowDataBound
Dim img As ImageButton = e.Row.Cells(12).Controls(0) as ImageButton
img.ImageURL = ImageString & Session("ClCode") & "/Thumbs/{0}"
End Sub
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 22

Expert Comment

by:prairiedog
ID: 22747826
Try this:

Dim img As ImageButton = CType(e.Row.Cells(12).Controls(0), ImageButton)

Open in new window

0
 

Author Comment

by:eharford
ID: 22748993
Your suggestion sorted the End of statement problem. I am now getting an ArgumentOutOfRangeException.
I understand that "e.Row.Cells(12)" represents the Column in the Gridview. What does ".Controls(0)" represent?
With thanks.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22749429
Wait a second. This is an ImageField, not ImageButton. My apology. Use this line:

Dim img As Image = CType(e.Row.Cells(12).Controls(0), Image)

Controls(0) represents the first control in Cell(12).

Make sure the ImageField is the 13th column of your GridView.


0
 

Author Comment

by:eharford
ID: 22760097
Apologies for labouring this problem. I have created the gridview using the SQLDatabase wizard and bound grid to it. I have ImageField as 13th column in grid as follows:
<asp:ImageField DataImageUrlField="PicFile1"></asp:ImageField>
PicFile1 holds the image file name.
I also have following code in code-behind:
Protected Sub dgPInv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles dgPInv.RowDataBound
Dim img As Image = CType(e.Row.Cells(12).Controls(0), Image)
img.ImageUrl = ImageString & Session("ClCode") & "/Thumbs/"
End Sub
I am still getting System.ArgumentOutOfRangeException was unhandled by user code
  Message="Specified argument was out of the range of valid values.
Parameter name: index"
  ParamName="index"
  Source="System.Web"

How do I refer to the variable file name in the ImageUrl line? I was using {0}.
With thanks
 
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22761013
Can you post your GridView code (ASPX file)?
0
 

Author Comment

by:eharford
ID: 22761721
Dont seem to be able to send aspx files  within or without zip file.
ASPX file:
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Invent2.aspx.vb" Inherits="Invent2" title="Constantine Ltd - Inventory Paged" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Button ID="cmdAll" runat="server" Text="Invent - All" Width="85px" />
    <asp:Button ID="cmdBack" runat="server" Text="Back" Width="85px" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Connectstring %>"
        SelectCommand="cClInv" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:SessionParameter Name="clCode" SessionField="ClCode" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:GridView ID="dgPInv" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
        DataKeyNames="InventNo" DataSourceID="SqlDataSource1" Width="967px">
        <Columns>
            <asp:CommandField ButtonType="Button" ShowSelectButton="True" />
            <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
            <asp:BoundField DataField="clInventNo" HeaderText="Client InvNo" SortExpression="clInventNo" />
            <asp:BoundField DataField="clInventNo2" HeaderText="Cat No" SortExpression="clInventNo2" />
            <asp:BoundField DataField="ArtistMaker" HeaderText="Artist" SortExpression="ArtistMaker" />
            <asp:BoundField DataField="ShortDesc" HeaderText="Title" SortExpression="ShortDesc" />
            <asp:BoundField DataField="iDate" HeaderText="Date" SortExpression="iDate" />
            <asp:BoundField DataField="Medium" HeaderText="Medium" SortExpression="Medium" />
            <asp:BoundField DataField="Height" HeaderText="Length" SortExpression="Height" />
            <asp:BoundField DataField="Width" HeaderText="Width" SortExpression="Width" />
            <asp:BoundField DataField="Depth" HeaderText="Height" SortExpression="Depth" />
            <asp:BoundField DataField="AcquPriceEquiv" DataFormatString="{0:C}" HeaderText="Value"
                HtmlEncode="False" SortExpression="AcquPriceEquiv" />
            <asp:ImageField DataImageUrlField="PicFile1"></asp:ImageField>
        </Columns>
        <HeaderStyle BackColor="Navy" Font-Size="Small" ForeColor="White" />
        <PagerSettings Position="TopAndBottom" Mode="NumericFirstLast" FirstPageText="First &amp;lt;&amp;lt;" LastPageText="Last &amp;gt;&amp;gt;" />
        <PagerStyle Font-Size="Small" />
    </asp:GridView>
  </asp:Content>
 
Vb file:
Partial Class Invent2
    Inherits System.Web.UI.Page
    Private ImageString As String = ConfigurationSettings.AppSettings("ImagePath")
 
    Protected Sub dgPInv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles dgPInv.RowDataBound
        Dim img As Image = CType(e.Row.Cells(12).Controls(0), Image)
        img.ImageUrl = ImageString & Session("ClCode") & "/Thumbs/"
    End Sub
    Protected Sub dgPInv_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgPInv.SelectedIndexChanged
        Dim index As Integer = dgPInv.SelectedIndex
        Response.Redirect("InvPart.aspx?txtInvNo=" + Me.dgPInv.DataKeys(index).Value.ToString)
    End Sub
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("Private") = False Then Response.Redirect("Default.aspx")
    End Sub
 
    Protected Sub cmdAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdAll.Click
        Response.Redirect("Invent.aspx?txtClCode=" + Session("ClCode"))
    End Sub
 
    Protected Sub cmdBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdBack.Click
        Response.Redirect("Account.aspx?txtClCode=" + Session("ClCode"))
    End Sub
End Class

Open in new window

0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22761859
OK, you will need to check if the row is a DataRow before you can use FindControl
Protected Sub dgPInv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles dgPInv.RowDataBound 
If (e.Row.RowType = DataControlRowType.DataRow) Then
  Dim img As Image = CType(e.Row.Cells(12).Controls(0), Image) 
  img.ImageUrl = ImageString & Session("ClCode") & "/Thumbs/" 
End If
End Sub 

Open in new window

0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22761898
Sorry, I should have caught the fact earlier that you are missing "If (e.Row.RowType.." in your RowDataBound event handler.
0
 

Author Comment

by:eharford
ID: 22762935
I am still not getting the image file name into img.imageurl. The properties of the image in the grid is coming through as the path only. How do I get the file name from the data source?
I am nearly there - I am very grateful to you.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22767898
In your original post, you have this:
iPicFile = ImageString & Session("ClCode") & "/Thumbs/{0}"

Where do you do this?
0
 

Author Comment

by:eharford
ID: 22774466
I was using:
DataImageUrlFormatString="Images/A009/Thumbs/{0}">
in the properties of the image column in the grid
and
iPicFile = ImageString & Session("ClCode") & "/Thumbs/{0}"
in the code-behind
I have set in the properties of the ImageField in the grid:
<asp:ImageField DataImageUrlField="PicFile1"></asp:ImageField> and am trying to add the value of PicFile1 to the iPicFile string.
0
 
LVL 22

Assisted Solution

by:prairiedog
prairiedog earned 1600 total points
ID: 22776425
If you define your ImageField as described in your original post:
<asp:ImageField DataImageUrlField="PicFile1"
DataImageUrlFormatString="Images/A009/Thumbs/{0}">
            </asp:ImageField>

then when the GirdView is bound, {0} will be replaced with the value of "PicFile1" automatically. So what you want is to replace "A009" with a Session variable Session("CICode"), is it right? If yes, then you can do this as attached code snippet. If not, please let me know.
Also, you mentioned a couple of times that in code-behind, you have iPicFile = ImageString & Session("ClCode") & "/Thumbs/{0}", but it does not make sense to me because you can't have "{0}" in your string concatenation. Moreover, "in code-behind" does not help me understand: where exactly (in what event handler ) do you have that line of code? What is ImageString?

Protected Sub dgPInv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles dgPInv.RowDataBound 
If (e.Row.RowType = DataControlRowType.DataRow) Then
  Dim img As Image = CType(e.Row.Cells(12).Controls(0), Image) 
  img.ImageUrl = img.ImageUrl.Replace("A009", Session("CICode")) 
End If
End Sub 

Open in new window

0
 

Accepted Solution

by:
eharford earned 0 total points
ID: 22777012
Many, many thanks - that works perfectly. In summary I have been struggling between the aspx and the code behind file
ImageString is the variable I was using to bring the path in from the web.config.
0
 

Author Comment

by:eharford
ID: 22777160
Done
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question