How can I navigate to a different URL when user clicks the hyperlink in the GridView with a condition? - asp.net

I am a beginner in learning asp.net. I have a column in a GridView with header name FORM ID. I want to be able to navigate to the different URL based on the part of the FORM ID.
For example,
Clicking on abc10001 will take us to "~/abc1.aspx?formid=abc10001"
Clicking on abc20001 will take us to "~/abc2.aspx?formid=abc20001"
I understand the use of the MID function like so v=MID(string,4,1) to capture the 4th value and redirect to page by determining the value v but I do not know how to apply this correctly. Please guide me. Your help is greatly appreciated.
The following is the aspx code I'm currently work on :
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false">
<columns>
<asp:Hyperlinkfield DataTextField="formid" HeaderText="Form ID" ItemStyle- Width="150px"
DataNavigateUrlFields="formid" DataNavigateUrlFormatString="~/abc1.aspx" />
</Columns>
</asp:GridView>

You could switch your Hyperlinkfield to a TemplateField with a HyperLink control to give you more control over the NavigateUrl like so:
<asp:TemplateField HeaderText="Form ID">
<ItemTemplate>
<asp:HyperLink runat="server" Text='<%# Eval("formid") %>'
NavigateUrl='<%# "~/abc" + Mid(Eval("formid"), 4, 1) + ".aspx" %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

This is my final code which is working successfully :
<asp:GridView ID="child" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="Form ID" >
<ItemTemplate>
<asp:Hyperlink runat="server" Text='<%# Eval("formid") %>'
NavigateUrl='<%# Eval("formid","~/abc" + Mid(Eval("formid"), 4, 1) + ".aspx?formid={0}") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Credits to #mason and #Tony L. for helping me out on this issue.

Related

How to add if statement logic in ASP.Net GridView Column

I have a GridView with the following column:
<asp:HyperLinkField DataNavigateUrlFields="agent_level" DataNavigateUrlFormatString="agent_production.aspx?agentlevel={0}" DataTextField="agent_id" HeaderText="View Agents" DataTextFormatString="View" Text="View"/>
I have been looking for a way to do the following: if the agent_level = 'b' (this value is from the database) then the "View" is not clickable.
You can use TemplateField for small business logic. If you want complex business logic, you want to consider using OnRowDataBound event.
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="View Agents">
<ItemTemplate>
<asp:HyperLink
NavigateUrl='<%# Eval("agent_level").ToString() == "b"
? "javascript:void(0)"
: "agent_production.aspx?agentlevel=" + Eval("agent_id") %>'
runat="server"
ID="AgentHyperlink" Text="View" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

passing a querystring from one of the griditems bound columns

I have a gridview, one of the item in the gridview is
<asp:GridBoundColumn DataField="Id" UniqueName="Id" DataType="System.Int32" Visible="false"></asp:GridBoundColumn>
and another item is
<asp:ImageButton id="RadButton_RunQuery" ImageUrl="~/images/run_query_button.jpg" PostBackUrl="~/Viewer/ViewerSummary.aspx?QueryID=" runat="server" />
want to pass Id from the asp:GridBoundColumn as a querystring to the postbackurl of the asp:imageButton.
How can I achieve this?
You can try with this code
PostBackUrl='<%# "~/Viewer/ViewerSummary.aspx?QueryID=" + DataBinder.Eval(Container.DataItem,"ID") %>'
Since you are using an ImageButton then that means you are using an ItemTemplate. You can do something like this:
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton id="RadButton_RunQuery" ImageUrl="~/images/run_query_button.jpg"
PostBackUrl='<%#string.Format("~/Viewer/ViewerSummary.aspx?QueryID={0}",Eval("Id")) %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>

Multiple DataNavigateUrlFields... one from datasource, one from drpdownlist

Lets say I have a drop down list and a grid view on a page like this
<asp:GridView ID="gvCategories" runat="server" >
<Columns>
<asp:HyperLinkField DataTextField="CategoryName" DataNavigateUrlFields="CategoryID" DataNavigateUrlFormatString="~/Learning.aspx?categoryID={0" />
</Columns>
</asp:GridView>
I'd like to add this to the URL:
&view=<%=SelectedDropdownlistvalue%>
How can I do this?
I would use TemplateField as:
<asp:GridView ID="gvCategories" runat="server" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("CategoryName", "~/Learning.aspx?categoryID={0}")+" &view=" + DropDownList1.SelectedValue %>'
Text="Goto Page"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Remember the DropDownList.SelectedValue is obtained at postBack, so the link is constructed at that time. To change the link dynamically when the value of the dropdownlist changes set the AutoPostBack="true" of the dropdownlist.

loading gridview with hyperlink column

I have a gridview and added a column "Hyperlink" to all records by enabling autogeneratefields.
When this gridview is loaded and when I click the hyperlink across any record I want to redirect to some other page with entire record passed as query string to that page?
can anybody help me on this?
These links should clarify how to do it:
How to pass variables thru a DataGrid hyperlink column
How To: Use a HyperLink control inside a GridView
Sample code (Look at the NavigateUrl property of HyperLink):
<asp:GridView ID="urlGrid" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1"
runat="server"
NavigateUrl='<%# "RedirectPage.aspx?xxxx=" &
DataBinder.Eval(Container, "DataItem.xxxx") &
"&yyyy=" & DataBinder.Eval(Container, "DataItem.yyyy")%>'
Text="Go!">
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SiteName" HeaderText="Site Name" />
</Columns>
</asp:GridView>

Gridview : Hyperlink and description in the same column cell

Apologies for the newbie question. My client wishes me to make a small change to the gridview on his http://www.flogitdonegal.com/SearchPage.aspx page.
Note the way the Title column is a hyperlink to view more information. This comes from a 'BriefDescription' field in the database.
How can I add 250 chars from the 'FullDescription' underneath the Title in the same cell, but I dont want it be a hyperlink.
Essentially it will be 2 fields coming into the same column.
Thanks in advance for all help.
John
If this is using a GridView you are most likely using a TemplateField as it is to display the HyperLink.
Within the ItemTemplate of the TemplateField you can specify an additional Label underneath using something as follows:
<asp:Label runat="server" id="FullDescLabel" Text='<%# DataBinder.Eval(Container.DataItem, "FullDescription") %>' />
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="gvwCompounds" runat="server" DataSourceID="objItemsFromYourDB">
<Columns>
....
<asp:TemplateField>
<ItemTemplate HeaderText="Title">
<asp:HyperLink runat="server" ID="Hperlink1" NavigateUrl='<%# Eval("BriefDescriptionUrl") %>' Text='<%# Eval("BriefDescription") %>' />
<br />
<asp:Label runat="server" ID="Label1" Text='<%# Eval("FullDescription") %>' />
</ItemTemplate>
</asp:TemplateField>
....
</Columns>
</asp:GridView>

Resources