locked
Hyperlink is not generated in gridview for a particular value containing “:” in asp .net gridview RRS feed

  • Question

  • User-376040721 posted

    I am trying to bind a gridview value with a dataset and one of the field in that is hyperlink which is redirecting to other page.But for a particular value containing ":" whose hyperlink is disable.i have written sever side code and client code for binding as follows

    <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" Font-Names="Arial" AutoGenerateSelectButton="True"
    Font-Size="10pt" RowStyle-BackColor="White" OnSelectedIndexChanged="gvCustomers_SelectedIndexChanged" OnRowDataBound="gvCustomers_RowDataBound"
    Cssclass="gvv table table-striped table-bordered">
    <HeaderStyle CssClass="bg-clouds" BackColor ="#9c27b0" ForeColor ="white"/>
    <Columns>
    <asp:BoundField ItemStyle-Width="150px" DataField="A_NO" HeaderText="A_NO" SortExpression="A_NO" />
    <asp:BoundField ItemStyle-Width="150px" DataField="B_RE" HeaderText="B_RE" SortExpression="B_RE" />
    <asp:BoundField ItemStyle-Width="150px" DataField="C_NAME" HeaderText="C_NAME" SortExpression="C_NAME" />
    <asp:HyperLinkField DataTextField="A_NO" HeaderText="PDF" DataNavigateUrlFields="A_NO,B_RE,C_NAME," DataNavigateUrlFormatString="~/ABC.aspx?A_NO={0}&B_RE={1}&C_NAME={2}" target="_blank"/>
    </asp:GridView>

    C#

    void getData()
    {
    string qry = "";
    qry = "SELECT TT_UP ||TT_ER||TT_NO as A_NO,B_RE,C_NAME";
    dt = conn.GetData(qry);
    gvCustomers.DataSource = dt;
    gvCustomers.DataBind();
    }

    Expected:Header Text "PDF" should be a hyperlink for all type of values return in A_NO field. Actual: if value in A_NO is like "LRAS:11" then hyperlink is disable. Any idea would be appreciated.

    Thursday, July 11, 2019 10:31 AM

All replies

  • User1564875471 posted

    Since you are using special characters part of the query string, You need to encode the values first. To do this, you should replace your HyperLinkField with TemplateField and use the HyperLink control inside the template field:

    <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink runat="server" Text='<%# Eval("A_NO") %>'
                            NavigateUrl='<%# string.Format("~/ABC.aspx?A_NO={0}&B_RE={1}&C_NAME={2}", HttpUtility.UrlEncode(Eval("A_NO").ToString()), HttpUtility.UrlEncode(Eval("B_RE").ToString()), HttpUtility.UrlEncode(Eval("C_NAME").ToString())) %>'></asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>

    Thursday, July 11, 2019 8:07 PM
  • User288213138 posted

    Hi Mohansarabi,

    According to your description, I couldn’t understand your requirement clearly.

    Do you mean you want to disable hyperlink when it contain ":"?

    If this is your requirement, I suggest you could try to set it as below codes:

    <asp:GridView ID="GridView1" runat="server">
                    <Columns>                  
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>                   
                    </Columns>
                </asp:GridView>
    
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    LinkButton lb1 = (LinkButton)e.Row.FindControl("LinkButton1");
    
                    if (value.IndexOf(':') >= 0)
                    {
                        lb1.Enabled= false;
    
                    }
                    else
                    {
                        //Do something
                    }
                }
            }

    If I misunderstand your requirement, please post more details information about your requirement.

    Best regards,

    Sam

    Wednesday, July 31, 2019 11:01 AM