Retrieve links stored in DB and show it in GridView - asp.net

Need a small help.
I'm having a table which is having links stored in some of the columns. I'm binding the table with a GridView present on my page. I need the cells of GridView to act as a link.
I have tried this code.
<asp:GridView runat="server" ID="gvAutomationTesting" CssClass="table table-bordered table-hover table-responsive" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Link 01" SortExpression="Link01">
<ItemTemplate>
<asp:LinkButton ID="lb01" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Link01") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The output which i'm getting is having a tag but without href attribute because of which even if i try to click it is not redirecting to the desired page.

hope below code snippets will help you, using templatefield you can assign the link button to grid column
<div>
<asp:GridView id="mygrid">
<asp:TemplateField headertext="number" datatype="string" filterexpression="CUSTOMERNUMBER"
sortexpression="CUSTOMERNUMBER">
<itemtemplate>
<asp:LinkButton ID="lnkbtn" runat="server" Text='<%# DataBinder.Eval(container.DataItem, "CUSTOMERNUMBER") %>' />
<%--CUSTOMERNUMBER is your DB column name--%>
</itemtemplate>
</asp:TemplateField>
<asp:boundfield headertext="Customer" datafield="CUSTOMERNAME" datatype="string" filterexpression="CUSTOMERNAME"
sortexpression="CUSTOMERNAME">
</asp:boundfield>
</asp:GridView>
</div>

Replace
<asp:LinkButton ID="lb01" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Link01") %>' />
by
<a ID="lb01" runat="server" href='<%# DataBinder.Eval(Container.DataItem, "Link01")' ><%# DataBinder.Eval(Container.DataItem, "Link01") %> </a>

Related

How to add a button control dynamically into a GridView cell ? VB.NET

Look, it´s simple. I have a GridView which is populated with data from my database.
What I want is put a button in each cell that contains a specific datum.
Pls, look the image below. It describes exactly what I want
In VB.NET, pls! =)
Example Image
Thanks very much!
You are going to need to define the button control inside of the <Columns> section of your GridView markup, like this:
<asp:gridview id="CustomersGridView" runat="server">
<columns>
<asp:boundfield datafield="DateColumn" headertext="Date"/>
<asp:TemplateField>
<HeaderTemplate>
Positive
</HeaderTemplate>
<ItemTemplate>
<asp:Label id="LabelPositive" runat="server" Text='<%# Eval("PositiveColumn")%>' />
<br />
<asp:Button id="ButtonPositive" runat="server" Text="Show" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Negative
</HeaderTemplate>
<ItemTemplate>
<asp:Label id="LabelNegative" runat="server" Text='<%# Eval("NegativeColumn")%>' />
<br />
<asp:Button id="ButtonNegative" runat="server" Text="Show" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Neutral
</HeaderTemplate>
<ItemTemplate>
<asp:Label id="LabelNeutral" runat="server" Text='<%# Eval("NeutralColumn")%>' />
<br />
<asp:Button id="ButtonNeutral" runat="server" Text="Show" />
</ItemTemplate>
</asp:TemplateField>
<asp:boundfield datafield="NoCommentsColumn" headertext="No Comments"/>
<asp:boundfield datafield="TotalColumn" headertext="Total"/>
</columns>
</asp:gridview>
Note: The datafield and Eval() calls are bound to made up names like NeutralColumn and NoCommentsColumn, substitute those names with your real database field names.
You need to add while data binding the grid. Look for an event call and handle your code there.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

How to insert comma in the value which is present in Hyperlink column in gridview

Here is my markup:
<asp:TemplateField HeaderText="Loss" SortExpression="Loss"
HeaderStyle-CssClass="StrongText" HeaderStyle-Font-Bold="true"
HeaderStyle-Font-Size="Medium" HeaderStyle-ForeColor="Blue"
HeaderStyle-Font-Underline="true" HeaderStyle-VerticalAlign="Bottom"
ItemStyle-Width="3%" ItemStyle-HorizontalAlign="Left"
ItemStyle-VerticalAlign="Bottom" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:HyperLink runat="server" ID="HyperLink8"
NavigateUrl='<%#"db_LossofPay.aspx?UserID="+ Eval("User ID")%>'
Text='<%# Eval("Loss","{0}")%>'
ForeColor="BLUE" Target="_blank"></asp:HyperLink>
<asp:Label ID="lblLoss" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Could you please help me with this? In BoundField I wrote DataFormatString="{0:N2}" which works fine, but I got stuck when using in hyperlink (template field)
Try with this:
<asp:HyperLink runat="server" ID="HyperLink8"
NavigateUrl='<%#"db_LossofPay.aspx?UserID="+ Eval("User ID")%>'
Text='<%# Eval("Loss","{0:#,##0.00}") %>' ForeColor="BLUE" Target="_blank" />
Try
Text='<%# String.Format("{0:N2}", DataBinder.Eval(Container.DataItem, "Loss"))%>'

In asp.net how to add meno or dropdown control in asp.net?

Data gridview bind the recored with data set.Gird view consist the column "Compnayname"
whenever user click on the column of gridview "Company name" then menu or dropdown control will be display.
so how to add dropdown or menu control in gridview.
I want to data display in grid view control and user click on company name then Menu will be display which contain the information like Send msg, save company detail.
You have to make Template Column
<asp:TemplateField HeaderText="Compnayname">
<ItemTemplate>
<asp:DropDownList ID="ddlCompany" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
here isthe complete GRIDView
<asp:GridView ID="grdList" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="grdList_RowCancelingEdit" OnRowEditing="grdList_RowEditing" OnRowUpdating="grdList_RowUpdating" OnPageIndexChanging="grdList_PageIndexChanging" AllowPaging="true">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label SkinID="OneColLabel" ID="lblName" runat="server" HeaderText="Edit" />
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnbEdit" runat="server" CommandName="Edit" Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnbUpdate" runat="server" CommandName="Update" Text="Update" />
<asp:LinkButton ID="lnbCancel" runat="server" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="drpEditName" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You have to bind Dropdownlist on code behind

Merge datagrid cells

i have to merge the cells from the cell that does not contain the radio button, to the cell that contains the radio button.
here the link for the interface
http://i839.photobucket.com/albums/zz316/girish_kolte/untitled.jpg
You need to use the TemplateField and here is a tutorial that explains some of the other fields that GridView offers as well.
<asp:GridView ID="gvwAirportSchedule" runat="server">
<Columns>
....
<asp:TemplateField>
<ItemTemplate HeaderText="Airport">
<asp:RadioButton ID="rbAirport" runat="server" Visible='<%# (bool)Eval("IsDestination") %>' />
<asp:Label runat="server" ID="Label1" Text='<%# Eval("Airport") %>' />
</ItemTemplate>
</asp:TemplateField>
....
</Columns>
</asp:GridView>
good answer from David.
One could reduce, by omitting Lable like below
<asp:RadioButton ID="RadioButton1" runat="server" Text='<%# Eval("Airport") %>' />

Simple databinding to gridview columns

I have a GridView that I use to show my users the result of a search. I want to allow them to choose which columns are shown on the GridView when performing their search. Simple enough, yes? I wanted to try doing this using just databinding, no events. Unfortunately, my code fails to update the GridView using checkboxes bound to the column's Visible property. The state of the chechboxes changes, but the Visible property of the columns does not.
Snippet of Search.aspx:
<myControl:FacultyGridView ID="FacultyGridView1" runat="server" />
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("HeaderText") %>' Checked='<%# Bind("Visible") %>' AutoPostBack=true/></ItemTemplate>
</asp:Repeater>
Code-behind snippet in Search.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = FacultyGridView1.GridView.Columns;
Repeater1.DataBind();
}
To be clear, the GridView is exposed as a public property of a user control named FacultyGridView. Relevant snippet of FacultyGridView.ascx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" AllowSorting="True" PageSize="25">
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" />
<asp:TemplateField HeaderText="University" SortExpression="UniversityID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("University.Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("DivisionMemberships") %>'>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Division.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" SortExpression="Title" />
<asp:TemplateField HeaderText="Research Type">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("ResearchTypeMappings") %>'>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ResearchType.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Expertise" HeaderText="Expertise" ReadOnly="True" SortExpression="Expertise" />
<asp:HyperLinkField DataNavigateUrlFields="Website" DataTextField="Website" HeaderText="Website"
SortExpression="Website" />
<asp:BoundField DataField="Phone" HeaderText="Phone" ReadOnly="True" SortExpression="Phone" />
<asp:TemplateField HeaderText="Email Address" SortExpression="EmailAddress">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("EmailAddress", "mailto:{0}") %>'
Text='<%# Eval("EmailAddress") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Finally, I should mention that the GridView is bound by a Button on the page, but I am not getting updates to the Visible property whether I play with the checkboxes before or after databinding. Furthermore, I have not seen my desired behavior when binding the repeater only on the first Page_Load() using if(!IsPostBack), nor by not using Checkbox.AutoPostback true or false. Any clue as to what I'm doing wrong? I expect it to be something simple, but I'm a bit green here.
As a note: I know how to do this easily with events, but I want to do it with databinding as a learning exercise.
Probably because every time the grid is bound to the data, the column & settings are recreated (with-out your changes).

Resources