Click here to Skip to main content
15,907,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I have a DataList control,inside which I am showing some images from the database.Each image represents different row,therefore different Img_ID(which is also a field in my table).

Now, my requirement is:
On clicking a particular image, it will be redirected to secondpage.aspx?Img_ID=(the selected images ID)

How to do that?
I thought of having an Imagebutton,so if there are 5 rows in my table,5 image buttons will be rendered and on the specific image buttons click event I will redirect with the page with specific Img_Id as query string.

Please help me as I am in urgent need.
Thanks in advance!

My code follows:
XML
<div>
           <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:OnyxConnectionString %>"
            SelectCommand="SELECT [ImagePath], [ProjectId], [ProjectName] FROM [PPV_ProjectMaster]">
        </asp:SqlDataSource>
        <br />
        <asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="4"
            DataKeyField="ProjectId" DataSourceID="SqlDataSource1">
        <ItemTemplate>
            <table id="Table1" cellpadding="1" cellspacing="1" visible ="true">
            <tr>
                <td width="50px">
                <td width="20px">
                    <p align="left">
                    <asp:Label ID="ProjectID" runat = "server" Text='<%# DataBinder.Eval(Container.DataItem, "ProjectId")%>' Visible="false">
                    </asp:Label>
                    </p>
                </td>
                <td width="100px">
                <p align="left">
               <img id="ProjectImage_ID" alt='<%#Eval("ProjectName")%>' src= '<%#DataBinder.Eval(Container.DataItem,"ImagePath") %>'
                style="height:100px;width:100px;border:1px solid gray;"/>
              <%--  Here I want to use the Image Button control--%>
               </td>
                </p>
                </td>
                </table>
        </ItemTemplate>
        </asp:DataList>
        </div>
Posted
Updated 27-Jul-10 4:51am
v4

I'm pretty sure you want to add something like this...

<asp:imagebutton id="Image_ID" runat="server" postbackurl="secondpage.aspx?Img_ID=<%# DataBinder.Eval(Container.DataItem," imageid=") %>"imageurl"<%# DataBinder.Eval(Container.DataItem,"imagepath") %>" xmlns:asp="#unknown" />


I replaced the double quotes around the postbackurl with single quotes to fix the problem of improper nesting. If that doesn't work, create an onbind event handler for the datalist and set the onclick event for the button there.
 
Share this answer
 
v3
Comments
avishekrc 27-Jul-10 10:49am    
Thanks!!
But then just a small problem,in my second page URL,it is coming as secondpage.aspx?Img_Id=3C%#DataBinder.Eval(Container.DataItem,%22ProjectId%22)%20%%3E
instead of secondpage.aspx?Img_Id=100(the img id )
what to do?
avishekrc 27-Jul-10 10:58am    
The double quotes I had solved.It is redirecting to the second page,only problem is successfully sending the query string value.
secorbett 27-Jul-10 10:58am    
For whatever reason, the editor is screwing up what I'm trying to post. For the postbackurl, surround the value with single quotes. Get rid of the "=" sign for imageID (replace with whatever the name of that field is.)
secorbett 27-Jul-10 11:01am    
Try defining an onclick event in a databind event handler rather than a postbackurl then. It might also be possible to set a commandargument value and catch that on the receiving page, but I haven't done that before, so I do not know if it would work.
Hi,
I have found the answer,This is what I have done:
PostBackUrl='&lt;%#GetUrl(DataBinder.Eval(Container.DataItem, "ProjectId")) %&gt;'
and in CodeBehind,I have written the GetUrl function as below:

protected string GetUrl(object ProjectId)
<pre lang="cs">{
return string.Format(&quot;~/Default2.aspx?ProjectId={0}&quot;, ProjectId);
}</pre>

Thanks for your help anyway!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900