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:
and in my datasource selected I set the count:
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:
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?
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);
}
}
Code:
protected void ItemDataSource_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
ViewState["ItemCount"] = e.AffectedRows;
}
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?