Ajax control toolkit is providing us a great control called ReoderList with lot of features. But there is not a direct method to deal with the database records. Anyway today I am bringing you a solution for this. This will view details from the database table in the reorderlist with both images and text. Not only that. It will update the new order to the database field once you change the order. I have tested this and this is my working code. You should change the commands in the SqlDataSource and change the field names in .cs file to your table field names.
You'd better keep remember this too for the best result. This will require the new release of the Ajax control toolkit and new framework of .net like 3.5.Because it is using Linq in .cs code. I found another issue and I have written to Microsoft people about this. Reorderlist control is not working in their IE 8 old version. I have mentioned this in my previous post. You can try with install new version of that browser and test it too.
.aspx
.cs
.css
SQL
Welcome for your comments and for your questions.
You'd better keep remember this too for the best result. This will require the new release of the Ajax control toolkit and new framework of .net like 3.5.Because it is using Linq in .cs code. I found another issue and I have written to Microsoft people about this. Reorderlist control is not working in their IE 8 old version. I have mentioned this in my previous post. You can try with install new version of that browser and test it too.
.aspx
<asp:UpdatePanel ID="Up1" runat="server">
<ContentTemplate>
<div class="reorderListDemo">
<ajaxtoolkit:ReorderList ID="ReorderList1" runat="server"
AllowReorder="True"
PostBackOnReorder="true"
DataSourceID="SqlDataSource1"
CallbackCssStyle="callbackStyle"
DragHandleAlignment="Right"
ItemInsertLocation="End"
DataKeyField="id"
EnableViewState="true"
SortOrderField="oderId"
Width="500px"
>
<ItemTemplate>
<div style="width:300px; display:inline; vertical-align:top;">
<div style="width:50px;float:left; vertical-align:middle; padding-top:40px; "> <asp:Label ID="Label2" runat="server" Text='<%# HttpUtility.HtmlEncode(Convert.ToString(Eval("oderId"))) %>' /> </div> <%--<%# HttpUtility.HtmlEncode(Convert.ToString(Convert.ToInt32(Eval("oderId"))+1))%>--%>
<div style="width:150px;float:left;"> <asp:Image Width="100px" ID="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "image_name", "~/DesktopModules/Ricette/imgUpload/tmp/tbn{0}")%>' /> </div>
<div style="width:70px;float:left;"> <asp:ImageButton CommandName="Delete" ImageUrl="imgUpload/basket.png" ID="ImageButton1" runat="server" Height="70px" Width="56px" /> </div>
</div>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("image_name") %>' />
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("id") %>' />
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("title_id") %>' />
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("oderId") %>' />
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("published") %>' />
</EditItemTemplate>
<ReorderTemplate>
<asp:Panel ID="Panel1" runat="server" CssClass="reorderCue" />
</ReorderTemplate>
<DragHandleTemplate>
<div style="height:99px; width:100px; cursor:move; float:left;">
<asp:Image runat="server" ImageUrl="~/DesktopModules/Ricette/imgUpload/hand.gif" />
</div>
</DragHandleTemplate>
<InsertItemTemplate>
<div style="padding-left:5px; float:none; border-bottom:thin solid transparent;">
<asp:Panel ID="Panel2" runat="server" DefaultButton="Button1" Width="650px" >
<asp:Button ID="Button1" runat="server" CommandName="Insert" Text="Refresh and Publish" />
</asp:Panel>
</div>
</InsertItemTemplate>
</ajaxtoolkit:ReorderList>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SiteSqlServer %>"
DeleteCommand="DELETE FROM [Ricette_Images] WHERE [id] = @id"
InsertCommand="INSERT INTO [Ricette_Images] ([title_id], [image_name], [oderId], [published]) VALUES (@title_id, @image_name, @oderId, @published) DELETE FROM [Ricette_Images] WHERE [image_name] = 'noname'"
SelectCommand="SELECT [id], [title_id], [image_name], [oderId], [published] FROM [Ricette_Images] WHERE [title_id] = @title_id order by oderId asc"
UpdateCommand="UPDATE [Ricette_Images] SET [title_id] = @title_id, [image_name] = @image_name, [oderId] = @oderId, [published] = @published WHERE [id] = @id">
<SelectParameters>
<asp:Parameter Name="title_id" Type="String" DefaultValue="<%= title_id %>" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="title_id" Type="Int32" />
<asp:Parameter Name="image_name" Type="String" />
<asp:Parameter Name="oderId" Type="Int32" />
<asp:Parameter Name="published" Type="Boolean" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="title_id" Type="Int32" DefaultValue="111" />
<asp:Parameter Name="image_name" Type="String" DefaultValue="noname" />
<asp:Parameter Name="oderId" Type="Int32" DefaultValue="11" />
<asp:Parameter Name="published" Type="Boolean" DefaultValue="false"/>
</InsertParameters>
</asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
.cs
protected void ReorderList1_ItemReorder(object sender, AjaxControlToolkit.ReorderListItemReorderEventArgs e)
{
//get Old Priority
int oldI = e.OldIndex;
//get New Priority
int newI = e.NewIndex;
//get the selected Item Value
string selectedDK = ReorderList1.DataKeys[oldI].ToString();
string i2updateList = "";
//Build update list based on everything after/equal to the new priority but not equal to the selected one and in order so they can be updated in order
SqlConnection oConn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Get("SiteSqlServer"));
{
SqlCommand sqlCmd = new SqlCommand("select id from Ricette_Images where id <> '" + selectedDK + "' and oderId >= '" + newI + "' order by oderId asc", oConn);
oConn.Open();
using (SqlDataReader reader = sqlCmd.ExecuteReader())
{
while (reader.Read())
{
if (string.IsNullOrEmpty(i2updateList))
{
i2updateList = reader["id"].ToString();
}
else
{
i2updateList += "," + reader["id"].ToString();
}
}
}
}
int newPriority = newI;
string[] rolArr = null;
rolArr = i2updateList.Split(',');
//update each from the list by 1
foreach (var x in rolArr)
{
newPriority = newPriority + 1;
SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Get("SiteSqlServer"));
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand("update Ricette_Images set oderId = '" + newPriority + "' where id='" + x + "'", myConnection);
myCommand.ExecuteNonQuery();
}
newPriority = newPriority;
}
//finally, update the selected one for its changed priority
SqlConnection myConne = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Get("SiteSqlServer"));
{
myConne.Open();
SqlCommand myCommand = new SqlCommand("update Ricette_Images set oderId = '" + newI.ToString() + "' where id='" + selectedDK + "'", myConne);
myCommand.ExecuteNonQuery();
}
}
void ReorderList1_ItemCommand(object sender, AjaxControlToolkit.ReorderListCommandEventArgs e, AjaxControlToolkit.ReorderListItemReorderEventArgs ee)
{
int oldI = ee.OldIndex;//get Old Priority
string selectedDK = ReorderList1.DataKeys[oldI].ToString();//get the selected Item Value
// If you want support for multiple commands
SqlConnection myConnect = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Get("SiteSqlServer"));
myConnect.Open();
if (e.CommandName == "Delete")
//return;
{
//DELETE FROM [Ricette_Images] WHERE [id] = @id
SqlCommand myCommand = new SqlCommand("DELETE FROM Ricette_Images where id='" + selectedDK + "'", myConnect);
myCommand.ExecuteNonQuery();
}
if (e.CommandName == "Select")
//return;
{
int titleid = Convert.ToInt32(HttpContext.Current.Session["RecordID"]);
//SqlDataSource1.SelectParameters.Add("userNum", userNum);
SqlDataSource1.SelectParameters.Add("@title_id", titleid.ToString());
//SELECT [id], [title_id], [image_name], [oderId], [published] FROM [Ricette_Images] WHERE [title_id] = title_id order by oderId asc"
SqlCommand sqlCmd = new SqlCommand("select [id], [title_id], [image_name], [oderId], [published] from Ricette_Images where title_id ='" + titleid + "' order by oderId DESC", myConnect);
sqlCmd.ExecuteNonQuery();
}
if (e.CommandName == "Insert")
//return;
{
string imgNullName = "t";
SqlCommand myCommandCheckNull = new SqlCommand("DELETE FROM Ricette_Images where image_name='" + imgNullName + "'", myConnect);
myCommandCheckNull.ExecuteNonQuery();
}
if (e.CommandName == "Refresh")
//return;
{
int Currenttitleid = Convert.ToInt32(HttpContext.Current.Session["RecordID"]);
bool Newpublished = true;
SqlConnection myConnect1 = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Get("SiteSqlServer"));
{
myConnect1.Open();
SqlCommand myCommand = new SqlCommand("update Ricette_Images set published = '" + Newpublished + "' where title_id='" + Currenttitleid + "'", myConnect1);
myCommand.ExecuteNonQuery();
}
}
}
.css
SQL
CREATE TABLE [dbo].[Ricette_Images](
[id] [int] IDENTITY(1,1) NOT NULL,
[title_id] [int] NULL,
[image_name] [varchar](500) NULL CONSTRAINT [DF_Ricette_Images_image_name] DEFAULT ('img'),
[oderId] [int] NULL,
[published] [bit] NULL CONSTRAINT [DF_Ricette_Images_published] DEFAULT ((0)),
CONSTRAINT [PK_Ricette_Images] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
Welcome for your comments and for your questions.
Saturday, November 07, 2009 4:09:00 AM
Is there a place where we can see all this code work together?
I'm having a problem in IE8. In earlier versions when I dragged an item I would see a ghost item follow the mouse. Now I just have that white box that switches places with other items.
Saturday, November 07, 2009 8:34:00 AM
What Do you mean by Ghost Item?Can't you drag your items on IE 8 with this codes?can you mention your issue in detail.
Thursday, December 10, 2009 10:35:00 AM
The ReorderList does NOT work with IE 8 unless you run in compatibility mode.
Saturday, September 15, 2012 6:48:00 AM
This is really interesting, You are a very skilled blogger.
I've joined your rss feed and look forward to seeking more of your fantastic post. Also, I have shared your website in my social networks!
My web site : Quotes About Beauty
Tuesday, September 18, 2012 1:57:00 AM
I bеliеve thіs is among thе most significant
information for me. Αnd i am satisfieԁ reading yοur artіcle.
But want tο commentary on sοme normal thіngs, The
site style is peгfect, thе аrtiсles is actually greаt : D.
Good actіvitу, cheers
My webpage :: E Cigs v2
Saturday, September 22, 2012 11:53:00 AM
I leaνe a response ωhen ӏ like a post on a site or if
І have ѕomething to valuablе to contribute to the conversatiοn.
Іt is tгiggereԁ by the fire communicated in the artіcle Ι browsеd.
Аnd аfter this article "Ajax Reorder list with database-Issues & solutions".
I ωаѕ moved enough to drop a thought :) I actually ԁο havе a сouple of
questions fоr you if it's allright. Could it be just me or do a few of these remarks appear like left by brain dead folks? :-P And, if you are writing at other places, I'd likе to fοllow you.
Woulԁ уou liѕt thе complete uгls of all youг shaгed pagеs like your linkеdin
ρrofile, Faceboоk page or twittег feеd?
my site :: trilastin coupons
Saturday, September 22, 2012 12:16:00 PM
You can find me from here. I am writing here now.
www.prasadmaduranga.com/Blog.aspx
Monday, September 24, 2012 4:20:00 PM
Ӏ гeaԁ this ρarаgrаph comрlеtеlу on the toріc
οf the comparison of most uр-to-ԁate and
pгесedіng technоlοgiеs, it's remarkable article.
Also see my page - types of engineering
Thursday, September 27, 2012 6:29:00 AM
I sеriοuslу lοve your blog.
. Εxcellent colorѕ & theme. Did you creatе thiѕ amazing site
yourself? Plеаse гeplу baсκ aѕ ӏ'm looking to create my own site and want to learn where you got this from or exactly what the theme is called. Thank you!
My web site ; Small Cap Stocks
Thursday, September 27, 2012 7:05:00 AM
After exploring а feω οf the articles on yοur
sitе, I really aрpreciate your teсhnіque of writing a blоg.
І book-marked it tο my boοkmаrk webρаge
list and wіll be cheсking back ѕoon.
Рlease visit mу ωeb ѕіte as well and let me knоω how
you feel.
Feel free to visit my webpage - cool graphic designs
Friday, September 28, 2012 6:43:00 AM
Wow! At last I got a blog from where I be capable
of truly get valuable facts concerning my study and knowledge.
Also visit my page ... disney malvorlagen
Thursday, October 04, 2012 5:11:00 PM
It wouild be nice if you could actually read the code without all the garbage on the page.
Tuesday, October 23, 2012 5:27:00 PM
Admiring the commitment you put into your blog and detailed
information you provide. It's awesome to come across a blog every once in a while that isn't the same old rehashed information.
Excellent read! I've saved your site and I'm including your RSS feeds to my Google account.
my site: nathan Gold
Thursday, October 25, 2012 10:52:00 AM
Heу there, I think yοur sitе might be hаѵіng bгοwser
compаtіbіlity issues. When I loοk at yοur blοg site in Chrome, іt loоks finе but ωhеn opening
in Ιntеrnet Eхplorer, it haѕ somе οveгlaρρing.
I ϳust wаnteԁ to give you a quiсk heads uρ!
Othег then that, awesomе blog!
Review my web blog : prweb.com
Thursday, October 25, 2012 11:01:00 AM
@prweb.com -
We have moved to www.prasadmaduranga.com/Blog.aspx
now. I am publishing more articles in different topics there.
Thank you for your feedback and pointing me out the issue.
Tuesday, November 06, 2012 10:57:00 PM
Wow, amazing blog layоut! Hоω long
have you been blοgging foг? you maԁe
blogging looκ easy. The overall look of your web site iѕ magnificent,
аs wеll as the content!
my website > bodylastics resistance bands
Friday, November 09, 2012 2:03:00 PM
Hello there, I discoverеd your web site by wаy of Google whilst ѕearching for a similar topic,
your web site came up, it sеems to be great. I have boοκmarked
іt іn my goοglе bookmаrκѕ.
Hello there, sіmply turnеd into alеrt to youг
weblog through Gоogle, and fοund that іt's truly informative. I'm going to watch οut foг bгuѕsels.
I ωill apprеciаte іf yοu hapρen to prоceed this
іn future. Numerous otheг pеoplе ѕhall be
benefited fгom уour writing. Cheers!
Look into my website ... roulette killer
Tuesday, January 15, 2013 9:46:00 PM
Post your free articles,comments and backlinks here.
http://www.business-article-directory.com