advanced web statistics

SqlDataSource and AJAX Reorder List from Codebehind

12/12/2006 4:21:48 PM

There was a member on the ASP.NET Forums having problems programmatically accessing a SqlDataSource and ReorderList on his form. I could not find the post again so I will post my reply here.  He was creating a SqlDataSource in his code-behind file and then adding it to a PlaceHolder control with no luck. 

If you are reading this and you are the person in question (or you are not and are having the same problem) then the following example should help you.

First I added both the ReorderList and SqlDataSource controls to the form as shown below.

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server" />

<ajax:ReorderList ID="ReorderList1" runat="server">
   <ItemTemplate>
      <!-- ... -->
   </ItemTemplate>
</ajax:ReorderList>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" />

</form>

Then in your code-behind for that page edit the controls.

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
      EditSqlDataSource();
      EditReorderList();
   }
}

private void EditReorderList()
{
   ReorderList1.AllowReorder = true;
   ReorderList1.CssClass = "ReOrderControlStyle";
   ReorderList1.DataSourceID = "SqlDataSource1";
   ReorderList1.DataKeyField = "KeyField";
   ReorderList1.DragHandleTemplate = "";
   ReorderList1.DragHandleAlignment = ReorderHandleAlignment.Left;
   ReorderList1.ItemInsertLocation = ReorderListInsertLocation.Beginning;
   ReorderList1.SortOrderField = "SortField"
   ReorderList1.LayoutType = ReorderListItemLayoutType.Table;
   ReorderList1.PostBackOnReorder = false;
}

private void EditSqlDataSource()
{
   SqlDataSource1.ConnectionString = "ConnectionString";
   SqlDataSource1.SelectCommand = "SELECT * FROM ...";
   SqlDataSource1.UpdateCommand = "UPDATE ... SET ...";
   SqlDataSource1.OldValuesParameterFormatString = "format";
   SqlDataSource1.UpdateParameters.Add(new Parameter("foo"));
   SqlDataSource1.UpdateParameters.Add(new Parameter("bar"));
}

Almost forgot...

using AjaxControlToolkit;

Easy.

AJAX, C#, Code, Tools

kick it on DotNetKicks.com

Leave a Comment

   

  Enter the text to proceed!