Weird behavior while creation of two Telerik RadComboBox items programmatically - asp.net

I've two Telerik RadComboBox controls, while adding of their items using the following code
foreach (var gate in Enum.GetNames(typeof(AuthorizedGates)))
{
var item = new RadComboBoxItem(gate, Convert.ToString((int)Enum.Parse(typeof(AuthorizedGates), gate)));
ddlTelerik1.Items.Add(item);
ddlTelerik2.Items.Add(item);
}
at runtime, the first combobox has zero items i.e. items are not added to it while items are added to the second one!
I tried the same for ASP.NET DropDownList using the following code
foreach (var gate in Enum.GetNames(typeof(AuthorizedGates)))
{
var item = new ListItem(gate, Convert.ToString((int)Enum.Parse(typeof(AuthorizedGates), gate)));
ddlAspNet1.Items.Add(item);
ddlAspNet2.Items.Add(item);
}
it is working properly and items got added to both of them.
Any ideas what could be the reason behind this weird behavior?

The difference is that the RadComboBox item is a control like the RadComboBox or DropDownList controls, so it's a class that indirectly inherits from WebControl, and can only have one instance on the page. It's not like a ListItem (which inherits from Object).

Related

Telerik RadComboBox AutomaticLoadOnDemand

I use Telerik RadComboBox in my project. I set EnableAutomaticLoadOnDemand="true" on RadComboBox. It works good but when I want to set selected item on load of page event, it doesn't show selected item
With load on demand mode, so combobox does not have any item. It just has item(s) when you action on it.
In my opinion, you should get the specific item and add it to combobox manually on page load event like this way (I'm not sure the structure, just an idea.)
if (!IsPostBack)
{
var itemSelected = service.GetById(Id); //item Id
this.combobox.Items.Add(new RadComboboxItem(itemSelected.Id, itemSelected.Name));
this.combobox.SeletedValue = Id.ToString();
}
RadCombo allows to set up SelectedValue and Text properties even through there are no items in it.

Devexpress ASPxGridView New Row Disabled

I have code like this with devexpress ASPxGridView. Everything goes well, but when I try to add new row, new row's textbox is disabled...
I have KeyFieldName set.
void BindGrid()
{
var AnnObj = SearchBarBanners.Select(i => new
{
Caption = i.Attribute("caption").Value,
ID = i.Attribute("id").Value, // this is generated by Guid
}).ToList();
ImagesGrid.DataSource = AnnObj;
ImagesGrid.DataBind();
}
I can suggest you two things without grid markup:
1. Call BindGrid method in Page_Init
2. If your datasource initially returns zero rows, grid won't be able to determine type of objects that will be rendered in grid. You need to use ASPxGridView.ForceDataRowType in order to solve this problem.
Use recommendations from the Q392961 DX Article to resolve this issue.

using JavaScript, find the ClientID of .NET control nested deep within a master page

The short question:
*takes deep breath*
How I can ClientID of TableRow inside Table added to PlaceHolder, which is in a UserControl inside a Web Part added to a SharePoint page using a MasterPage?
The explanation:
I'm building a usercontrol that dynamically shows SPList items, one per Web.UI.Table(). The 2nd row of the Table() will be hidden at first, with an image used to initiate some JavaScript to hide/show the row at the clients browser (rather than use postback or ajax - I have reasons).
The Table() is then added to a PlaceHolder in a UserControl, which is then used by a WebPart, and that WebPart is added to a SharePoint Page, which of course uses a MasterPage. I'm having real trouble working out the JavaScript method to find the ClientID of the TableRow, given that it's nested so deeply.
My .ascx code looks something like this..
<script type="text/javascript">
function showHidden(itemId) {
var controlId = document.getElementById('<%= phDownloadTable.ClientID %>');
//alert(controlId);
document.getElementById(controlId).style.display = (document.getElementById(controlId).style.display == "table-row") ? "none" : "table-row";
}
</script>
<asp:PlaceHolder ID="phTable" runat="server"></asp:PlaceHolder>
and my .cs codebehind is something like this..
Table myTable = new Table();
TableRow hiddenRow = new TableRow();
hiddenRow.ID = "row" + itemId;
hiddenRow.Attributes.Add("style","display: none;");
... create TableCells and add to hiddenRow...
TableRow displayRow = new TableRow();
TableCell toggleCell = new TableCell();
Image toggleImage = new Image();
toggleImage.ImageUrl = "/images/myimage";
toggleImage.Attributes.Add("onclick","javascript:showHidden('" + hiddenRow.ClientID + "');
toggleCell.Controls.Add(toggleImage);
displayRow.Cells.Add(toggleCell);
... create more TableCells and add to displayRow
myTable.Rows.Add(displayRow);
myTable.Rows.Add(hiddenRow);
the result is that the toggleImage "onclick" attribute shows showHidden('row999');, which passes 'row999' to the JavaScript function, but I cannot figure out there how to get the full clientId of the TableRow it refers to.
The source of my page shows the clientId to be ctl00_SPWebPartManager1_g_eda9b9e9_4c7a_48e0_a2aa_fd8cdd65de6c_ctl00_row999, which is longer than I'm used to seeing, and seems to contain two 'ct100_' parts, suggesting multiple levels of controls.
Any ideas would be greatly appreciated. I've tried all the usual avenues (googleing for 'javascript .net control client id' and the like, but so far I've not found anything that helps. Most suggest document.getElementById('<%= myControl.ClientId %>')... which is fine, but I don't know 'myControl' - I need that send from the toggle image.
Fingers crossed!!
Kevin
If you cant set the client id, you should be able to set a class, and that should be respected by .nat.
Them you can select the element by class name.
JavaScript has no wildcard selection options. Try using jQuery, that makes things more flexible.
Then you can use something like:
$("tr[id*='<%= phDownloadTable.ClientID %>']").css("display", your value);
this way you will still find the right element, even when it's moved to another place on the page.
Here is a more detailed explanation on how to use these wildcard selectors:
http://api.jquery.com/attribute-contains-selector/
With plain JavaScript you can do a document.getElementsByTagName('tr') and then loop those to find the right object.
If you are using Framework 4.0 you can set the ClientIdMode of the page like this:
Page.ClientIDMode = System.Web.UI.ClientIDMode.Static;
That way you can have more predictable client ids. For example, you can have id's without all the ctl00_ prefixes.

ITemplate, the reason for leaving InstantiateIn(Control container)

I'm implementing the ITemplate interface in ListView control. If i realize it for ItemTemplate in my custom class, everything will be OK. I mean, the runtime will invoke InstantiateIn when i use
ListView.ItemTemplate = new CustomClass();
CustomClass :ITemplate
{
public void InstantiateIn(Control container)
{
HtmlTable table = CreateHeader();
container.Controls.Add(table);
}
...
}
But i want to do the same with ListView.LayoutTemplate. In this case, the runtime invokes InstantiateIn only one time, but every next update it leaves my method. What is the reason for it?
Layout template says how the root container does look like, it is not per item thing.
I have to change my LayoutTemplate after clicking different buttons.
And My LayoutTemplate has a header for the whole ListView. I have to change it, it depends on button.
Also i have two custom classes with implementations of ITemplate (one for ItemTemplate and one for LayoutTemplate). I am going to realise the folowing behaviour:
1). If i click Button1
ListView.ItemTemplate = new CustomItemClass1();
ListView.LayoutTemplate = new CustomLayuotClass1();
2). If i click Button2
ListView.ItemTemplate = new CustomItemClass2();
ListView.LayoutTemplate = new CustomLayuotClass2();
But i can't see my header of LayoutTemplate more, then one time.

Set HTML input checkbox name in listview

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

Resources