Panel Controls[i] - index out of range - asp.net

I have a panel Profiles List on a web page. It contains multiple small panels, like a list of items contains lines. The items here are profiles. Each profile is actually a small panel SingleProfile with checkbox inside. Checkbox is the core here, because it has an ID, which is used to find the same items.
On this page there is also another similar big panel. I should add to the second panel only those items, which do not exist in the first one. So, the task is to show in the second panel only those profiles, which are not presented in the first panel. This is checked using IDs of the checkboxes in both lists.
To check if a newly added profile in the second panel already exists in the first panel, I try to access the ID property of each item in the first panel through Controls[] property. I do it in the cycle:
if (ProfilesPanel1.Controls.Count > 0)
{
for (int i = 0; i <= ProfilesPanel1List.Controls.Count - 1; i++)
{
//label1.Text += " "+Convert.ToString(sqlReader["chain_id"]); //Bug #2 - see Stored Procedure description
cbx1 = new CheckBox();
cbx1 = (CheckBox)ProfilesPanel1List.Controls[i].Controls[1]; //Bug #1 - here the Exception occurs
if (cbx1.ID == Convert.ToString(sqlReader["chain_id"])) //if profile already exists in the first panel
{
//Do not add this profile to the second panel
}
}
}
The exception "System.ArgumentOutOfRangeException" occurs in the line marked with "Bug #1".
I use sqlReader which takes IDs from a stored procedure. Stored procedure works fine in the Sql Server. But, when I output values of the sqlReader into a label on a web page (see comments in the code), each value is doubled, like "1 1 2 2 3 3 4 4 5 5".

Solution to BUG #1
Check number of controls and its names from quickwatch in debug mode and see, is ProfilesPanel1List.Controls[i] a panel or some other control. Choose your index correctly after checking it in debug mode.
And also you don't need to use cbx1 = new CheckBox(); line of code. Its next line of code is enough to initialize that Checkbox.
Your sqlReader code is not enough to get the actual cause of 2nd error.

For Bug#1, there is a chance that there might be no nested controls existing underneath 'Controls[i]'. So to guard against the bug, you can first check if there are any nested controls underneath the current element and then checking the type of control before type casting.
if(ProfilesPanel1List.Controls[i].Controls.Count>1 && ProfilesPanel1List.Controls[i].Controls[1] is CheckBox) //Iff you are double sure about layout structure and assured that it is not going to
//change. If changes are anticipated then again doing a foreach(..) over nested 'Controls' and type
//checking is much better.
{
//Do the type conversion etc., as per your requirement.
}
For Bug#2, what do you exactly mean by 'each value is doubled'? As I can see that you are appending to the same Label (with ID: Label1) inside the loop so, there is a chance for the text to be concatenated on every iteration. Also, please be sure as per your requirement, whether you need to manipulate same label's text or label inside nested controls of the parent panel.

Related

Cannot add new correlated record to new created record

When I create new record in Google AppMaker and then try to add correlated record to this new one I get this warning in console:
com.google.apps.appmaker.client.datasource.AbstractModelDataSource
WARNING: Could not find element with key RecordKey{key=private$7,
model key=...
Both datasources are set to:
Manual save mode
Automatically load data
The problem doesn't appear when I refresh the page or try to add correlated record to other existing record.
Anybody knows what could be a reason for this error?
App Maker doesn't allow to have unsaved changes on both ends of relation, most likely it is a reason, why you recieve error message in the first case. But in theory it should work once you save one of the relation ends (first save one of the records and then link them and save again):
var countryDs = app.datasources.Country;
var capitalDs = app.datasources.Capital;
countryDs.createItem();
countryDs.item.Name = 'United States';
countryDs.saveChanges(function() {
capitalDs.createItem();
capitalDs.item.Name = 'Washington, D.C.';
capitalDs.item.Country = countryDs.item;
capitalDs.saveChanges();
});
OK, I fixed it.
I have two forms. First to create item. Second to edit data. In the first form page need to be set to:
On Detach: Clear Changes To Datasource.
Datasources need to be set to autosave.

Text Property Confusion

I am not sure what is happening, maybe someone can clarify.
The scenario is simple, I have a form that I am submitting to update a DB.
So on Page_Load, I set each field to the current value in the current object.
Example:
txtFirstName.Text = empInfo.FirstName // FirstName = Jane
txtLastName.Text = empInfo.LastName
// Etc
Now at runtime it may be edited by the user, typical textbox stuff.
When I run my button click to update it will always return the assigned Text value previously and not the new user-edited value.
Let's say the user edits the field:
First Name: [ Joe ]
If I were to print txtFirstName.Text, it is STILL Jane
Note: This does not happen if the Text property is never set, in that case, it works as expected
Sounds like the code that assigns your txtFirstName.Text control/property is running again after post back and overwriting the new value. Make sure that you initialization code is wrapped in a check for (!IsPostBack) to ensure it is only run the first time the page is accessed an not with every post back (update) to the page.
Post your Page_Load code or where you do the initialization and we can probably confirm this is the issue.

Adding Removing Columns to Datagrid in Flex with States

I was developing an app that in the first state you choose a date range using dataFields then you press a button to go to another state and generate a datagrid showing an employee list and another scrollable datagrid with its columns generated dynamically having the worked hours for every employee in every date.
The step sequence to get the error message is:
You choose a date range for example: from 01/01/2013 to 01/31/2013
You press generate button (The app change the currentState = "EmployeeList" and all is OK)
You press the back button (you return to initial state and all is OK)
If you change the date range having more days than before date range then all is OK
If you change the date range having less days than before date range then next error is reached
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at mx.controls.dataGridClasses::DataGridBase/http://www.adobe.com/2006/flex/mx/internal::columnHeaderWordWrap()
at mx.controls.dataGridClasses::DataGridItemRenderer/validateProperties()
at mx.managers::LayoutManager/validateClient()
at mx.controls.dataGridClasses::DataGridHeader/updateDisplayList()
at mx.core::UIComponent/validateDisplayList()
at mx.managers::LayoutManager/validateDisplayList()
at mx.managers::LayoutManager/doPhasedInstantiation()
at mx.managers::LayoutManager/doPhasedInstantiationCallback()
and if you google it then some results take to visit apache bug reporting site
https://issues.apache.org/jira/browse/FLEX-22108
And there is no more...
But I found how to solve it!
To solve it first of all I was googling a lot and i looks like no one got this error and I discover that is a Flex Bug reported to Apache. And I was analyzing the original code from DataGrid.as and DataGridColumn.as to think about a possible solution and I was making some tests and nothing work.
What I did and I hope it will be useful to someone
when you click the back button, inside the backbutton_clickEventLister() and before currentState="";
I just set columns array to new Array();
protected function bttnBack_clickEventHandler(event:Event) : void {
// This code line solved it
dtGrdWorkedHours.columns = new Array();
// Make sure of code it before state change stament
currentState = "";
}

Selecting list values automatically results in StaleElementReferenceException

In my search page I have a list with an input box and 'Go' button. I get the list value in my console but what I want is to iterate the list and enter the text (text is static) in the input box. Every time the list is selected it automatically searches rather than having to press the 'Go' button.
WebElement search_list = driver.findElement(By.xpath("//*[#id='tl_tPlace']"));
List <WebElement> li = search_list.findElements(By.tagName("option"));
for(int i =0; i<li.size();i++){
System.out.println(li.get(i).click());
try{
driver.findElement(By.xpath("//*#id='cmain_Place']")).sendKeys("text");
Now the first time the list has "select the state" it passes for the 2nd loop and it selects the list item. Once selected, the page reloads. Then there's an error message:
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
This is entirely expected behavior, since the DOM is completely recreated when the page, so any references you have to elements will become invalid after the page refreshes.

select number of items in drupal views

Is there a way i can have an exposed filter wherein the user can select the number of items displayed by the drupal view?
I am nearly positive that you can do this with hook_views_pre_build (&$view). I know for a fact that you can mess with $view->pager to update whether to use the pager or not and to alter the number of items per page.
This is a snippet from a custom module I did:
if (is_numeric($perpage) && (int) $perpage > 1) {
$view->pager["items_per_page"] = (int) $perpage;
} else if ($perpage == "all") {
$view->pager["use_pager"] = false;
$view->pager["items_per_page"] = 0;
}
I suspect you can turn the pager off and also set the items per page to limit the results.
EDIT:
OK, just saw the part about the exposed filter. The code snippet is actually from a bit of code that simulates an exposed filter for this case. The page has some links on it to select the number of items per page. The links refresh the page, and tack on a perpage=whatever parameter. The hook then sanitizes the input, and basically runs the code snippet above. I have also done something similar using HTML a select, and then wiring up the parameter w/ refresh using jQuery.
Based on this thread in the issue queue, that appears to be a long requested feature. It has apparently been added to the 6.x-3.x-dev branch, so it should be available in the 6.x-3.0-alpha3 release.
Or if you're using Drupal 7, it has been added to the 7.x-3.x-dev branch too, so it should be in the 7.x-3.0-alpha1 release.

Resources