DatalistSort

Vipul
Posted by in ASP.NET category on for Intermediate level | Views : 2864 red flag

This article can sort datalist's data alphabetically and also with delete functionality.


 Download source code for DatalistSort

 

 

 

Datalist Sort

I have sort By A,B for sample.You can sort by other alphabets using this code.

I have added Application_sort.aspx and QueryString.aspx

1.Application_sort.aspx

<asp:DataList ID="dl" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None"

BorderWidth="1px" CellPadding="3" CellSpacing="2" GridLines="Both" OnItemCommand="dl_ItemCommand"

Style="z-index: 100; left: 227px; position: absolute; top: 139px">

<HeaderTemplate>

<table>

<tr>

<td style="height: 21px" width="100">

Id

</td>

<td style="height: 21px" width="100">

Title

</td>

<td style="height: 21px" width="100">

Director

</td>

</tr>

</table>

</HeaderTemplate>

<ItemTemplate>

<table>

<tr>

<td width="100">

<%# DataBinder.Eval(Container.DataItem,"Id")%>

</td>

<td width="100">

<asp:HyperLink ID="prod_linkk" runat="server" NavigateUrl='<%# "~/QueryString.aspx?Id=" + Eval("Id") %>'

Text='<%# Eval("Title") %>'></asp:HyperLink>

</td>

<td width="100">

<%# DataBinder.Eval(Container.DataItem,"Director")%>

</td>

<td>

<asp:ImageButton ID="img_but" runat="server" CommandArgument='<%# Eval("Id") %>'

ImageUrl="~/Delete.gif" />

</td>

</tr>

</table>

</ItemTemplate>

<FooterTemplate>

<asp:LinkButton ID="cmd_a" runat="server" OnClick="cmd_a_Click">A</asp:LinkButton>

<asp:LinkButton ID="cmd_b" runat="server" OnClick="cmd_b_Click">B</asp:LinkButton>&nbsp;

<asp:LinkButton ID="cmd_all" runat="server" OnClick="cmd_all_Click">All</asp:LinkButton>

</FooterTemplate>

<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />

<SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />

<ItemStyle BackColor="#FFF7E7" ForeColor="#8C4510" />

<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />

</asp:DataList>

2.Application_sort.aspx.cs

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=d:\datalist_sort\App_Data\MyDatabase.mdf;Integrated Security=True;User Instance=True");

SqlDataAdapter da = new SqlDataAdapter();

DataSet ds = new DataSet();

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

conn.Open();

da = new SqlDataAdapter("select * from movies", conn);

da.Fill(ds, "movies");

dl.DataSource = ds;

dl.DataBind();

conn.Close();

}

}

protected void cmd_a_Click(object sender, EventArgs e)

{

conn.Open();

da = new SqlDataAdapter("select * from movies where title like 'A%'", conn);

da.Fill(ds, "movies");

dl.DataSource = ds;

dl.DataBind();

conn.Close();

}

protected void cmd_b_Click(object sender, EventArgs e)

{

conn.Open();

da = new SqlDataAdapter("select * from movies where title like 'B%'", conn);

da.Fill(ds, "movies");

dl.DataSource = ds;

dl.DataBind();

conn.Close();

}

protected void cmd_all_Click(object sender, EventArgs e)

{

conn.Open();

da = new SqlDataAdapter("select * from movies", conn);

da.Fill(ds, "movies");

dl.DataSource = ds;

dl.DataBind();

conn.Close();

}

protected void dl_ItemCommand(object source, DataListCommandEventArgs e)

{

int no = int.Parse(e.CommandArgument.ToString());

conn.Open();

SqlCommand cmd = new SqlCommand("delete from movies where Id=" + no, conn);

cmd.ExecuteNonQuery();

conn.Close();

conn.Open();

da = new SqlDataAdapter("select * from movies", conn);

da.Fill(ds, "movies");

dl.DataSource = ds;

dl.DataBind();

conn.Close();

}

3.QueryString.aspx

<table style="left: 420px; position: relative; top: 168px" bgcolor="#ccccff">

<tr>

<td colspan="3">

Employee Details

</td>

</tr>

<tr>

<td>

ID

</td>

<td>

:

</td>

<td>

<asp:Label ID="lbl_eno" runat="server" Text="Label"></asp:Label>

</td>

</tr>

<tr>

<td>

Title

</td>

<td>

:

</td>

<td>

<asp:TextBox ID="txt_nm" runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td>

Director

</td>

<td>

:

</td>

<td>

<asp:TextBox ID="txt_add1" runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td style="height: 26px">

</td>

</tr>

</table>

4.QueryString.aspx.cs

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=d:\datalist_sort\App_Data\MyDatabase.mdf;Integrated Security=True;User Instance=True");

protected void Page_Load(object sender, EventArgs e)

{

string num;

num = Request.QueryString["Id"];

con.Open();

SqlCommand cmd = new SqlCommand("select Id,Title,Director from movies where Id=" + int.Parse(num), con);

SqlDataReader dr;

dr = cmd.ExecuteReader();

if (dr.Read())

{

lbl_eno.Text = dr["Id"].ToString();

txt_nm.Text = dr["Title"].ToString();

txt_add1.Text = dr["Director"].ToString();

}

con.Close();

}



 

Page copy protected against web site content infringement by Copyscape

About the Author

Vipul
Full Name: vipul vk
Member Level: Starter
Member Status: Member
Member Since: 4/15/2009 9:08:15 PM
Country:

http://www.dotnetfunda.com
Working as a software developer

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)