Frage

I had DataList and I added in header to bind specific value , I did my code well but when I tried to click on header <a href > it doesn't redirect to the URL .

 <asp:DataList ID="DL_ElarabyNews" runat="server" RepeatColumns="0">
                    <HeaderTemplate>
                        <div class="araby-news-title">
                            <a href='<%#Eval("ArtType_ID","NewsPage.aspx?ArtTypeID="+ Eval("ArtType_ID"))%>'>
                                </a>
                        </div>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <div class="all-ads-conatiner">
                            <div class="single-ads-conainer">
                                <div class="ads-img">
                                    <asp:Image ID="Img_Elaraby" alt="Lastweek" runat="server" ImageUrl='<%# Eval("Art_Img")%>'
                                        Width="47" Height="34" />
                                </div>
                                <div class="ads-text">
                                    <a href='<%#Eval("Art_ID","NewsDetailsPage.aspx?ArtID="+ Eval("Art_ID"))%>'>
                                        <%# Eval("Art_Title")%>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </ItemTemplate>
                </asp:DataList>
War es hilfreich?

Lösung

Looks like your datasource is filtered by ArtType_ID and you want to have a link in the DataList header to pass the ID to NewsPage.aspx.

The problem is, Header doesn't repeat with item.So you can't bind <a> with ArtType_ID. But you can have a easy workaround.

In the code add a property named ArtType, and a Method named GetArtType and where you are binding the datasource to the DataList, get the ArtType_ID, save in ArtType:

public string ArtType { get; set; }

public string GetArtType()
{
    return ArtType;
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //datasource is the name of your datasource 
        ArtType = datasource.First().ArtType_ID.ToString();
        DL_ElarabyNews.DataSource = datasource;
        DL_ElarabyNews.DataBind();
    }
}

Now in the markup you can access the GetArtType, your HeaderTemplate should look like:

<HeaderTemplate>
    <div class="araby-news-title">
        <a href='NewsPage.aspx?ArtTypeID=<%# GetArtType()%>'>Test</a>
    </div>
</HeaderTemplate>

Hope it helps!

Andere Tipps

What you are doing doesn't really make sense.

If there are 10 items in the datalist, which one should the header display?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top