ASP.NET - Grid view hyperlink

Asked By k syam on 31-Jan-12 01:31 AM
Hi all,

i have a small issue in form, one grid is there in that one hyperlink is there name is View Attachment user can upload a file after that he has to click the view attachment link button then attachment will be opened.my issue is if not upload a file then in that grid view  that hyperlink button was not displayed how to solve this problem,below is my screen shot.view attachment is in item template below is my aspx code,when we bind the data to the grid if attachment is not availble  at that time the hyper link button was not displayed this is my issue

  <asp:TemplateField HeaderText="View Attachment">
                        <ItemTemplate>
                        
                        <a href='<%#Eval("CertificateAttachment")%>' target="_blank" style="color: Black"  >View
                          Attachment</a>
                        </ItemTemplate>
                        <ItemStyle Width="26%" />
                        <HeaderStyle HorizontalAlign="left" />
                      </asp:TemplateField>

 
Venkat K replied to k syam on 31-Jan-12 01:35 AM
If you assign an empty value to the href then the link for the anchor tag will be disabled, just add a iif condition and verify if there is an attachment the assign the value to href else assign empty value

<a href='<%#Eval("CertificateAttachment")%>' target="_blank" style="color: Black"  >View
              Attachment</a>

Change to:

<a href='<%#Eval("CertificateAttachment")="" ? "" : Eval("CertificateAttachment")%>' target="_blank" style="color: Black"  >View Attachment</a>

Hope this helps!
Jitendra Faye replied to k syam on 31-Jan-12 01:37 AM

For this you have implement RowDataBound Event of GridVIew.

here you have to check that attachment is present of not based  on that you can hide and show Link button.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //Checking whether the Row is Data Row
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
     //Check her for attachment in database for that particular record.

     if(!FlagAttch)   //If not attached
      {
        LinkButton lnk= (e.Row.FindControl("LinkButton1"));
        if(lnk!=null) 
       {
           lnk.Visible =false;
       }
      }  
   }
}

Try like this and let me know.

D Company replied to k syam on 31-Jan-12 01:41 AM
Hello Friend,

Simple way is check whether upload is there or not than make invisible to hyper link.

bool isAttached

if(!iaAttached)
{
//invisible the link
}

Regards
D
k syam replied to Jitendra Faye on 31-Jan-12 02:16 AM
This is not a hyper link,this is Anchor tag in html,how to find control anchor tag in grid???

<a href='<%#Eval("CertificateAttachment")%>' target="_blank" style="color: Black"
                                                  id="Attmnt">View Attachment</a>
kalpana aparnathi replied to k syam on 31-Jan-12 04:01 AM
hi,

try this way:

<asp:HyperLinkField DataNavigateUrlFields="View           Attachment"
DataNavigateUrlFormatString="yourpagename.aspx?id={0}"
  DataTextField="CertificateAttachment" NavigateUrl="yourpagename.aspx" />