How to bind Datalist control from code behind ?


Wednesday, May 26, 2010

Solution :

This blog will explain you, how you can bind Datalist control from code behind.
First you need to add Datalist control in .aspx file, you can do this by draging the control form the toolbox or you can past e the below code in .aspx file.

Then you need to bind this control from the database, you can do this by assigning Datasource of control to dataset, find code for this also below.


Place below code in HTML file.

<asp:DataList ID="dlPhotos" runat="server" RepeatColumns="3" CellSpacing="0" CellPadding="5"
            RepeatDirection="Horizontal" Width="100%">
            <ItemStyle VerticalAlign="Top" />
            <ItemTemplate>
                <table cellspacing="0" cellpadding="0" width="100%" border="0" align="center">
                    <tr>
                        <td align="center" class="cellheightfifteen">
                            <a id="lnk<%#Container.DataItem("iProductImageID")%>" href="javascript:void(0)" 

onclick="removePhoto('<%=Application("SiteURL")%>ajaxopes.aspx','<%#Container.DataItem("iProductImageID")%>','<%#container.da

taitem("sImageFileName")%>')">
                                Remove Photo </a><span class="redlink" id="spn<%#Container.DataItem("iProductImageID")%>"
                                    style="display: none;"></span>
                        </td>
                    </tr>
                    <tr>
                        <td class="heightfive">
                        </td>
                    </tr>
                    <tr>
                        <td align="center">
                            <table id="tbl<%#Container.DataItem("iProductImageID")%>" 

width="<%=Application("THUMBIMAGEWIDTH") + 10%>"
                                height="<%=Application("THUMBIMAGEHEIGHT") + 10%>" border="0" cellspacing="0"
                                cellpadding="0" class="tblbox1">
                                <tr>
                                    <td align="center" id="tdImage" runat="server">
                                        <img 

src='<%=Application("SiteURL")%>Images/ThumbleImages/<%#container.dataitem("sImageFileName")%>?<%=now.tostring%>'
                                            alt='Loading...' />
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td align="center" class="cellheightfifteen">
                            <input type="radio" id="opt<%#Container.DataItem("iProductImageID")%>" name="rdPhoto"
                                onclick="setPhotoId('<%#Container.DataItem("iProductImageID")%>')" />
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>



Place below code in Code behind.

Code in VB.Net

If Not dsImage Is Nothing Then
       If dsImage.Tables(0).Rows.Count > 0 Then
          dlPhotos.DataSource = dsImage
          dlPhotos.DataBind()
       End If
End If

OR

Code in C#.Net

if ((dsImage != null)) 
{
        if (dsImage.Tables(0).Rows.Count > 0) 
    {
            dlPhotos.DataSource = dsImage;
            dlPhotos.DataBind();
        }
}


Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Indianic Infotech Ltd (India)
rajesh@indianic.com


No comments :