0

Developing In: c# asp.net web forms 4.5

I've looked at many posts and all of them made a hyperlink with either datafield in it, by datatextfield() or similar.. or called eval from backend. I didn't do neither of that, so I tried doing every thing I could but it didn't work. CommandField, HyperLinkeField, HyperLink, .. all didn't work.

The main point is that the data comes out fine, but I can't seem to make it hyperlink. Is it impossible to have a hyperlink this way?

I'm trying to make a hyperlink on gridview. The thing is that the column I'm trying to make the hyperlink doesn't get data from the datafield.

It gets it by onRowDataBound method of gridview something like this..

<asp:GridView runat="server" ID="someGrid" CellPadding="10" 
    DataKeyNames="idx" AutoGenerateColumns="false"
    selectMethod="someGrid_GetData" ItemType="someTable"
    updateMethod="someGrid_UpdateItem" AutoGenerateEditButton="true"
    deleteMethod="someGrid_DeleteItem" AutoGenerateDeleteButton="true"
    onRowDataBound="someGrid_RowDataBound">
    <Columns>
        <asp:BoundField DataField="thing1" HeaderText="thing1" />
        <asp:BoundField DataField="thing2" HeaderText="thing2"/>
        <asp:DynamicField DataField="poDate" DataFormatString="{0:d}" />
        <asp:BoundField HeaderText="vendor" />
        <asp:CommandField HeaderText="sku" ShowSelectButton="true" SelectText="{0}" ButtonType="Link"/>
    </Columns>
</asp:GridView>

and on the code background, it goes like this..

    protected void soGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {  
        using (soAction soa = new soAction())
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string s = e.Row.Cells[2].Text;
                string thin1 = soa.get_thin1(s);
                e.Row.Cells[4].Text = thin1;

                string thin2 = soa.get_thin2(s);
                e.Row.Cells[5].Text = thin2;
            }
        }
    }

Thanks!

edit : Maybe I wasn't clear about what is the challenge here.. normally the examples use dataTextField property in gridview but I cannot use the dataTextField property because I'm binding the data depending on the model rendered afterwards. I'm doing this because I wanted to set the itemType into someTable so I can use the updatemethod and deleteMethod of the asp.net.

Jinmo Chong
  • 91
  • 1
  • 12
  • do a google search on `new DataGridViewLinkColumn();` for examples it's pretty straight forward – MethodMan Dec 08 '17 at 19:04
  • @MethodMan Thanks man. Will do. Didn't know about that. – Jinmo Chong Dec 08 '17 at 19:05
  • this is what `Google.com` is there for.. others as well as yourself should make this a habit, meaning search first .. ask second – MethodMan Dec 08 '17 at 19:06
  • @MethodMan I don't think we can use DataGridViewLinkColumn.. it's for winForm right? I'm building a web forms asp.net 4.5 version and I can't import it here... – Jinmo Chong Dec 08 '17 at 19:14
  • ok, then do a google search for examples using asp.net web forms.. come on now.. – MethodMan Dec 08 '17 at 19:22
  • @MethodMan I did. And I couldn't find a solution similar to mine. All of them uses eval or dataTextField property, like this...https://www.aspsnippets.com/Articles/HyperLinkField-HyperLink-in-ASPNet-GridView-Example.aspx or https://stackoverflow.com/questions/12823422/gridview-hyperlink-field-in-c-sharp or https://stackoverflow.com/questions/21243070/how-to-add-hyperlink-to-boundfield-in-gridview-c-sharp-asp-net. They all use dataTextField but I cannot use dataTextField so I'm asking – Jinmo Chong Dec 08 '17 at 19:28
  • here is 1 https://stackoverflow.com/questions/19798685/how-to-create-a-link-in-gridview-in-asp-net – MethodMan Dec 08 '17 at 19:30
  • here is another https://stackoverflow.com/questions/2530297/hyperlinks-in-datagridview if you do not know how to use eval, the copy paste and follow the example as well as read up on that as well – MethodMan Dec 08 '17 at 19:31
  • @MethodMan Nope. Those won't work. because they get the value from the text property and then put hyperlink on them. In my case, the text is already binded by the backend code, so I cannot call text property or dataTextField property. – Jinmo Chong Dec 08 '17 at 19:36
  • then you should be able to create the hyper link.. especially if it's being provided not sure why you are finding this to be so difficult.. – MethodMan Dec 08 '17 at 19:38
  • @MethodMan I mean the data is provided, but I can't make it a hyperlink. I tried making hyperlinkField, hyperlink, commandfield, but nothing worked. – Jinmo Chong Dec 08 '17 at 19:40
  • https://stackoverflow.com/questions/12166354/hyperlink-a-column-with-relative-path-value-of-another-column-in-mysql-database this falls under `` – MethodMan Dec 08 '17 at 19:41
  • @MethodMan Nope. Again, they provide text field or dataTextField on the property. – Jinmo Chong Dec 08 '17 at 19:48
  • I actually decided to make another column with details and make that a hyperlink because I could not spend more time on this issue. But as my table is getting bigger, It would have been nice to make the column itself as a hyperlink. Still would be thrilled to know how to do this. – Jinmo Chong Dec 08 '17 at 20:49
  • Something like this: https://stackoverflow.com/questions/47702144/how-to-replace-ids-of-users-with-their-fullnames-in-the-current-scenario/47703596#47703596 ? You could set the href or any other value using a method. – wazz Dec 09 '17 at 08:17

1 Answers1

2

Just use a TemplateField like this, you can do anything you want in a templatefield, combine data from multiple columns, create controls, whatever:

<Columns>    
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
        <EditItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server"
                Text='<%# Bind("FirstName") %>'></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server"
                Text='<%# Bind("FirstName") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

https://learn.microsoft.com/en-us/aspnet/web-forms/overview/data-access/custom-formatting/using-templatefields-in-the-gridview-control-cs

Sea Charp
  • 288
  • 1
  • 6
  • This is actually what I needed. I just figured this out just before you post this. Thanks, but if you posted this sooner I could've saved at least a day. But again, thanks. – Jinmo Chong Dec 11 '17 at 15:06