?
Solved

Using an Image button control in Gridview  How to pass a parameter  to anther page

Posted on 2011-04-22
9
Medium Priority
?
126 Views
Last Modified: 2013-12-29
I would like to see some code and how to setup a image button with a Gridview, that you can click and pass a parameter to anther page. I am coding in VB.net


0
Comment
Question by:fjalbert
  • 5
  • 4
9 Comments
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35449125
Hello, you what to do this?

In your code behind file:
    Protected Function MyUrl(ByVal myParam As String) As String
        Return "~/MyPage.aspx?Param=" & Server.UrlEncode(myParam)
    End Function

Open in new window


In your aspx file:
<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:HyperLink Text="My Link" NavigateUrl='<%# MyUrl(Eval("MyParam")) %>' runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Open in new window


You can also assign an image to the HyperLink control by using ImageUrl property.
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35449143
The HyperLink control must be in this way to open a new window when clicked:
<asp:HyperLink Text="My Link" NavigateUrl='<%# MyUrl(Eval("MyParam")) %>' runat="server" ImageUrl="MyImage.png" Target="_blank" />

Open in new window

0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35449163
By the way, as I say, Im using a HyperLink control to do that instead of a ImageButton, if you only want to use an image with a custom link that opens in a new window (or tab) this is the way to go.
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:fjalbert
ID: 35464039
Thanks for the help, so i wanted to use the image button, can you help with code to add an image to the image button thx
0
 

Author Comment

by:fjalbert
ID: 35464139
Here is the code that i am using for an image button:

<asp:TemplateField HeaderText="Link">
<ItemTemplate>
 <asp:imagebutton ID="Imageb1"
 NavigateUrl='<%# MyUrl(Eval("HeadCount_ID")) %>' runat="server"
ImageUrl="~/Images/help.gif" /></imagebutton>
 </ItemTemplate>
 </asp:TemplateField>  the linked work fine but the Image button getting errors
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35464513
Hi, ok, I will give you the image button code, but in your problem description you want to pass a parameter to another page, notice that the ImageButton dont work in a direct way, it will do a postback, then in your server you need to catch that event and take the command name and argument to make the correct page redirect:

<asp:TemplateField>
    <ItemTemplate>
        <asp:ImageButton runat="server" ID="ImageButton1" ImageUrl="~/Images/help.gif" CommandName="MyCommand" CommandArgument='<%# Eval("MyParam") %>' />
    </ItemTemplate>
</asp:TemplateField>

Open in new window

0
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 2000 total points
ID: 35464520
Why you want to use a ImageButton control? the HyperLink and ImageButton controls looks the same thing, but they have different behaviour.
0
 

Author Comment

by:fjalbert
ID: 35469468
I wanted to give the user more of a icon type interface, I will give this a try thx again.
0
 

Author Closing Comment

by:fjalbert
ID: 39745232
Thanks
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

749 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