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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add Delete Select logic in a Repeater

Status
Not open for further replies.

DJVisuaL

Programmer
May 31, 2000
52
US
I'm having a hard time creating the logic for a SqlDataSource binded to a Repeater.

In the other controls this is all done automatically but I need to use the Repeater for formatting purposes.

I have a repeater binded to a table, then a delete button, add textbox and an add button like so: [item1] [item2] [item3] [Delete Button] [Add TextBox][Add Button]

The table has 2 fields ItemID and Item. Item is binded to .Text and ItemID is binded to .ToolTip so I can get to it in order to delete.

Have any of you created this kind of logic manually?

So far I have in Page_Load some ViewState variables to keep tabs on things:


Code:
 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState.Add("SelectedID", "0");
            ViewState.Add("SelectedIndex", 0);
            ViewState.Add("ItemCount", 0);
        }
    }
and in my datasource selected I set the count:

Code:
    protected void ItemDataSource_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        ViewState["ItemCount"] = e.AffectedRows;
    }
I have code in the Repeater1_PreRender to loop through and set the color of the currently selected item and code in ItemCommand to set which is currently selected and this works fine:
Code:
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            // find the button
            Button lb = (Button)e.Item.FindControl("ItemButton");
            
            // set which index to select (change colors) in prerender
            ViewState["SelectedIndex"] = e.Item.ItemIndex;

            // set select parameters to button text and do the select
            ItemDataSource.SelectParameters["ItemID"].DefaultValue = lb.ToolTip;
            ViewState["SelectedID"] = lb.ToolTip;
        }
    }

My problem is how to handle the adds and deletes and where should this code go? After I Insert() or Delete() it needs to update the ViewState["ItemCount"] and select the first item. Do I have to re-select to get a new count? or maybe Rebind the Repeater even?
 
I am not sure if that what you need but you can possibly add delete button to repeater like this

<ItemTemplate>
<tr><td><%# DataBinder.Eval(Container.DataItem, "Model") %></td><td><asp:Button Text="push" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "ID") %>' CommandName="delete" runat="server" /></td></tr>
</ItemTemplate>

and execute delete or insert command of the datasource in your protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")

and pass a parameter here possibly

DataSource.delete();

 
thanks for your reply!

how do I do this when my delete button is outside of the repeater?
I want them to be able to select an item and then press the delete button. If i put CommandName="delete" will the repeater still trap this if the button is outside the repeater like so? ([item1][item2]...) [delete button]
 
OK I figured this out.
I moved the delete/minus button inside of the repeater's footer template then I was able to specify a CommandName and access it through Repeater1_ItemCommand method.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top