Wednesday, 22 October 2014

How To Set Fixed Rows in DataList Control in .NET ASP C#

--> In data list control we can set Repeated columns but we can't set Rows directly Because there is no attribute for that operation.
--> so, to set that fixed rows we have to right the logic.
--> To achieve this requirement we have to update columns size based up on the number of entries or count.
Here I want to take  List from generic collection to set the number of entries as follows

List<int> myList = new List<int>();
for (int i = 1; i <38; i++)
{
myList.Add(i);
}


List<int> myList = new List<int>();
for (int i = 1; i <38; i++)
{
myList.Add(i);
}
     
Now, the above myList consist of 38 elements, if you want to give entries dynamically, take the values from text box.   


List<int> myList = new List<int>();
string entries = textBox1.text;
for (int i = 1; i <convert.Toint32(entries); i++)
{
myList.Add(i);
}


List<int> myList = new List<int>();
string entries = textBox1.text;
for (int i = 1; i <convert.Toint32(entries); i++)
{
myList.Add(i);
}

Now, i want to fix the Rows Size 3, for that we have to use the following logic
                                                                                                                               
         for(j=1;j<myList.Count;j++)                      //count of entries
            {
                if (myList.Count == 3*j)            //if it is multiple by three we can fix the multiple
                {                                                         by three valued columns dynamically
                    columns = j;
                    ViewState["datalist_columns"] = columns;
                }
            }
            if (myList.Count >3*columns)        // if the count is not multiple by three then
            {                                                                we can check the near multiple by three
                columns = myList.Count / 3;             and increase one column because count is 
                columns = columns + 1;                       exceed .
                ViewState["datalist_columns"] = columns;
                      }
            myDataList.RepeatColumns =Convert.ToInt32(ViewState["datalist_columns"]);
            myDataList.DataSource = myList;

CODE In Detail :

.ascx :


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="datalist.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

    <asp:DataList ID="myDataList" runat="server"
          
          RepeatDirection ="Vertical"     
            CellPadding="10"
            ForeColor="#333333"
            RepeatLayout="Table"
            ShowFooter="False"
            ShowHeader="False"
         GridLines="Both">
            <AlternatingItemStyle BackColor="White" />
            <ItemStyle BackColor="#EFF3FB" />
            <SelectedItemStyle BackColor="#D1DDF1"
                Font-Bold="True" ForeColor="#333333" />
            <ItemTemplate>
                <asp:Label ID="myLabel" runat="server"
                    Text="<%# Container.DataItem.ToString() %>" />
            </ItemTemplate>
        </asp:DataList>
    </div>
    </form>
</body>
</html>

.CS CODE:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace datalist
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        int columns;
        int j;
        protected void Page_Load(object sender, EventArgs e)
        { 
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            List<int> myList = new List<int>();
            string photos =TextBox1.Text;
            for (int i = 0; i < Convert.ToInt32(photos); i++)
            {
                myList.Add(i);
            }
            for (j = 1; j < myList.Count; j++)
            {
                if (myList.Count == 3 * j)
                {
                    columns = j;
                    ViewState["datalist_columns"] = columns;
                }
            }
            if (myList.Count > 3 * columns)
            {
                columns = myList.Count / 3;
                columns = columns + 1;
                ViewState["datalist_columns"] = columns;
            }
            myDataList.RepeatColumns = Convert.ToInt32(ViewState["datalist_columns"]);
            myDataList.DataSource = myList;
            myDataList.DataBind();
        }
    }
}

Screen Shots :

















0 comments:

Post a Comment