Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Implementing order on a page with up/down arrows 2

Status
Not open for further replies.

developer155

Programmer
Jan 21, 2004
512
0
0
US
Hi, I have a page in my Content Manager System that have its content populated by repeater. The repeater shows items that will be populated on the live site.
I need to implement functionality were user can have up/down arrows and control order in which items will show.
So I need up/down arrow controls and a way to order repeater items with those.And of course db field that hold order

Does anyone know what is the way to implement this?

thanks
 
Add an integer field to your database, something called order I guess.

Select the items form the databse with an order by clause.

Add code into your cms form whereby if the user clicks on the up arrow, you swap the order value of the 2 items. (ANd then bind your repeater to a DataView, so you can reorder it on the fly without writing / reading from teh db every postback
 
sounds like you have what you need. the only issue you may still need to answer is how far back do you sort content based on sort order(weight) instead of timestamp?

the grid view will have readonly columns for the title/teaser. the id will be the datakey. create a templatefield for up/down arrows. (set the row index as the CommandArguement) you may want to allow the user to enter the weight manually so they can jump multiple weights at a time.

in the code behind bind the grid on pageload. then when the user clicks either up or down parse the command argument to an integer, get the record id from the datakey using the row index (command argument). adjust the weight accordingly.

here is a simple code example outlining your requirements
Code:
//aspx
<asp:gridview id="myGrid" datasourceid="myData" allowpaging="true" datakeynames="node_id">
   <columns>
     <boundfield headertext="Title" readonly="true" datafield="title" />
     <boundfield headertext="Content Snippet" readonly="true" datafield="teaser" HTMLEncode="false" />
     <Templatefield headertext="Adjust Weight">
        <ItemTemplate>
            <asp:Button 
                   id="UpButton" 
                   CommandName="increase" 
                   CommandArguement='<%#Container.ItemIndex %>' 
                   text="Up" 
                   CausesValidation="false" 
                   OnClick="AdjustWeight_Click" />
            <asp:Button 
                   id="DownButton" 
                   CommandName="decrease" 
                   CommandArguement='<%#Container.ItemIndex %>' 
                   text="Down" 
                   CausesValidation="false"
                   OnClick="AdjustWeight_Click" />
        </ItemTemplate>
     </Templatefield>
   <columns>
   <pagingStyle mode="numericfirstlast" />
</asp:gridview>
<asp:datasource id="myData" ... />


//code behind
protected void AdjustWeight_Click(object sender, EventArgs e)
{
   int index = int.Parse(e.CommandArgument);
   int nodeId = (int)this.myGrid.DataKeys[index]["node_id"];

   //fetch record using nodeId

   switch(e.CommandName)
   {
       case "increase":
          increase weight by 1, save record
          break;
       case "decrease":
          decrease weight by 1, save record
          break;
   }
}
this mock up will allow nodes(content) to have the same weighted values. if you don't want this you will need to adjust all the content with weights >= the new weight.

update these records with a simple query like
Code:
update node set weight = weight + 1 where weight >= @weight

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
thanks!!
Jason, this should work with the repeater as well, correct?
 
yes, infact the repeater would probally be a better choice since most of this is read-only data.

you will need to set the command argument of the buttons to the node_id, instead of the index of the row. The repeater does not have a DataKey property as it's meant for read only data.
your buttons would now look like this
Code:
<asp:Button 
                   id="UpButton" 
                   CommandName="increase" 
                   CommandArguement='<%#Eval("node_id") %>' 
                   text="Up" 
                   CausesValidation="false" 
                   OnClick="AdjustWeight_Click" />
            <asp:Button 
                   id="DownButton" 
                   CommandName="decrease" 
                   CommandArguement='<%#Eval("node_id") %>' 
                   text="Down" 
                   CausesValidation="false"
                   OnClick="AdjustWeight_Click" />
and your code behind
Code:
int node_id = int.Parse(e.CommandArgument);

error checking is missing from this example. you will need to check that you can't go "up" on the first record, and "down" on the last. Nothing would exist for the selected record to pass.

if your concerned about file size and internet connectivity you may want to concider disabling viewstate for the repeater.

depending on the number of records on screen you may want to concider the gridview so you can easily page your data. the repeater doesn't have paging attributes.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top