We help IT Professionals succeed at work.

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

fjalbert
fjalbert asked
on
142 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


Comment
Watch Question

Carlos VillegasFull Stack .NET Developer

Commented:
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.
Carlos VillegasFull Stack .NET Developer

Commented:
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

Carlos VillegasFull Stack .NET Developer

Commented:
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.

Author

Commented:
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

Author

Commented:
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
Carlos VillegasFull Stack .NET Developer

Commented:
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

Full Stack .NET Developer
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
I wanted to give the user more of a icon type interface, I will give this a try thx again.

Author

Commented:
Thanks
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.