locked
Hyperlink inside datalist RRS feed

  • Question

  • User-169979396 posted

    Hello,

    I have this code

    <asp:DataList ID="ContentDataList" runat="server" DataKeyField="Id" 
                DataSourceID="ContentSqlDataSource">
                <ItemTemplate>
                    <asp:HyperLink ID="DownloadLink" NavigateUrl='<%# Eval("DownloadUrl") %>' runat="server" Visible="False">Download</asp:HyperLink>
        </ItemTemplate>
            </asp:DataList>
    so how i can change the hyperlink DownloadLink from code behind using c#

    Wednesday, November 2, 2011 5:33 AM

Answers

  • User3866881 posted

    Hello msomar:)

    I suspect that your Request.QueryString["MainType"] doesn't match "News". So you can ignore the lower or upper case, just have a try like this:

    if (Request.QueryString["MainType"].ToLower()=="News".ToLower())
    {
       //Do what you want here……
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 3, 2011 9:23 PM

All replies

  • User-2030397957 posted

    Hello

    Find the control inside Datalist just like below code

    foreach(DataListItem dli in DataList1.Items)
    {
    hyperlink lnk1= (hyperlink ) dli.FindControl(“DownloadLink″);
    //…
    }

    and set the property what ever you want...

    Wednesday, November 2, 2011 5:42 AM
  • User-68639941 posted
    debug and check and also ensure you doing this process after calling dataList.DataBind()
    nothing wrong in your code
    ensure you are getting value in querystring 
    
    Request.QueryString["MainType"])  and try bu using below if statement
    	if(Request.QueryString["MainType"].Equals("News", StringComparison.CurrentCultureIgnoreCase))
    			{....
    			}
    
    if (Convert.ToString(Request.QueryString["MainType"]) == (string)"News") 
            { 
              
                foreach (DataListItem dli in ContentDataList.Items) 
                { 
                    HyperLink lnk1 = (HyperLink)dli.FindControl("DownloadUrl"); 
                lnk1.Visible = true; 
                     
                } 
     
            }
    to change like in code-behind you can di like below
    
    	protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    	{
    		if (e.Item.ItemType == ListItemType.Item)
    		{
    			HyperLink hp = e.Item.FindControl("DownloadLink") as HyperLink;
    hp.NavigateUrl = "Test.aspx";
    		}
    }
    Wednesday, November 2, 2011 5:43 AM
  • User-169979396 posted

    PLEASE take a look of my code behind

    if (Convert.ToString(Request.QueryString["MainType"]) == (string)"News")
            {
             
                foreach (DataListItem dli in ContentDataList.Items)
                {
                    HyperLink lnk1 = (HyperLink)dli.FindControl("DownloadUrl");
                lnk1.Visible = true;
                    
                }
    
            }
    but nothing happen
    is my code wrong?
    Wednesday, November 2, 2011 6:19 AM
  • User3866881 posted

    Hello msomar:)

    I suspect that your Request.QueryString["MainType"] doesn't match "News". So you can ignore the lower or upper case, just have a try like this:

    if (Request.QueryString["MainType"].ToLower()=="News".ToLower())
    {
       //Do what you want here……
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 3, 2011 9:23 PM