 |
| ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 29th, 2004, 06:43 PM
|
|
Authorized User
|
|
Join Date: Jan 2004
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
populating a drop down lists in a datagrid
Me again!!!! :D how are you all?!
Yet again I come cap in hand looking for more help - in the words of Oliver Twist (ok, Charles Dickens if you want to be pedantic): "please sir, can i have some more?!"
I have created a datagrid which contains two bound Columns and the a template column, into which I have placed a drop down list which selects values from my database. The problem lies in populating this dropdown list. When I am referencing it from the vb.aspx page, it wont recoginse the name I have given it. I have tried various things to try and find my way around this such as putting the datagrid name before the name of the list etc. Tried to make it a child object and a few other things, but they didnt want to work either.
I appreciate that I could hard code the values into the aspx page using the following tag, <asp:ListItem>, but I would be keen to avoid this. So anyone got any ideas please?
many thanks,
Morris
|
|

February 29th, 2004, 07:54 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
you can got the list in the itemdataBound event for the grid
try to get the control using findcontrol like
If e.Item.ItemIndex = -1 And e.Item.Cells(4).HasControls Then
CType(e.Item.Cells(4).FindControl("DropDownList1") , DropDownList).datasource=createdatasource
CType(e.Item.Cells(4).FindControl("DropDownList1") , DropDownList).DataTextField="Name"
CType(e.Item.Cells(4).FindControl("DropDownList1") , DropDownList).DatavalueField="value"
End If
Ahmed Ali
Software Developer
|
|

March 1st, 2004, 12:46 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Alternatively, you could create a protected datasource on the page and use databinding syntax within the markup for the dropdownlist in the datagrid template to automatically bind the dropdownlist for each datagrid item...
<asp:dropdownlist Runat="server" ID="ddlList"
DataSource="<%# myProtectedDataSource%>"
DataTextField="datasource property/field for text of option"
DataValueField="datasource property/field for value of option" />
Create the datagrid's datasource once as a class scope protected member, and populate the datasource once in page load (based on postback). This same datasource will be used by every member DDL of datagrid. The datasource can be any datasource you would normally use: DataSet, DataTable, object collection, arraylist, etc.
The harder part of this is preselecting the DDL item (if you need to). You'll have to do this in the ItemDataBound handler:
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim objDDL As DropDownList = CType(e.Item.FindControl("ddlList"), DropDownList)
objDDL.SelectedIndex = objDDL.Items.IndexOf(objDDL.Items.FindByValue(<dataitem value>))
End If
You'll need to replace <dataitem value> with a conversion to the appropriate dataitem object that the griditem is bound to (like a table row). Then you need to get the value. The source object for the conversion is e.Item.DataItem which returns the dataitem that the current datagrid item is bound to.
For example...
CType(e.Item.DataItem, System.Data.DataRow).Item("myColumn").ToString
Peter
------------------------------------------------------
Work smarter, not harder.
|
|

March 1st, 2004, 01:12 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
|
quote:<asp:dropdownlist Runat="server" ID="ddlList"
|
Quote:
DataSource="<%# myProtectedDataSource%>"
DataTextField="datasource property/field for text of option"
DataValueField="datasource property/field for value of option" />
|
i have a question from where i can get this source
"<%# myProtectedDataSource%>"
and how i can create????
Ahmed Ali
Software Developer
|
|

March 1st, 2004, 02:51 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
You must create this data source in your codebehind. The member must be class scope:
Public Class MyASPXPage
...
Protected myDataSet As DataSet
or
Protected myArrayList As ArrayList
or
...
End Class
Then you populate that data source in Page_Load(). The ASPX page runs in the context of a class inherited from the code behind class so it can access this datasource by its simple name. Then you need only fill in the necessary attributes of the list control (datagrid or other) to work with that data source. If it's a DataSet you might need to specify the table name in the DataMember attribute.
Peter
------------------------------------------------------
Work smarter, not harder.
|
|

March 6th, 2004, 09:17 PM
|
|
Authorized User
|
|
Join Date: Jan 2004
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Good day,
I think wrox should create a forum just for me at the moment with the amount of posts I am making at the moment. I appologise for cluttering the place up.
At the moment,I am geting this error at the moment: 'Object reference not set to an instance of an object' - do anyone know anything about it?
I following the advice from given earlier the datagrids are now bound to a data source, but i an other problem. Trying to get the value out of the lists is proving to a little bit on the hard side.
In a previous post i was working with checkboxes etc and I assumed that the code should be similar when extracting values from a dropdownlist as it would be for extracting values from a text box etc. However, I am getting the following error: 'System.NullReferenceException: Object reference not set to an instance of an object.' when I try to run the code. The code is below:
Dim ddlSelected As System.Web.UI.WebControls.DropDownList
Dim ddCompValue As DataGridItem
ddlSelected = ddCompValue.FindControl("ddCompValue")
For Each ddCompValue In dgEmps.Items
'get primary key of the data grid
compID = Convert.ToInt32(dgEmps.DataKeys(ddCompValue.ItemIn dex).ToString())
Dim rating As Integer = Convert.ToInt32(ddCompValue.ItemIndex.ToString)
strSQL_rating = "INSERT INTO EMP_EVALUATION ( ECR_EMPLOYEE_ID, ECR_EVALUATION_DT, ECR_COMPETENCY_ID, ECR_PROFICENCY_LVL_CD ) Vaules (" & emp_id & "," & evaldt & "," & compID & "," & rating & ");"
Dim ratingCmd As OleDbCommand = New OleDbCommand(strSQL_rating, dbConn)
ratingCmd.CommandType = CommandType.Text
'Execute the new rating query
ratingCmd.ExecuteNonQuery()
Next
the code is falling over at the following line: ddlSelected = ddCompValue.FindControl("ddCompValue") and I have no idea why - surely when working with datagrid item, the procedure should be the same for all the others?
As always - all help gratefully recieved!
Morris
|
|

March 7th, 2004, 05:37 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
Don't worry about "cluttering up" this forum. It's made for this, isn't it? You could, however, decide to create a new thread for a new topic, as this will make things a little clearer.
It seems to make sense you get that error. Take a look at this:
Dim ddCompValue As DataGridItem
ddlSelected = ddCompValue.FindControl("ddCompValue")
For Each ddCompValue In dgEmps.Items
As you can see, you declare a DataGridItem in the first line. The next line, then, tries to use the FindControl method of the DataGridItem. However, the DataGridItem isn't an object yet; all it is, is a variable declaration.
I think your DataGridItem should be retrieved from the Items collection of the DataGrid inside a loop, just as you did with the check boxes. If you get each DataGridItem from the DataGrid, then inside the For Each loop, the DataGridItem will be a real instance, so you can use its FindControl method to get at the drop down list.
Does this help?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

March 7th, 2004, 07:48 PM
|
|
Authorized User
|
|
Join Date: Jan 2004
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
yeah, that worked.
I just wasnt paying attention to what I was doing - oops!
Thanks
Morris
|
|

March 18th, 2004, 07:42 PM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
for those who are interested in seeing how to bind a drop down list to a datasource for ASP.NET this is a bit of C# code behind that will do it.
public void DataGrid1_DataBound(Object sender, DataGridItemEventArgs e)
{
if ((DataGrid1.EditItemIndex > -1)&&(e.Item.Cells[1].HasControls()))
{
Control myControl1 = e.Item.Cells[1].FindControl("ddlInspectors");
if(myControl1!=null)
{
DropDownList ddlInspectors = (DropDownList) e.Item.Cells[1].FindControl("ddlInspectors");
ddlInspectors.DataTextField = "ASPECTID";
ddlInspectors.DataValueField = "ASPECTID";
ddlInspectors.DataSource = ds.Tables[1].DefaultView;
ddlInspectors.DataBind();
}
}
}
this is how you can get the selected value back from the datagrid's code behind updatd command:
public void DataGrid1_Update(Object sender, DataGridCommandEventArgs e)
{
Control myControl1 = e.Item.Cells[1].FindControl("ddlInspectors");
if(myControl1!=null)
{
DropDownList ddlInspectors = (DropDownList) e.Item.Cells[1].FindControl("ddlInspectors");
Response.Write(ddlInspectors.SelectedValue.ToStrin g());
}
}
|
|

September 29th, 2004, 01:50 AM
|
|
Registered User
|
|
Join Date: Sep 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
I have two item templates with two dropdown. On selection of the first dropdown , i need to select the second dropdown. If any links are samples available let me know.
Reply to : [email protected]
Regards,
Govind.
Quote:
quote:Originally posted by planoie
Alternatively, you could create a protected datasource on the page and use databinding syntax within the markup for the dropdownlist in the datagrid template to automatically bind the dropdownlist for each datagrid item...
<asp:dropdownlist Runat="server" ID="ddlList"
DataSource="<%# myProtectedDataSource%>"
DataTextField="datasource property/field for text of option"
DataValueField="datasource property/field for value of option" />
Create the datagrid's datasource once as a class scope protected member, and populate the datasource once in page load (based on postback). This same datasource will be used by every member DDL of datagrid. The datasource can be any datasource you would normally use: DataSet, DataTable, object collection, arraylist, etc.
The harder part of this is preselecting the DDL item (if you need to). You'll have to do this in the ItemDataBound handler:
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim objDDL As DropDownList = CType(e.Item.FindControl("ddlList"), DropDownList)
objDDL.SelectedIndex = objDDL.Items.IndexOf(objDDL.Items.FindByValue(<dataitem value>))
End If
You'll need to replace <dataitem value> with a conversion to the appropriate dataitem object that the griditem is bound to (like a table row). Then you need to get the value. The source object for the conversion is e.Item.DataItem which returns the dataitem that the current datagrid item is bound to.
For example...
CType(e.Item.DataItem, System.Data.DataRow).Item("myColumn").ToString
Peter
------------------------------------------------------
Work smarter, not harder.
|
|
|
 |