My problem is this:
I display a number of IDs using 5 Dropdownlists inside a FormView. The dropdownlists are fed by SQLdatasources to retrieve the IDs.
To display blank entries during the initial presentation of the dropdownlist, I use the code below which works fine during the Page_Load and whenever I click the Cancel button. However, whenever I click the insert button to insert a record, the dropdownlist will not work with the code below no matter where I put it. If I put the code below to execute after the inserted event of the FormView, the code never clears the dropdownlists . If I place the code inside the insert button click, it clears the dropdownlists before the record is inserted. I have placed the code after just about every single event in the Formview, ObjectDataSource, and even the dropdownlists themselves. Do anyone out there have any ideas as to what to use since the events are not making sense. I use the code below in a separate class that I call from different areas. Any help is appreciated. I wish I could call the Page_Load event since I know that it works, but I don't know how to do it.
myFormView.DataBind();
FormViewRow row = myFormView.Row; DropDownList myDD1 = (DropDownList)row.FindControl("DropDownList1");myDD1.Items.Insert(0,
new ListItem("", ""));myDD1 = (
DropDownList)row.FindControl("DropDownList2");myDD1.Items.Insert(0,
new ListItem("", ""));myDD1 = (
DropDownList)row.FindControl("DropDownList3");myDD1.Items.Insert(0,
new ListItem("", ""));myDD1 = (
DropDownList)row.FindControl("DropDownList4");myDD1.Items.Insert(0,
new ListItem("", ""));myDD1 = (
DropDownList)row.FindControl("DropDownList5");myDD1.Items.Insert(0,
new ListItem("", ""));
Dropdownlist can not clear textboxes after an insert
WelshBird
Gents:
I feel so stupid now. The answer was right there under my nose. The formView allows you to enter blanks or any text in the Edit Items for each dropdownlist. And I knew that, however, what I didn't know is that under the RequiredFieldValidator properties, which you can assign to each dropdownlist to avoid saving blanks or anything else you don't want, you can set the "initialvalue" property of the validator to anything, including blanks. In that case you have a blank at the begining of the dropdownlist and it gets validated before you do and insert by the Field Validator. My problem resolved. I which there had been more documention on this.
Kees 123
The Ispostback does not work in this instance. I don't want to use direct dropdownlists because I don't want to write the code to write the complete record back to the DB. I have my formview attached to an object datasource to do that.
I'm now trying to read the ID DataTables individually into ArrayLists so I can then read the ArrayLists into the individual DropDownLists. That way, I don't have to deal with the postback behaviour and the inconsistency of the Inset/event bahviour.
I'll comeback if I have any success.
rta_llc
My last proposition about using a reader into an arraylist and then from arraylist into the dropdownlist inside the formview was a thing of beauty, but it bombed out as well. The list can not be bound to the datasource for saving into the DB. Additionally, the DDL items dissappear on postback. Very weird. This FormView is more difficult to work with than I thought. The previous comment about using dropdownlist directly into the form rather than within the formview may be the way to go. A lot more code, but it may be better in the long run.
Anyway, thanks to all of you. If someone ever comes up with something, let me know. It seems that it may work perfectly in other applications, but just not for this one. That insert key is something.
Dave Matsumoto
Hello,
Try using !postback and see if that makes a difference. Try using the dropdowns directly and directly bind them to the datasourse....
Example :
DropDownList list = new DropDownList();
list.DataSource = something;
list.DataBind();
Will Merydith
I think you are going to have to call a clear and reset the index back to -1
myDD1.Items.Clear();
myDD1.SelectedIndex = -1;
The do redo your insert, one at a time... Loop through what you want....
Just a guess!!!