Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Get events from dropdownlist in DataGrid

  Asked By: Tracey    Date: Oct 16    Category: Asp.net    Views: 2899
  

Sounds maybe stupid but how can i interrupt on eventchanging on that drop downlist...

I've 3 columns into my DataGrid. When the value of the Dropdownlist in the 2nd Column change, i want his value been written in the 3rd column...

I guess the faults are in the declaration of the sub DDL_Changer or in the attribute onSelectedIndexChanged, which is not the good one...

Someone knows how to deal with this?


Public Sub DDL_Changer(ByVal objSource As Object, ByVal ObjArgs As DataGridCommandEventArgs) => not same type as needed for the onselectindexChanged...

Dim myDropdown As DropDownList = CType(ObjArgs.Item.FindControl("myDDL"), DropDownList)
Dim value As String
value = myDropdown.SelectedItem.Text
Dim index As Integer
index = myDropdown.SelectedIndex
DataGrid1.Items.Cells(index).Text = value => this doesn't go

End Sub





<Datagrid id=DataGrid1>
<Columns>
<asp:BoundColumn DataField="aantal" HeaderText="nummer" />
<asp:TemplateColumn headertext="aantal" >
<ItemTemplate >
<asp:DropDownList Runat="server" OnSelectedIndexChanged="DDL_Changer" ID="myDDL" /> => this doesn't go, how do i have to do it?????
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="totaal">
<ItemTemplate>
<asp:Label Runat="server" ID="lineTotal" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</DataGrid>

Share: 

 

5 Answers Found

 
Answer #1    Answered By: Deloris Harris     Answered On: Oct 16

This is a kind of testing application (i'm implementing an shopping caddy (testing out the + and - of ASP.NET for thesis))
The second column  is filled with dropdownlists (has to become the selector for the numbers of articles in a caddy)
I want the selected value in the row of the datagrid, been written  in the same row in the third column (has to become later number X price = total price...)
I succeeded to add the dropdownlist(txs to D Nelson!) but not handle the events...
I've also tried to work with that signature, but i don't know how to find the correct index  of the datagrid  that way?

 
Answer #2    Answered By: Luisa Fischer     Answered On: Oct 16

ok I got it now...
The way you can do this is a mix of what you have been doing. I will write you some steps you should follow to achieve this.

1. set the autpostback to true for the ddl.
2. You don't have to use the ddl_onselectedindexchange event handler because like you say you don't get access to the current row of the datagrid
3. set the onItemDataBound event handler of the datagrid. This event raises every time a row of the datasource is bounded to the datagrid
4. hook to that event in the code behind. This is the signature for that event
DataGrid_ItemDataBound(sender as Object, e as System.Web.UI.WebControls.DataGridItemEventArgs)

5. Note the second one (DataGridItemEventArgs) this one will provide the item  being bounded. So what you should do here is get the ddl with FindControl and use the selected value of it to write to the third column  of the dg. i.e.


Dim myDropdown As DropDownList = CType(e.Item.FindControl("myDDL"), DropDownList)

Dim value As String
value = myDropdown.SelectedItem.Text
e.Item.Cells(3).Text = value

I assume here that the third column is the index  3, maybe it's not there because you use another column hidden or whatever

 
Answer #3    Answered By: Eshe Chalthoum     Answered On: Oct 16

it still doesn't work, maybe because i was already using the onItemDataBound for inserting the dropdownlists into the datagrid...
When page loads, i get in the 3rd columns  "1" => normal, 1 is default checked value
But when i change  a value in a dropdown, all values in the 3rd column  disappear...

Do you see what i'm doing wrong?
Has it smth to do with the IsNothing(drop1)?

 
Answer #4    Answered By: Waggoner Fischer     Answered On: Oct 16

Well... it's not so easy as it seemed to be
It requires a little bit of hack...
Here we go
The problem you had is that you cannot access to the selecteditem in the itemdatabound event... I thought this was possible but it seems to not
So to fix this, first, add the OnSelectedIndexChanged handler to the ddl (this is the one who see the value of the selected item)
Now change  the onitemdatabound with the old one you had:


Public Sub eventItemDataBound1(ByVal S As Object, ByVal objArgs As DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim drop1 As DropDownList = objArgs.Item.FindControl("myDDL")
Dim myDataSet As DataSet
Dim myDataTable As DataTable

myDataSet = CType(Session("data"), DataSet)
myDataTable = myDataSet.Tables("Tabel")

Dim myDataView As DataView

myDataView = New DataView(myDataTable)
drop1.DataSource = myDataView
drop1.DataMember = "Tabel"
drop1.DataTextField = "aantal"
drop1.DataValueField = "aantal"
drop1.DataBind()
' add this to initialize the third column  with the value 1 (or write it derectly on the aspx)
e.Item.Cells[2].Text = 1;


 end  If
End Sub

And finally write this to the onselectedindexchanegd handler
Public Sub ddl_change(object sender, System.EventArgs e)
Dim ddl as DropDownList = Ctype(sender, DropDownList)
' get the datagrid  item being used bounded. Note that is the parent of the parent of the ddl
Dim dgi As DataGridItem = Ctype(ddl.Parent.Parent, DataGridItem)
dgi.Cells[2].Text = ddl.SelectedItem.Value
End sub

this is a hack but works... the problem is that teh databound event is being fired before the ddl change. If not you could store the selected item  in a public var and use it in the databound event

 
Answer #5    Answered By: Shannon Hughes     Answered On: Oct 16

first of all if you want to raise the onselectedindexchanged event you have to set the autopostback property of the dll to true. Secondly, this is the correct signature for that event of the ddl
DDL_Changer(object sender, System.EventArgs e)

Also, I've seen your code and maybe this is not exactly what you want... Explain a bit what you want to do with that ddl

 
Didn't find what you were looking for? Find more on Get events from dropdownlist in DataGrid Or get search suggestion and latest updates.




Tagged: