dropdown not choosing the correct item - asp.net

I am using asp.net and vb.net as code behind. I have dropdown box. It will populate the list of values on page load. I have a data coming from webservice. arrdata consists of web service data.
If IsNothing(arrData(12)) Then
DropDownList_mylist.Items.Add(New ListItem("Choose"))
DropDownList_mylist.SelectedIndex = DropDownList_mylist.Items.IndexOf(DropDownList_mylist.Items.FindByText("Choose"))
Else
DropDownList_mylist.SelectedIndex = DropDownList_mylist.Items.IndexOf(DropDownList_mylist.Items.FindByText(arrData(12)))
End If
The problem is, it is giving the first item. not the correct item.

Use the AppendDataBoundItems property and set it to true if there are existing items in the list already.
Similar question here:
Dropdown list bound to object data source - how to update on a button click

Related

Google Appmaker: Update form based on selected dropdown option

I have an AppMaker app that has a from based off of one address table/datasource. I can get a form with next/prev buttons, but replaced the key field (name) with a dropdown list of all names (a user can start typing names to jump there, with the dropdown showing).
My hope is that when a user selects from the dropdown, the entire form updates and the next/prev buttons work properly as well (there too many records to page thru with next/prev only). I don't have to have next/prev functionality if it complicates things too much.
Currently the dropdown is working, but I cannot get the index for the next/prev buttons set or the rest of the form to reflect the selected dropdown record.
I've tried to set the "onValueEdit" event to something like this...
var selected = widget.value;
var idx = widget.options.indexOf(selected);
console.log("Selected: "+selected+", index = "+idx+"\n");
if(idx < 0) { //...this error is never hit
console.log("Index error - setting to zero!\n");
idx = 0;
}
widget.datasource.loadPage(idx); //...update form?
Two observations via console logging:
The "idx" var is never set to the selected dropdown index reliably, and is
often "0" (tho no error msg ever shows), so the "indexOf()" function
isn't working as expected.
The "selected" var (name) is always correct.
If I call widget.datasource.loadPage(...) with a fixed value (say 5) it has no effect on what is shows in the form either (previous loaded data remains) - obviously not the way to do it :v/
Can you steer a noob in the right direction?
If you are using default App Maker form, then you can see that so-called pager, doesn't actually paginate. It triggers prevItem/nextItem datasource methods, in other words it navigates through datasource items, not pages. Here is a quote from App Maker docs:
nextItem: Selects the next item. For Query Datasources, if the current item is the last item on the page, then this loads the next page and selects the first item on the newly loaded page.
So, if you already have all your items loaded(you set query page size for your datasource to 0), then you need just to change selected item within datasource:
// onValueEdit dropdown event
// assuming, that form and dropdown share same datasource
widget.datasource.selectKey(newValue._key);
If you really have lots of items and it is not feasible to load all of them in one call... then it will be all another story...
UPDATE:
It's important that Options and Value are set as shown in the image below!
However, I had trouble setting them that way (read: wasted hours!) until I wiped them both completely using More options in the binding picklist, and tried again (I had even tried on a brand new app!). I was being forced to choose ..projections.. and then a final field before the OK button would be available.
Not sure if AppMaker is buggy here or there is something simple I'm not understanding!
None of the coding in my original question is required.
Once set this way, binding just works as you would expect it!!
All other fields are set as #datasource.item. and are bound to whatever item is chosen. No Events settings are necessary for the dropdown either, as I thought they might be.
I deleted this page and started again, and replaced the default business name data field with a drop down, I set the dropdown as:
Options: #datasources.Addresses.items
Value: #datasources.Addresses.item
It works fine?! Not sure what happend in my original page!
UPDATE:
So it seems you need to delete both the Value and Options and then re-enter these. The OK will light up when you do.
Also, my original take on App Maker was to build the UI and attach data. That was my first mistake. You build the data then have App Maker build edit/add pages for you.

How to access Dynamically Generated Controls not knowing their names

My page generates a list of check boxes at run time. I set the Id of each of these boxes manually by assigning to each the string.Format("checkbox_{0}, n} where n is a running number.
How to I find out the list of checkbox controls from codebehind? I couldn't find them in Page.Form.Controls. Where are they placed?
(I can see the checked checkboxes in Request.Form but that contains client side names of each control.)
Thanks.
-
---More info----
My iterative loop Adds CheckBoxes to a Panel control (created in the mark-up page) as follows:
CheckBox checkbox = new CheckBox();
checkbox.Text = "Add to list";
checkbox.ID = string.Format("checkbox_{0}", n.ToString("0"));
Panel1.Controls.Add(checkbox);
The checkboxes show up nicely on the page. I can tick. When I submit the Http POST stream contains all those ticked ones.
On post back, Panel1.Controls.Count is 0. When the page was first generated, Panel1.Controls.Count was 200, as it contained lots of LieteralControls for layout.
You can use the findcontrol method on page object or parent control object to find that checkbox object. like:
CheckBox chx=(CheckBox)Page.FindControl("Checkbox1");
if(chx!=null)
chx.Checked = false;
you can put this insode the loop also like:
for(int i=0;i<n;i++)
{
CheckBox chx=(CheckBox)Page.FindControl("Checkbox"+n);
if(chx!=null)
chx.Checked = false;
}
On post back, Panel1.Controls.Count is 0. When the page was first generated, Panel1.Controls.Count was 200, as it contained lots of LieteralControls for layout.
If you add controls dynamically in this way, you need to add them again (same ids, same order) on each postback.
Which means you need to persist whatever info you need to add them again. For example, you could store the number of checkboxes in ViewState, then regenerate that number of checkboxes on each post back.

Pre-select items on data-bound cascading dropdown lists in ASP.NET page

I have the following problem:
On a ASP.NET page, say books.aspx, I have three dropdown lists: ddlBook, ddlChapter, ddlPage, one listview lvPageContents and four sql datasources dsBooks, dsChapters, dsPages and dsPageContents.
On initial design, user selects the Book from ddlBook (populated from dsBook), then Chapter (from the selected book's chapters) and Page (from the selected chapter's pages). Finally, the lvPageContents shows the data of the selected page.
I would like to add the functionality of calling books.aspx with QueryString("BookID") or "ChapterID" or "PageID" and programmatically put the corresponding values as selected items to the corresponding dropdown lists.
Should I completely override the datasource/databinding of ASP.NET in code behind, or is there another workaround?
In code behind .cs file you can pre select Drop down list using Item Lists index.
DropDownList1.SelectedIndex = 0;
or by using values like this-
DropDownList1.SelectedValue = "page1";

CheckedListBox Item Selection

I have created a site that allows users to see a list of files that currently reside on my FTP site.
I have got the files appearing but when I select one and click download, it is not registering that I have selected an item.
At the moment I am using:
If CheckBoxList1.SelectedItem.Value = "" Then
MsgBox("A file needs to be selected...!")
End If
'NOTE - A SELECTION ISN'T BEING RECORDED BY THE PROGRAM!!'
If CheckBoxList1.SelectedItem.Value <> "" Then
I think that it is not registering the selection being made, because the item being selected is not a string value.
But I am not sure what to set this as.
It would help if you provided the actual code being used to attach the selection / change handler as well as the event method body.
I'm guessing your attaching to the checkbox SelectedIndexChanged event. If that's the case then make sure that you have actually bound a "value" to the checkbox list, ensure that you are using the "DataTextField"and "DataValueField" properties instead of just default binding the checkboxlist to a collection.

SharePoint list item with checkbox databinding possible?

In SharePoint I can tee up a binding to an edit field like this below. When the form posts back the changes are automatically persisted to the underlying list item.
<PublishingWebControls:RichHtmlField ID="Field1" FieldName="MySPListItemFieldName" ...
So this works great for RichHtmlFields, but say I've got a Yes/No (boolean) field in the same list item, is there a similar construct to bind that field to a check box control in a similar way?
My goal is to not have to throw down a line of c# to transfer the value of the control to the field, I want it to be automatic like RichHtmlField. It seems like there has to be a straight forward way of doing this since SharePoint does this itself with its internal list item editing page (EditForm.aspx).
What you're after is a BooleanField control on your form:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.booleanfield.aspx
Just set the FieldName to that of your Yes/No column, should work fine.

Resources