Set default value of dropdownlist based on datasource - asp.net

I have a webform that uses a dropdownlist to populate a gridview. The dropdown list is bound to a datasource. I would like to have the default value of the dropdown list be a value in the listing. I have a series of 5 values, one of which is actually a blank. The default value I am looking for would be index 2 or the value "Agency Error"
Here is the query I have for the datasource:
SELECT DISTINCT [AnnoType] FROM [AnnoType]
Is there a way to do this?
I am just trying to eliminate one of maybe 50 clicks the user needs to do in the process.

Use SelectedIndex property
dropdownlist.SelectedIndex = 2;
or SelectedValue property
dropdownlist.SelectedValue = dropdownlist.Items.FindByText("Agency Error").Value;

Related

Refresh drop down list

I have a drop down list that is populated based on the selected value of another drop down list. The main drop down list is dlJobName. It contains the list of all active jobs. The second drop down list is dlStage. This contains the distinct stage values related to the JobName. Here is the query that populates the dlStage drop down list.
SELECT DISTINCT [AnnotationDate] as Annotation_Date FROM [vw_GridviewSource] WHERE ([Name] = #Name)
In this code the AnnotationDate is not a date field but a text field. #Name is a variable equal to the selected value from the dlJobName list.
Here is my dilemma. When I make a selected_index_change event for the dlJobName, before I update the gridview that uses both of these drop down lists, I need to update the dlStage to only available values. What is happening is the dlStage list is not updated and the values in the drop down list is not available for the new JobName. Is there a way to force the query to run on the dlStage drop down list in conjunction with the selected index change event? This would cause the gridview to at least populate and not error out.
I can provide whatever additional code is necessary.
I think you have not set AutoPostBack="true" of drop down list. If Problem continues please put your HTML and cs code for proper help.
Hi maybe like other user comment you need go to property from dropdownlist, search AutoPostBack and change False for True. Don't forget clean firsr your dropdownlist
DropDownList.Items.Clear() (VB.NET)
DropDownList.Items.Clear(); (C#)
After clean your dropdownlist charge again your method to bind dropdownlist
set the property
dlJobName.AutoPostBack="true"
This will makes the SelectedIndexEvent to fired when the selected index of the main dropdown is changed.
In that selectedIndexChanged Event load the second DropdownList.
Here is an example,
http://asp-net-example.blogspot.in/2009/03/how-to-use-dropdownlist-autopostback.html
Hope your problem solved, if still exists kindly paste your snippet.

Casting a control to another page

I have tried everything to no avail. I need to cast the value from a user selected dropdownlist to another dropdownlist on another page. The data in the textboxes are a series of numbers.
Also is there a way to display the value in the second dropdown list as well as other values? For example, my dropdown has the values 1-6, user selects 4, which displays first in the dropdown on page 2 but the other values also display in case they want to change there selection??
If Not Page.PreviousPage Is Nothing Then
Dim table As Control = PreviousPage.Controls(0).FindControl("table1")
Dim ddl As DropDownList = CType(table.FindControl("ddlB_Codes"),c DropDownList)
If Not ddl Is Nothing Then
ddl_OC.DataSource = ddl.SelectedValue.Substring(0, 6)
End If
End If
Quick update I FINALLY got my casting to work through a session, only now I need to know how to display the session values and the other additional values for my drop box in case the user wants to change something? Thanks for the help
ddl_OC.DataSource = CType(Session.Item("valCodes"), String)
ddl_OC.DataBind()
ddl_SO.DataSource = CType(Session.Item("valAccts"), String)
ddl_SO.DataBind()
Use event selectedvaluechanged to set datasource for another DropDownList, when first one is changed.
Don't forget to populate it vin DataBind function usage.

DevExpress RowUpdating old values returning text value

I have a DevExpress ASPXGridView which has been bound with a dataset.
I'm using the edit row command to edit the data in the row and return it to the database.
When the edit button is clicked, the row opens up and there is an ASPXComboBox that is bound to another dataset. The TextField and the ValueField have both been properly set in the ASPXComboBox and are displaying data correctly. The data is as shown
List item
List item
When e.NewValues("") is called the integer value field i.e 1 is returned. However, when e.OldValues("") the TextField is returned i.e. "List Item"
Is there a way of returning the the integer value when calling e.Oldvalues without having to make another call to the database?
Thanks

Setting value of checkbox programmatically in VB.NET

I can check the value of a checkbox in a GridViewRow:
isChecked = CType(row.FindControl("chkSelect"), CheckBox).Checked
But what's baking my noodle is trying to figure out how to programmatically set a checkbox to checked.
The scenario is I have some rows in a GridView that are associated to another value in a dropdown. So, when I select the value in the dropdown, I'd like the checkboxes in the GridViewRows that are associated with that value to be already checked.
Problem: The check value is not persisted in the database. There's no field for it. The checkbox on the GridViewRows is an ASP TemplateField.
So I iterate through the rows and would like to check whichever checkboxes I need to based on whatever condition.
Hope I was sufficiently clear!
You should be able to do it like this
CType(row.FindControl("chkSelect"), CheckBox).Checked = True
or
CType(row.Cells(index).Controls(controlIndex), CheckBox).Checked = True
Also, see the following article for more information.

RadioButton list in GridView - 2 way databinding possible?

If I have an ASP.Net RadioButtonList within a TemplateField in a GridView, which is tied to a Data Control, I can get databinding on page loading like so:
<asp:RadioButtonList SelectedValue='<%#Eval("RequirementOption")%>'>
So when the gridview renders, the user can select an option for each row in the grid.
What I'm wondering is, when the save button is pressed and a postback occurs, is there a way to automatically push these selected values back through to the datacontrol so they can be saved, or do I have to manually loop through all the grid rows and exract the chosen value, like so:
For iRow As Integer = 0 To Me.myGrid.Rows.Count - 1
Dim selectedValue As String = CType(Me.myGrid.Rows(iRow).FindControl("RequirementOption"), RadioButtonList).SelectedValue
'...Load up the appropriate class and save the selected value
Next
Can it be done automatically via databinding?
NOTE: I am talking about about updating all the rows and then accessing all of them opn postback, not one row at a time via inline editing.
Yes, the selectedValue will be available as long as you specify Bind instead of Eval.
(assuming you're updating a record in the usual way)

Resources