I want to implement a comment list and use list view to binding data. But now I have a problem that, only specific item can be deleted or edited by owner. how to config list view to do this function, thanks!
Once the list is bound (like in OnDataBound event), iterate through the items, and use logic similar to this:
foreach (ListViewDataItem item in ListView1.Items)
{
btnDelete.Enabled = User.UserID == ListView1.DataKeys[item.DataItemIndex]["UserID"];
}
Related
I have a few different pages with a radiobutton list that I populate using a collection of answers in the page load event of each page. I was wondering rather than repeating the same code on 4 different pages, is it possible to pass in the radio button list as a parameter to my own class that I made and populate it in there? I can't seem to see A ListItem parameter to pass in.
My code is
foreach (var item in collection)
{
RadioButtonList.Items.Add(item);
}
Instead of having to repeat that, I'm trying to pass the list and the collection to my Person Class, and create a method in there that does the same.Any help is appreciated!
Unless I'm missing something obvious, can you not have something like a static function that has the RadioButtonList and collection as parameters?
public static void PopulateMyRadios(RadioButtonList rdoButtons, MyCollection collection)
{
foreach (var item in collection)
{
rdoButtons.Items.Add(item);
}
}
Yes.. We can create a method of parameter string array which are hold the text of radio button required. Then create html radio button list by using string array and input this radio button list to any of div of corresponding page as inner HTML.
By default, whenever you rollOver/mouseOver (not sure of the difference) an item in a Datagrid or a List, that item is highlighted with the component's rollOverColor. I'm just wondering if there's any way to do that programmatically. I haven't been able to find much help on the issue. For example, suppose I have two DataGrids. When I rollOver an item in the first DataGrid, I want to highlight the corresponding index in the second one as well. Basically, as if two separate cursors were rollOver'ing two separate DataGrids. How can I do this?
Ian
You can listen for the datagrid's itemRollOver event and then select a row in the other datagrid by using it's selectedIndex or selectedItem properties.
1) Create a custom DataGrid with this function :
public function indicesToItemRenderer(rowIndex:int, colIndex:int):IListItemRenderer
{
var firstItemIndex:int = verticalScrollPosition - offscreenExtraRowsTop;
if (rowIndex < firstItemIndex ||
rowIndex >= firstItemIndex + listItems.length
)
{
return null;
}
return listItems[rowIndex - firstItemIndex][colIndex];
}
2) When you want to hightlight an item, call this code :
youCustomADG.indicesToItemRenderer(idxRow, idxCol).dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OVER);
I have a List of items which is based on the contents of the "category" that a user selects
When the user changes selection, I change the dataProvider of the list be be the contents of the current category.
Sometimes the list contains items, sometimes it does not
Is there a way of hiding the list when it has no items?
I know that I could do this when setting the dataProvider, but it seems like there should be an event or something else that I could be using.
You could try
visible="{myList.dataProvider.length>0}"
includeInLayout="{myList.dataProvider.length>0}"
where "myList" is the id of your List component.
My first solution to this was to override set dataProvider:
override public function set dataProvider(value:IList):void {
super.dataProvider = value;
this.setVisible(value.length > 0);
}
This did work, however Robusto's solution works also and is preferable IMO.
I am trying to add a checkbox in a listview with value as ids of the records from the database so I can allow the user to check the ones they want to delete and when they click the delete button I can get value collection of checkbox with request.form.
My problem is, because checkbox in a listview ASP.NET renders the listview name into the name property of the checkbox, it prevents me to do request.form["checkboxname"].
I don't want to use Listviews delete commands but simply use request.form to get the collection of checked values.
How can I set name of the htmlinput checkbox so .NET doesn't change it in render time?
I have tried:
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
HtmlInputCheckBox _CheckBoxDelete = (HtmlInputCheckBox)e.Item.FindControl("CheckBoxDelete");
_CheckBoxDelete.Visible = true;
_CheckBoxDelete.Value = DataBinder.Eval(dataItem.DataItem, "id").ToString();
_CheckBoxDelete.Name = "deletechecked";
But still it renders like:
<input name="PmList$ctrl0$CheckBoxDelete" type="checkbox" id="PmList_ctrl0_CheckBoxDelete" value="3" />
This is happening because ListView is a Naming Container. You can get around this in a couple of ways, but they all boil down to the choice of:
Rendering the HTML you want.
Pulling out the checked items in a different way.
The former is doable, but you'll likely loose a lot of ASP.NET's built in functionality. I'd advise against it unless you're deeply familiar with the control life cycle.
You've got everything you need for the pulling the values out in a way ASP is expecting:
HtmlInputCheckBox _CheckBoxDelete = (HtmlInputCheckBox)item.FindControl("CheckBoxDelete");
You just need to wait for the control hierarchy to be populated, and then loop over the ListView.Items looking for the checkboxes. Your "Delete" button's event handler is probably a good place to call this from.
Incidentally, why are you using a HtmlInputCheckbox, rather than a CheckBox?
I have sorted it out with:
string idCollectionTodelete = string.Empty;
foreach (string x in Request.Form)
{
if (x.IndexOf("CheckBoxDelete") > -1)
{
idCollectionTodelete += Request.Form[x] + ",";
}
}
new DB().DeleteUserPm(
ActiveUsername(), subdomain, idCollectionTodelete.TrimEnd(','));
It is not an ideal solution but it does work for me.
I do this
List<HtmlInputCheckBox> chkDeleteContacts = new List<HtmlInputCheckBox>();
foreach (RepeaterItem item in rptrFamilyContacts.Items)
{
chkDeleteContacts.Add((HtmlInputCheckBox)item.FindControl("chkDeleteContact"));
}
foreach(HtmlInputCheckBox chkDeleteContact in chkDeleteContacts)
{
//Delete Contact
if(chkDeleteContact.Checked)
blnStatus = BusinessUtility.DeleteConsumerContact(LoginConsumerID, chkDeleteContact.Value);
}
Slightly easier in my opinion
I have a dropdown in which we add certain items after the dropdown is bound by data from the db, hence the need to sort the dropdown arises. So i need to sort a dropdown which can have duplicates. What is the best way of doing this?
Instead of adding items directly to the Dropdown, I would suggest adding them to the data structure that you bind to. If the items in this structure implement IComparable, then you can define a comparison method to apply sorting before the Dropdown is actually bound to the data source.
Assuming you are binding to a Generic List you can try something like this :
var ddlFoo = new List<foo>();
foreach (var lc in myDropDownList.Items)
{
ddlFoo.Add((foo)lc);
}
myDropDownList.DataSource = ddlFoo.OrderBy(dl => dl.fooID);
myDropDownList.Databind();