Showing posts with label items. Show all posts
Showing posts with label items. Show all posts

Monday, March 26, 2012

Populate a DropDownList

I need to populate a dropdownlist with items form one SQL Table in SqlDataSource1 but the selected value and the data text need to match the ones that are already into another SQL Table in SqlDataSource2.

Any Sugestions?

You can manually compare the data between the tables in codebehind using the classes found in System.Data.SqlClient & System.Data.Sql. Then you can use the DropDownList.Items.Add(new ListItem()) methods to add new items to your dropdownlist.

Saturday, March 24, 2012

Populate dropdown

Hy Guys,

I have a datagrid that, in the edititem mode, have a dropdownlist. This
dropdownlist have to be populated with the items of another dropdownlist
that is in the same page.

How can I do it??

Thank'sthis is what you need

http://msdn.microsoft.com/library/d...stomcolumns.asp

--
Regards,

HD

"Andr Almeida Maldonado" <deweb@.bol.com.br> wrote in message
news:ebVFaqi2DHA.2428@.tk2msftngp13.phx.gbl...
> Hy Guys,
> I have a datagrid that, in the edititem mode, have a dropdownlist. This
> dropdownlist have to be populated with the items of another dropdownlist
> that is in the same page.
> How can I do it??
> Thank's

populate dropdownlist

I have a list which I am trying to populate from my db, I can my items but not my unique id, my code is below

_cmd.CommandText = "SELECT workgroupID, workGroupname FROM dbo.workgroup"
_dr = _cmd.ExecuteReader



While _dr.Read
workgroupSelect.Items.Add(_dr("workgroupname"))
workgroupSelect.DataValueField = _dr("workgroupid")
End While


How do I get my id field into the list so that I know which one was selected??

How do I know which one was selected?

Grateful for any help.

Cheersok, I now have
With workgroupSelect
.DataSource = _dr
.DataTextField = "workgroupname"
.DataValueField = "workgroupid"
.DataBind()
.Items.Insert(0, "<- Select a Work Group ->")
End With

so all I need now is to know is the workgroupId of the one selected??

Cheers
try :

workgroupSelect.SelectedItem.Value
I have tried that,

I have ..

Protected Sub workgroupSelect_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles workgroupSelect.SelectedIndexChanged
Dim strselected As String
strselected = workgroupSelect.SelectedValue.ToString
Label1.Text = strselected
End Sub

but the only I can get the label text to show the value is to set the dropdowncontrol to postback = true
is there a way around this??
is the value actually changing even if I cant see it in the label??

Thanks
The value would change but there is no way to see it refreshed unless the postback is activated. Such is web development :sick:

populate listbox using Javascript

I tried following :
document.getElementById("lsTest").Items.Add(new ListItem("Test"))
This gives following error message:
'document.getElementById(...).Items' is null or not an object
Also tried:
document.getElementById("lsTest").Add(new ListItem("Test"))
Result - Error Message:
'listitem' is undefined
Any idea?var o = document.createElement("option");
o.text = "test"
o.value = "test";
document.getElementById("lsTest").add(o);
-- bruce (sqlwork.com)
"RA" <rchaudhary-nospam@.storis.com> wrote in message
news:%23IOtEyPyEHA.2016@.TK2MSFTNGP15.phx.gbl...
| I tried following :
| document.getElementById("lsTest").Items.Add(new ListItem("Test"))
|
| This gives following error message:
| 'document.getElementById(...).Items' is null or not an object
|
| Also tried:
| document.getElementById("lsTest").Add(new ListItem("Test"))
| Result - Error Message:
| 'listitem' is undefined
|
| Any idea?
|
|
Hi RA,
you can not add a listitem from javascript. A ListBox is rendered as
html SELECT element. So, you should add the items in javascript as if you
are adding to a SELECT element, which would be like follows
document.getElementById("lsTest").add(new Option("Text","Value"));
HTH
Kumar
"RA" <rchaudhary-nospam@.storis.com> wrote in message
news:%23IOtEyPyEHA.2016@.TK2MSFTNGP15.phx.gbl...
> I tried following :
> document.getElementById("lsTest").Items.Add(new ListItem("Test"))
> This gives following error message:
> 'document.getElementById(...).Items' is null or not an object
> Also tried:
> document.getElementById("lsTest").Add(new ListItem("Test"))
> Result - Error Message:
> 'listitem' is undefined
> Any idea?
>
To remove an item from a textbox I tried;
document.getElementById("lsTest").remove(new Option("Testing","Testing"));
and
document.getElementById("lsTest").remove("Testing");
neither of above work. Both deletes very first Item in the listbox.
What I am doing wrong?
"Kumar Reddi" <KumarReddi@.REMOVETHIS.gmail.com> wrote in message
news:Odz5c8PyEHA.3096@.tk2msftngp13.phx.gbl...
> Hi RA,
> you can not add a listitem from javascript. A ListBox is rendered as
> html SELECT element. So, you should add the items in javascript as if you
> are adding to a SELECT element, which would be like follows
> document.getElementById("lsTest").add(new Option("Text","Value"));
> HTH
> Kumar
> "RA" <rchaudhary-nospam@.storis.com> wrote in message
> news:%23IOtEyPyEHA.2016@.TK2MSFTNGP15.phx.gbl...
>

populate listbox using Javascript

I tried following :
document.getElementById("lsTest").Items.Add(new ListItem("Test"))

This gives following error message:
'document.getElementById(...).Items' is null or not an object

Also tried:
document.getElementById("lsTest").Add(new ListItem("Test"))
Result - Error Message:
'listitem' is undefined

Any idea?var o = document.createElement("option");
o.text = "test"
o.value = "test";
document.getElementById("lsTest").add(o);

-- bruce (sqlwork.com)

"RA" <rchaudhary-nospam@.storis.com> wrote in message
news:%23IOtEyPyEHA.2016@.TK2MSFTNGP15.phx.gbl...
| I tried following :
| document.getElementById("lsTest").Items.Add(new ListItem("Test"))
|
| This gives following error message:
| 'document.getElementById(...).Items' is null or not an object
|
| Also tried:
| document.getElementById("lsTest").Add(new ListItem("Test"))
| Result - Error Message:
| 'listitem' is undefined
|
| Any idea?
|
|
Hi RA,
you can not add a listitem from javascript. A ListBox is rendered as
html SELECT element. So, you should add the items in javascript as if you
are adding to a SELECT element, which would be like follows

document.getElementById("lsTest").add(new Option("Text","Value"));

HTH
Kumar

"RA" <rchaudhary-nospam@.storis.com> wrote in message
news:%23IOtEyPyEHA.2016@.TK2MSFTNGP15.phx.gbl...
> I tried following :
> document.getElementById("lsTest").Items.Add(new ListItem("Test"))
> This gives following error message:
> 'document.getElementById(...).Items' is null or not an object
> Also tried:
> document.getElementById("lsTest").Add(new ListItem("Test"))
> Result - Error Message:
> 'listitem' is undefined
> Any idea?
To remove an item from a textbox I tried;

document.getElementById("lsTest").remove(new Option("Testing","Testing"));
and
document.getElementById("lsTest").remove("Testing");

neither of above work. Both deletes very first Item in the listbox.

What I am doing wrong?

"Kumar Reddi" <KumarReddi@.REMOVETHIS.gmail.com> wrote in message
news:Odz5c8PyEHA.3096@.tk2msftngp13.phx.gbl...
> Hi RA,
> you can not add a listitem from javascript. A ListBox is rendered as
> html SELECT element. So, you should add the items in javascript as if you
> are adding to a SELECT element, which would be like follows
> document.getElementById("lsTest").add(new Option("Text","Value"));
> HTH
> Kumar
> "RA" <rchaudhary-nospam@.storis.com> wrote in message
> news:%23IOtEyPyEHA.2016@.TK2MSFTNGP15.phx.gbl...
>> I tried following :
>> document.getElementById("lsTest").Items.Add(new ListItem("Test"))
>>
>> This gives following error message:
>> 'document.getElementById(...).Items' is null or not an object
>>
>> Also tried:
>> document.getElementById("lsTest").Add(new ListItem("Test"))
>> Result - Error Message:
>> 'listitem' is undefined
>>
>> Any idea?
>>
>>

Wednesday, March 21, 2012

Populating a datagrid bound list view by a query

Hi,
My question today is below is my code which binds a listview to a databound column in my datagrid, but at the moment the list items are hard coded in. What I actually want is for the listitems to be populated from a lookup style query so possible from a data reader bringing back the ID which will be stored in the bound column but so the user just sees the Text associated with the value. Hope that makes sense.

<asp:TemplateColumn HeaderText="Model">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.FK_StockModelID") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropModel" Runat="server">
<asp:ListItem Value="1" text="1"></asp:ListItem>
<asp:ListItem Value="2" text="2"></asp:ListItem>
<asp:ListItem Value="3" text="3"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ControlToValidate="DropModel" text="*" Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>

I think I can pretty much suss this out if I can work out how to access the dropdown list from tthe codebehind. So if anyone knows that as a starting point, please enlighten me.

PS sorry about the smilies in the code the ASP syntax just happent to be the same code.http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/creatingcustomcolumns.asp
Ok I found this code at the above link but Im struggling to use it, can anyone explain it line by line or tell me how to make it reference my template item shown above.
Private Sub dgNewCarriageBuildSheets_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgNewCarriageBuildSheets.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim Cu As String = DRV("ShipVia")
Dim DDL As DropDownList = CType(e.Item.Cells(4).Controls(1), DropDownList)()
Dim SQL As String = _
"SELECT ShipperID, CompanyName FROM Shippers ORDER BY ShipperID"
Dim DA As SqlDataAdapter = New SqlDataAdapter(SQL, ConnStr)
Dim DS As New DataSet
Dim item As ListItem
DA.Fill(DS, "Shippers")
DDL.DataSource = DS.Tables("Shippers").DefaultView
DDL.DataTextField = "CompanyName"
DDL.DataValueField = "ShipperID"
DDL.DataBind()
item = DDL.Items.FindByValue(CurrentShip)
If Not item Is Nothing Then item.Selected = True
End If
End Sub
Yes, you'd do it in the ItemDataBound event. After checking for the ItemType, declare a variable of type Combobox. Set it to e.Item.Cells(x).FindControl(your combobox id), and set the value of that combobox to the value you want from the dataset. You can use e.Item.ItemIndex to get the row number in the repeater/grid/dataset.
Private Sub dgNewCarriageBuildSheets_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgNewCarriageBuildSheets.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim cboModel As DropDownList
Dim cboModel As DropDownList = e.Item.Cells(FindControl(DropModel))
'Also I have put DopModel as the ID of the Dropdown list in the item template but it doesnt seem to be recognised? Name DropModel is not declared

End If
End Sub
Fixed

Dim cboModel As DropDownList = e.Item.Cells(11).FindControl("DropModel")

Now to try the next bit...
Nearly there, I am now just getting an error at run time on the line underlined saying cannot convert dbnull to string?
Private Sub dgNewCarriageBuildSheets_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgNewCarriageBuildSheets.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim lstItem As ListItem
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim CurrentModel As String = DRV("FK_StockModelID")
Dim cboModel As DropDownList = e.Item.Cells(11).FindControl("DropModel")
Main.objCarriageBuildSheet.GetStockModels() 'Populates a dataset
cboModel.DataSource = Main.objCarriageBuildSheet.dds_StockModels.dt_StockModels
cboModel.DataTextField = "Model"
cboModel.DataValueField = "FK_StockModelID"
cboModel.DataBind()
lstItem = cboModel.Items.FindByValue(CurrentModel)
If Not lstItem Is Nothing Then lstItem.Selected = True
End If
End Sub
DataBinder.Eval(e.Item.DataItem, "FK_StockModelID")
Like this ?
If e.Item.ItemType = ListItemType.EditItem Then

Dim lstItem As ListItem
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim CurrentModel As String = DataBinder.Eval(e.Item.DataItem, "FK_StockModelID") 'DRV("FK_StockModelID")
Dim cboModel As DropDownList = e.Item.Cells(11).FindControl("DropModel")

Main.objCarriageBuildSheet.GetStockModels() 'Populates a dataset
cboModel.DataSource = Main.objCarriageBuildSheet.dds_StockModels.dt_StockModels
cboModel.DataTextField = "Model"
cboModel.DataValueField = "FK_StockModelID"
cboModel.DataBind()
lstItem = cboModel.Items.FindByValue(CurrentModel)

If Not lstItem Is Nothing Then lstItem.Selected = True

End If

I still get same error
Hmm... maybe before you call that line, you should check whether e.Item.DataItem evaluates to null... place a breakpoint there and have a look at it.
It evaluates to a system.data.datarowview object
Hi Mendhak, I have finally solved the problem a different way.

My HTML code looks like this now, the main difference is I have set the datasource to a function in my code behind page.
<asp:TemplateColumn HeaderText="Model">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.FK_StockModelID") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=DropModel Runat="server" DataValueField="ModelID" DataTextField="BrandModel" DataSource="<%# PopulateList %>" >
</asp:DropDownList>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" Runat="server" text="*" ControlToValidate="DropModel"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateColumn>

The function simply returns a previously filled dataset.
Public Function PopulateList() As DataSet

Return Main.objCarriageBuildSheet.dds_StockModels
End Function

this seems to be working nicely.

Friday, March 16, 2012

Populating Drop Down List

Hi,

I have a drop down list which is bound to a database field and is populated from so. However I want to add 1 of my own items to the drop down list no matter what is in the database. Is this possible?

ThanksYes. You can use the .Items.Add or .Items.Insert to add items to the dropdownlist. Just be sure to do this after you bind your data, because the bind will remove all items from the list.

hope this helps,
sivilian
Thanks very much, solved my problem.
Well the method is working but how we can set the additional item at the top, for example set "Select one State" on top of my drop down.

Thanks
Hello, try that in the page_load:

ddl.Items.Insert(new ListItem("text",value"),0);

regards
Thanks for you time,

Overload resolution failed because no accessible 'Insert' can be called with these arguments:

the aforsaid error return on line

AltShippingAdd.Items.Insert(new ListItem("Carbon", "C"),0);

Thanks in Advance
Hello,

this working fine, i think there is some problem in syntex.

AltShippingAdd.Items.Insert(0, New ListItem("Select One"))

the above statement is working fine but we can't able to define the value of that item is there a way to define the value of this item.

Thanks.
oh! in advance.
Hello, maybe you are right, I messed up with the syntax. but try to have:

AltShippingAdd.Items.Inert(0,new ListItem("Select One","1"))

regards
Thanks Bilal
It works fine
I am glad I was able to help you out my friend. For any further help, please dont hesitate to contact me here at the forums.

regards