DropDownList Data Binding To List <String> Not Working?
Oct 22, 2010
I'm trying to bind a List<String> to a DropDownList in a user control. I think I'm doing the right thing, but it seems that after my code executes the bindings are cleared. Here's the code for review!
User control:
<asp:DropDownList ID="subjectNameDropDown" runat="server"/>
<asp:DropDownList ID="yearLevelDropDown" runat="server"/>
Auto-generated designed code-behind:
public partial class NewSiteMetadataUserControl {
protected global::System.Web.UI.WebControls.DropDownList subjectNameDropDown;
protected global::System.Web.UI.WebControls.DropDownList yearLevelDropDown;
}
Code-behind:
public partial class NewSiteMetadataUserControl : UserControl
{
protected override void CreateChildControls()
{
subjectNameDropDown = new DropDownList();
yearLevelDropDown = new DropDownList();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
EnsureChildControls();
// Attempt 1
List<String> subjectNames = GetSubjectValues();
foreach (var subjectName in subjectNames)
subjectNameDropDown.Items.Add(subjectName);
subjectNameDropDown.DataBind();
// Attempt 2
List<String> yearLevels = GetYearLevelValues();
yearLevelDropDown.DataSource = yearLevels;
yearLevelDropDown.DataBind();
}
}
Should this approach work? If it should, how can I debug what happens after the code executes?
View 2 Replies
Similar Messages:
Jul 14, 2010
I have a gridview where I can browse an user's information. Through the gridview I'm also able to edit some of this information. A recent change in my client's needs demands that one of these fields is now a DropDownList. This field represents a user's "schedule" (not literally a schedule but works like one), and the different schedule options to be assigned to a user are shown in the drop down list.
Problem: In the drop down list I want to show list items like "9-12 15-17", "10-14 15-18" so that it is easy for the administrator to tell which schedule he's assigning to an user. However, that shouldn't be their actual value. I need "9-12 15-17" to actually be '1' since this is the schedule ID that I'm trying to pass on to the DB.
Here's what I have so far:
[Code]....
And with this last method I change the selected item's value to the Index. Meaning that if I select "9-12 15-17" which has an Index of 1 in the dropDownList, the actual value is now 1. Again, this is the behavior I expect. My experience with ASP is quite little as I'm new on this, but as far as I know, this only actually updates the string array. How would I go about modifying this to reflect my updates on the database? All input is taken into account.
View 3 Replies
Feb 17, 2010
Plz go through my code-
<asp:ScriptManager runat="server"/>
<cc1:ModalPopupExtender ID="mpe_AddState" runat="server" PopupControlID="PanelAddState" TargetControlID="btnAddNew" BackgroundCssClass="ModalPopupBG" OkControlID="btnOk"
CancelControlID="btnCancel" BehaviorID="ModelPopUp_AddState">
</cc1:ModalPopupExtender>
<!-- Begin of Content table -->
<table width="100%" border="0" cellspacing="2" cellpadding="2" align="left">
<tr> <td>
<!-- Begin of Search Country Panel -->
<asp:Panel runat="server" DefaultButton="btnSearch">
<table border="0" cellspacing="3" cellpadding="3" align="left">
<tr> <td width="15%" >Search</td> <td width="30%">
<asp:TextBox runat="server" CssClass="textBox" ></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv_txtSearch" runat="server" ErrorMessage="Field can't be left Blank" SetFocusOnError="true" ControlToValidate="txtSearch" Display="None" ValidationGroup="search"> </asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender runat="server" TargetControlID="rfv_txtSearch">
</cc1:ValidatorCalloutExtender> </td> <td width="25%">
<asp:Button runat="server" Text="Search" ValidationGroup="search" />
</td> </tr> <tr> <td width="15%">Country</td> <td width="30%">
<asp:DropDownList id="ddlCountry" runat="server" AutoPostBack="true" Height="30px" Width="76px">
</asp:DropDownList> </td> <td width="15 %">
<asp:Button runat="server" Text="AddNew" /> </td>
<td width="25%"></td> </tr> <tr> <td> </td> </tr>
</table> </asp:Panel>
<!-- End of Search Country Panel -->
<!-- Begin of Add State Panel -->
<asp:Panel runat="server" CssClass="formTextBold"
style="width:390px; height:500px; cursor: move;" Height="278px" Width="335px">
<div> <table border="0" > <tr><td></td></tr> <tr bgcolor="skyblue">
<td colspan ="3" style ="font-weight :bold "> Enter The State Details
</td> <td align ="center"><a href="#" onclick ="HidePopup();">
<font color="red" size="+1">X</font> </a> </td> </tr> <tr> <td> </td> </tr> <tr>
<td> </td> <td> State Code </td> <td>
<asp:TextBox id="txtStateCode" runat="server"></asp:TextBox>
</td> </tr> <tr> <td> </td> <td >State Name</td> <td>
<asp:TextBox id="txtStateName" runat="server"></asp:TextBox>
</td> </tr> <tr> <td> </td> </tr>
<tr> <td> </td> </tr> <tr> <td> </td>
<td >Country Name</td> <td>
<asp:DropDownList id="ddlSelectCountry" runat="server">
</asp:DropDownList></td> <tr> <td> </td> </tr>
<tr> <td> </td> </tr> <tr>
<td width="28%" colspan="2"></td> <td >
<asp:Button runat="server" Text="Ok" Width="50px" />
<asp:Button runat="server" Text="Cancel" /> </td>
</tr> </tr> </table> </div> </asp:Panel>
<!-- End of Add Country Panel -->
.cs code is-
protected void Page_Load(object sender, EventArgs e) {
if(!IsPostBack) {
ddlSelectCountry.DataSource = Obj_BLogic.Country_List();
ddlSelectCountry.DataTextField = "Country_Name";
ddlSelectCountry.DataValueField = "Country_Id";
ddlSelectCountry.DataBind(); }
ddlCountry.DataSource = Obj_BLogic.Country_List();
ddlCountry.DataTextField = "Country_Name";
ddlCountry.DataValueField = "Country_Id"; ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("Select"));
grdState.Columns[0].Visible = false; }
I want list of countries exactly once in both the dropdownlist (ddlCountry & ddlSelectCountry). But is giving the list of countries twice.
View 1 Replies
Aug 11, 2010
How do I bind a result from a JSON string to a dropdownlist (I have 2 fields), "NAAM" should be DataTextField and "CODE" should be the DataValueField. Using ddl.Items.Add doesn't do the trick.
string:
{"NAAM":["Drenthe","Flevoland","Friesland","Gelderland","Groningen","Limburg","Noord Brabant","Noord Holland","Overijssel","Utrecht","Zeeland","Zuid Holland"],"CODE":["D","X","B","G","A","K","P","L","E","M","S","H"]}
code:
[Code]....
View 1 Replies
Sep 3, 2010
I have the following object:
CrossTabDataObject
{
string RowName{get;set;};
int RowId{get;set;}
List<string> CellContents = new List <string>();
//Constructor..... etc.
}
I'm building a dynamic crosstab grid using GridView in asp.net 3.5
I wish to bind to CellContents[0] for dynamic column 1, CellContents[1] for dynamic column 2 (dynamic column 0 is the RowName field from the CrossTabDataObject) etc. I am using:
object boundValueObj = null;
Control ctrl = (Control)sender;
IDataItemContainer dataItemContainer = (IDataItemContainer)ctrl.NamingContainer;
boundValueObj = DataBinder.Eval(dataItemContainer.DataItem, strSelectedID);
This code is in the InstantiateIn function of the gridview as I'm creating drop down list templates in each cell of the crosstab.
I have code (not shown) that sets strSelectedID depending on the column being created.
When strSelectedID is equal to "RowName" for dynamic column [0] the DataBinder.Eval function works fine and sets boundValueObj as expected. The problem comes when strSelectedID is set to "CellContents[n]" where n is the Column Index.
DataBinder.Eval only works with properties and fields of objects. How do I get around this?
View 1 Replies
Mar 28, 2011
Currently,below is my code.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source= E\SQLEXPRESS;" + "Initial Catalog=k;Integrated Security=SSPI");
SqlDataAdapter adapSel;
string mySQL = "select column_name from information_schema.columns where table_name='examtimetable' '" + dd_list + "'";....
I receive an error "Incorrect syntax near 'System.Web.UI.WebControls.DropDownList'."
View 3 Replies
Dec 12, 2010
I am using VB.NET and I can't compare the radio button lists selectedItem.Value to a string, it doesn't work...here is the code: (I have also tried selectedValue it does not work either)
Response.Write("RB1: " + rblOne.SelectedItem.Value + " FML FML FML<br/>")
If rblOne.SelectedItem.Value = "No" Then
Response.Write("Hey there!<BR/>")
pnlR1.Visible = True
If NumberOfAnswers = 7 Then
Score = Score - 10
ElseIf NumberOfAnswers = 6 Then
Score = Score - 15
Else
Score = Score - 20
End If
Response.Write("Score: " + Score.ToString)
End If
Response.End()
If rblOne.SelectedItem.Value = "No" Then is not working, notice the debug statements in there, here is the output: Why won't it evaluate that rblOne.SelectedItem.Value = "No" !?!?!? I tried rblOne.SelectedValue, that doesn't work, AND I added .ToString to both
View 1 Replies
Aug 4, 2010
one drop down list cointains all country list ,when we select thet particular country the other drop down list has to show all the states in that country ,this is what i am thinking to do ,can some one guide me how to do ,and give you give some reference links
View 3 Replies
Jan 12, 2011
I have a table Person (in my database) which has fields like PersonID, PersonName, PersonSurname, etc. What I'd like to do is to bind the PersonName + PersonSurname (e.g. Mark Black) as values for displaying the data in drop down list and PersonID as the data identifier.
As I'm still new with Entity Framework I'm not sure how to manage to do it.
View 2 Replies
Aug 24, 2010
private void AddDBDataToControl(DataSet app_Support,int count) {
int i = 0; while (count != 0) {
DateTime dt = Convert.ToDateTime(app_Support.Tables[0].Rows[i]["FromDate"].ToString());
//dt=23/08/2010 10:04:00 AM
DropDownList h1 = (DropDownList)GVDate.Rows[i].Cells[1].FindControl("txthour");
h1.SelectedValue = dt.Hour.ToString();
DropDownList m1 = (DropDownList)GVDate.Rows[i].Cells[2].FindControl("txtmin");
m1.SelectedValue = dt.Minute.ToString();
DropDownList h2 = (DropDownList)GVDate.Rows[i].Cells[5].FindControl("txthourto");
DateTime dt2 = Convert.ToDateTime(app_Support.Tables[0].Rows[i]["ToDate"].ToString());
//dt2=28/08/2010 14:25:00 PM
h2.SelectedValue = dt2.Hour.ToString();
DropDownList m2 = (DropDownList)GVDate.Rows[i].Cells[6].FindControl("txtminto");
m2.SelectedValue = dt2.Minute.ToString();
count--; i++; } }
When the function executes
h1.SelectedValue = "10";
and
m1.SelectedValue ="04";
but when it reaches to
count--
h1.SelectedValue takes the value "14"; rather than h1.SelectedValue = "10"; and
h2.SelectedValue = "14";
and
m1.SelectedValue ="25"; rather than m1.SelectedValue ="04"; and
m2.SelectedValue = "25"
View 4 Replies
Jan 15, 2011
Am trying to bind data to a dropdown list on pageload based on a condition. Code explains further below.
public partial class AddExhibit : System.Web.UI.Page {
string adminID, caseIDRetrieved;
DataSet caseDataSet = new DataSet();
SqlDataAdapter caseSqlDataAdapter = new SqlDataAdapter();
string strConn = WebConfigurationManager.ConnectionStrings["CMSSQL3ConnectionString1"].ConnectionString;
protected void Page_Load(object sender, EventArgs e) {
adminID = Request.QueryString["adminID"];
caseIDRetrieved = Request.QueryString["caseID"];
if (caseIDRetrieved != null) {
CaseIDDropDownList.Text = caseIDRetrieved;
//CaseIDDropDownList.Enabled = false;
} else { try { CreateDataSet();
DataView caseDataView = new DataView(caseDataSet.Tables[0]);
CaseIDDropDownList.DataSource = caseDataView;
CaseIDDropDownList.DataBind(); }
catch (Exception ex) {
string script = "<script>alert('" + ex.Message + "');</script>";
} } }
The CreateDataset method that is called in the if..else statement is contains the following code.
private void CreateDataSet() {
SqlConnection caseConnection = new SqlConnection(strConn);
caseSqlDataAdapter.SelectCommand = new SqlCommand("Select CaseID FROM Cases", caseConnection);
caseSqlDataAdapter.Fill(caseDataSet); }
However when I load the page and as usual the condition that is supposed to bid the data is met, the gridview decides to displays as follows. Is it me or its the Datagrid?
View 3 Replies
Dec 26, 2010
[Code]....
My Default Page
[Code]....
when it goes to databind it gives me error asDropDownList1 do not contain binding with ID
View 1 Replies
Dec 3, 2010
I have an array in my code behind page. say:
int[] Hour = new int[24];
I also have a dropdownlist on my aspx page. say:
<asp:dropdownlist id="ddlHour" runat="server">
I want to bind the value of array to the dropdownlist such that when I dropdown the list, I get to see the values from 0 to 24.
View 1 Replies
May 26, 2010
I have two tables. t1 and t2
t1 is related to t2 through Order Numbers
these order numbers in table t2 have many part numbers(and hence the need for a second table t2)
I can display t1 using gridview just fine
In that same gridview iI have added a template column containing a dropdownlist(to show partnumbers)
now how can I bind this dropdownlist to the order number so that when the page is loaded the user can click on the dropdownlist to see all the partnumbers associated with that order number.
View 3 Replies
Dec 23, 2013
I am using a dropdown list in gridview and populating it from database.However ,i am getting "object reference not set to an instance ..." error even though i have reference.I have two more similar drop down list and they are working perfectly.
I am uploading the options for the dropdownlist from a "tab delimited" text file for all the dropdown list.I am not getting where exactly the problem is.
<asp:TemplateField HeaderText="Property" HeaderStyle-Wrap="false">
<ItemTemplate>
<asp:Label ID="lblProperty" runat="server" Text='<%#Eval("Property")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblProperty" runat="server" Visible="false" Text='<%#Eval("Property") %>'></asp:Label>
<asp:DropDownList ID="ddlPropertyEdit" runat="server"></asp:DropDownList>
[code]...
View 1 Replies
Jul 7, 2010
I am not able bind data to the dropdownlist from database. following is code.
DataTable dtModule = BO.GetBizModel_DDL(ServiceID);
if (dtModule != null)
{
ddlBizModl.Items.Clear();
ddlBizModl.DataSource = dtModule;
ddlBizModl.DataTextField = "BIZ_MODL_NM";
ddlBizModl.DataValueField = "BIZ_MODL_ID";
ddlBizModl.DataBind();
}
dtModule.Dispose();
View 3 Replies
Nov 25, 2010
when i am selecting an item in dropdown list datasource is bind and after another selection it is bind again .but i want it bind only once.
View 4 Replies
Mar 9, 2010
I am binding same data Table to Combo box and List Box on the WindowsForm Load.
When I select a value in Combo box automatically that value is also getting selected in List Box and same thing when I select a value in List Box that value is also getting selected in Combo Box.
Selection should be only for that particular control, how can I achieve this ?
View 4 Replies
Nov 17, 2010
On page load, dropdownlist1 needs to be populated with a list of string objects in a specific table in a sql database. Everytime the selected item in the dropdownlist1 changes, dropdownlist2 needs to be populated with a different table from a database ( I don't know if it can be the same database or not). And it needs to be dynamic, so that everytime the value in dropdownlist1 is changed, the items in dropdownlist2 need to change. All this si being done in VB on a Web Form.
How would I go about doing this? What would the code look like?
View 2 Replies
Apr 26, 2013
i have given 28 states as a datasource to the dropdownlist..whenever the user enters with id the dropdownlist must populate with his statename not from the first state (A to Z) as it is stored in database.?is thr any option to give query as default value to the dropdown?
View 1 Replies
Feb 11, 2010
I'm having trouble binding my textbox to my dropdownlist. I got it to work but I don't think it is the correct way of doing it. I have my DDL bound to a datasource and populating everything correctly. However, I couldn't find a better way of binding my textbox to it besides using the FormView control and its counterpart, Templates (edit,insert,etc) It looks messy and i was hoping there was a better way of doing it. I've researched it online for awhile now but I couldn't understand how to implement for my situation.
My DDL has company names in it and the datasource has all the information from the table including what I'm trying to have my textbox pull (Address).
View 3 Replies
Sep 8, 2010
I have a Dropdownlist in an itemTemplate in a gridview. My gridView binds to dataTable1 (from which the selected value for dropdown comes from) and dropDown binds to dataTable2 (i need to get DataValueField and DataTextField from here).
[Code]....
View 3 Replies
Oct 7, 2010
I'm trying to bind a value to a dropdownlist. Here how the code looks like in the aspx page...Bind the textboxes below is not a problem...but its not working with the dropdownlist..
So basically, what i want is the value selected from the dropdownlist to be remembered after i press next button and back button..
[Code]....
View 5 Replies
Aug 9, 2010
Split off from [URL]
Getting records From DataBase using DataTale which contain 1000 Rows, then binding to GridView using Generic List. Like above Example
GridView1.DataSource= GetData();
GridView1.Binding();
Now i want to do search, sorting & Paging in the GridView with out Postback of the Page & with out hitting the DataBase when do Paging or searching in GridView.
View 3 Replies
Jan 20, 2011
I have a form in which I have a dropdown list and a list box.
This dropdownlist is populated by a column in thetable . This drop down list may have values ranging from 1 to 5.
This values in the dropdownlist are to be made the caption headings of the list view control.
For insance if there are three values in the dropdown there should be three columns in the list box with the heading as the values of the dropdownlist.for 5 it should be 5.
Also there are two additional columns which would be constant.
View 6 Replies