Search code examples
c#asp.netgridviewhyperlink

ASP.NET Gridview hyperlink with 2 parameters not working


I have a asp.net gridview in which I want VIEW option. Based on SAPID and CandidateID it should redirect to that respective page. But while passing that 2 parameters, I am unable to redirect with querystring. Please help.

Below is the code.

<asp:GridView ID="grdVSATDetail" runat="server" AutoGenerateColumns="false" CssClass="dashboard" Width="100%" ShowHeaderWhenEmpty="true">
                        <Columns>
                            <asp:BoundField DataField="STATE" HeaderText="R4G State" ItemStyle-Width="9%" />
                            <asp:BoundField DataField="SAP_ID" HeaderText="SAP-ID" ItemStyle-Width="10%" />
                            <asp:BoundField DataField="CANDIDATE_ID" HeaderText="Candidate Id" ItemStyle-Width="9%" />
                            <asp:BoundField DataField="SITE_NAME" HeaderText="Site Name" ItemStyle-Width="15%" />
                            <asp:BoundField DataField="STATUS_NAME" HeaderText="Status" ItemStyle-Width="9%" />
                            <asp:BoundField DataField="SITE_TYPE" HeaderText="Site Type" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" />
                            <asp:BoundField DataField="CANDIDATESTATUS" HeaderText="Candidate Status" ItemStyle-Width="8%" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" />
                            <asp:BoundField DataField="SITEID" HeaderText="Site Id" ItemStyle-Width="8%" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" />
                            <asp:BoundField DataField="PRIORITYSITE" HeaderText="Priority Site" ItemStyle-Width="9%" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" />
                            <asp:BoundField DataField="LATITUDE" HeaderText="Latitude" ItemStyle-Width="8%" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" />
                            <asp:BoundField DataField="LONGITUDE" HeaderText="Longitude" ItemStyle-Width="8%" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" />
                            <asp:TemplateField HeaderText="Edit" ItemStyle-Width="6%">
                                <ItemTemplate>
                                    <asp:ImageButton ID="btnView" OnClick="btnView_Click" Width="15" runat="server" ToolTip="Edit Work Item" ImageUrl="~/Images/pencil.png" Enabled='<%# Eval("STATUS_NAME").ToString()=="VSAT form filled" || Eval("STATUS_NAME").ToString()=="Pending for Approval"?false:true %>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="View" ItemStyle-Width="5%">
                                <ItemTemplate>
                                    <asp:HyperLink ID="btnDisplay" Width="45" Text="View" runat="server" ToolTip="View VSAT Info" NavigateUrl="~/Sitesurvey.aspx?SapId={0}&CandidateId={1}"></asp:HyperLink>                                    
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>No records found !</EmptyDataTemplate>
                    </asp:GridView>

update

Getting error as

The input is not a valid Base-64 string as it contains a non-base 64 character


Solution

  • There are several approaches that you can use.

    You can of course have a standard button click event, and then with server side code create the URL based on that row, and navigate that way.

    So, in place of a hyper link, just use a standard button, and a standard button click event.

    Hence this:

        protected void cmdView_Click(object sender, EventArgs e)
        {
            Button cmdView = (Button)sender;
            GridViewRow gRow = (GridViewRow)cmdView.NamingContainer;
    
            string sURL =
                $@"~/ViewProjects/Project?SAPIP={gRow.Cells[1].Text}&CandidateID={gRow.Cells[2].Text}";
    
            Response.Redirect(sURL);
    
        }
    

    As noted, you can probably form a hyper-link in the markup directly without a button click, and hence say this also should work:

    <asp:TemplateField>
        <ItemTemplate>
            <a href="./ViewProjects/Project?SAPIP=<%#Eval("SAP_ID")%>&CandidateID=<%# Eval("Candidate") %>" />
        </ItemTemplate>
    </asp:TemplateField>