user249375
user249375

Reputation:

add hyperlink to a gridview

I am creating a webpage using C# and asp.net I have a simple sqlite database. I have a gridview where i am displaying just simple 2 Book categories. The two categories are Fiction technical

I would like to assign a link to those categories so the user can be directed to a new page.

Here is a snapshot of the data being displayed in the gridview.. where i want to add a link to Fiction and technical to redirect to a new page. enter image description here This is my dataset and gridview.

 DataSet dsgrid;
 dsgrid = (DataSet)Cache["GridViewDataSet"];
 if (dsgrid == null)
 {
 dsgrid = GetDataSet();  //call function
 Cache["GridViewDataSet"] = dsgrid;

 }
 else
 {

 }
 //bind our cache data to a datasource
 GridView1.DataSource = dsgrid.Tables[0];
 GridView1.DataBind();
 }
 catch (Exception ex)
 {
 this.lblError.Text = ex.Message;
 }

 }

 private DataSet GetDataSet()
 {

        String connectionString = "Data Source=" + Server.MapPath(@"~\App_Data\bookDB.db");
        String selectCommand = "Select * from Category";
        SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(selectCommand, connectionString);

        DataSet ds = new DataSet();
        dataAdapter.Fill(ds);

        //GridView1.DataSource = ds;
       // GridView1.DataBind();

        return ds;

    }

Thank you

Upvotes: 1

Views: 3805

Answers (4)

apksatheesh
apksatheesh

Reputation: 1

Try like this

<asp:HyperLinkField DataTextField="Value of column you want to make as HYPERLINK" HeaderText="COLUMN NAME" runat="server" DataNavigateUrlFields="Value you want to pass to other page(fname)" DataNavigateUrlFormatString="~/demo.aspx(another page name)?fname**strong text**={0}"/>

eg:

<asp:HyperLinkField DataTextField="stid" HeaderText="stid" Target="_self" runat="server" DataNavigateUrlFields="fname" DataNavigateUrlFormatString="~/demo.aspx?fname={0}"/>

Upvotes: -1

Nudier Mena
Nudier Mena

Reputation: 3274

this is the markup code, you have to add a hyperlink to the gridview control inside the IntemTempalte collection and add a binding expression to the text property of the contained controls

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="128px">
        <Columns>
        <asp:TemplateField HeaderText="Code">
        <ItemTemplate>
        <asp:Label ID="lblCodigo" runat="server" Text='<%#Eval("columnNae")%>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
        <ItemTemplate>
       <asp:HyperLink ID="link" runat="server" Text='<%#Eval("columnNae") %>' NavigateUrl="http://nudierweb.somee.com"></asp:HyperLink>
        </ItemTemplate>
        </asp:TemplateField>

        </Columns>
    </asp:GridView>

Upvotes: 1

Doug
Doug

Reputation: 5318

Here are a few options, which one you choose will depend on the amount of flexibility you need.

Built in button column:

    <asp:GridView >
        <Columns>
            <asp:ButtonColumn DataField="some_field" Visible="false" ButtonType="linkButton" />         
        </Columns>
    /<asp:GridView >

Column template: (This will give you the most flexibility.)

<asp:GridView>
    <Columns>
        <asp:TemplateField HeaderText="Its a link!">
            <ItemTemplate>
                <asp:LinkButton ID="SomeName" runat="server" Text=''>
            </asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView> 

Enjoy!

Upvotes: 1

DiDi
DiDi

Reputation: 110

You need to use HyperLinc control

<asp:HyperLink id="hyperlink1" NavigateUrl="http://www.yournavigateurl.com"
              Text="Your Hyperlink text" runat="server"/>

Upvotes: 0

Related Questions