|
-
Feb 18th, 2004, 07:40 AM
#1
Thread Starter
Frenzied Member
Whats wrong wiht this code? Invalid cast
VB Code:
Dim chkBox As New CheckBox
Dim oOrder As New COrder
For Each di As DataGridItem In dgOrders.Items
chkBox = CType(di.Cells(4).Controls(0), CheckBox)
If chkBox.Checked Then
oOrder.CloseOrder(di.Cells(0).Text)
End If
Next
This code occurs when I hit a save button and it checks which checkboxes in the grid that was checked and update the db (oorder.closeorder)
but the line
VB Code:
chkBox = CType(di.Cells(4).Controls(0), CheckBox)
doesn't work, I get an invalid cast exception... why? is there any other way to do it?
kind regards
henrik
-
Feb 18th, 2004, 10:56 AM
#2
Make sure Control(0) is in fact a checkbox. Try:
Msgbox(TypeName(di.Cells(4).Controls(0)))
-
Feb 19th, 2004, 01:36 AM
#3
Thread Starter
Frenzied Member
Hm ok this is wierd. In my datagrid I have:
Buttoncolumn, bound control, boundcontrol, templatecontrol, templatecontrol
When I check
di.Cells(0).Controls(0) I get linkbutton, which is great
di.Cells(1).Controls(0) <--invalid index (no control here)
di.Cells(2).Controls(0) <--invalid index (no control here)
di.Cells(3).Controls(0) <--LITERAL Control (wierd, because it's checkbox)
di.Cells(4).Controls(0) <--LITERAL Control (wierd, because it's checkbox)
I really have no clue why it is like this....?
kind regards
Henrik
-
Feb 19th, 2004, 11:33 PM
#4
I wonder how many charact
IF I know exactly what control belongs there..
I use:
VB Code:
Dim X as Checkbox = DirectCast(di.Cells(4).Controls(0),CheckBox)
I think your problem lies in the fact, that the datagrid has temporarily placed a literal where the checkbox will take it place (before the page has rendered)or before the data has been binded. Once the binding actually occurs, the datagrid probably renders a checkbox in there.
You see, you need to put your code in the databinding event, or shortly after in the prerender.
Last edited by nemaroller; Feb 19th, 2004 at 11:43 PM.
-
Feb 20th, 2004, 07:27 AM
#5
Thread Starter
Frenzied Member
Well the page works like this:
First I render a few textboxes and a search button
The user click search and I perform a databind on the datagrid if I got any values back from the db. I also make a button "save" visible, and it is in that button event that I want to loop through the datagrid and check if any checkboxes are checked... Here is the code
VB Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
FillUI()
End If
End Sub
Public Sub FillUI()
Dim oCompany As New clsCompany
Dim oStatus As New clsStatus
With ddlSalesCompany
.DataSource = oCompany.GetNamesDr
.DataValueField = "COMPANY_KEY_NO"
.DataTextField = "NAME"
.DataBind()
.SelectedItem.Text = "*"
oCompany = Nothing
End With
End Sub
Private Sub btnSearchInvoice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchInvoice.Click
Dim oOrder As New COrder
dgOrders.DataSource = oOrder.GetOrders(txtOrderNo.Text, ddlSalesCompany.SelectedItem.Text, ddlStatus.SelectedItem.Text)
dgOrders.DataBind()
If dgOrders.Items.Count < 1 Then
dgOrders.Visible = False
btnSaveOrder.Visible = False
Else
dgOrders.Visible = True
btnSaveOrder.Visible = True
End If
End Sub
Public Sub SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
Dim lb As New LinkButton
lb = CType(e.Item.Cells(0).Controls(0), LinkButton)
Server.Transfer("AInvoiceDetails.aspx" & "?OrderId=" & lb.Text)
'Response.Write("Text" & lb.Text)
End Sub
Private Sub btnSaveOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveOrder.Click
'Here I want to make something nice for each checkbox that is checked
End Sub
Im really not sure what you mean... the data is bound to the grid... and the savebutton doesn't perform a postback?? Because when I click the button the data is still bound to the grid!
anyone with an explanation???
kind regards
Henrik
-
Feb 20th, 2004, 09:06 AM
#6
I wonder how many charact
Your save button must be posting back, because unless you wrote JScript, (and the fact that you have Private Sub btnSaveOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveOrder.Click
in your code means your handling it on a postback.)
What I was getting at was the Checkboxes may not exist inside your datagrid at the time you are checking (the SaveOrder_Click function), because the DataGrid hasn't rendered yet.
Try putting this in the Page_Init event (it may solve it), or it may not:
EnsureChildControls()
-
Feb 20th, 2004, 09:17 AM
#7
I wonder how many charact
Otherwise try this code in your savebutton-click event (grabbed from mspress Programming VB.NET):
VB Code:
Dim dg as DataGridItem
For Each dg In dgrCompanies.Items 'find the checkbox in the row
Dim cb As CheckBox = DirectCast(dg.FindControl ( "chkSelect"),CheckBox) 'replace chkSelect with name of your checkbox
'if checked
IF cb.Checked Then
'do something
End if
Next
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|