Cell(0) and Control(0)

G

Guest

Hi,

I keep getting the following error in my code:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index

I presume it is because I have the wrong control number but am not 100% sure! How do I work this out?

Thanks

...::CODE::..
Sub DGPages_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
' First, make sure we're NOT dealing with a Header or Footer row
If e.Item.ItemType <> ListItemType.Header And _
e.Item.ItemType <> ListItemType.Footer Then
'Now, reference the LinkButton control that the Delete ButtonColumn
'has been rendered to
Dim deleteButton As LinkButton = e.Item.Cells(0).Controls(0) ' Error

'We can now add the onclick event handler
deleteButton.Attributes("onclick") = "javascript:return " & _
"confirm('Are you sure you want to delete Page #" & _
DataBinder.Eval(e.Item.DataItem, "ID") & "?')"
End If
End Sub

...::
<asp:datagrid id="DGPages" runat="server"
HeaderStyle-Font-Bold="True"
HeaderStyle-HorizontalAlign="Center"
HeaderStyle-ForeColor="White"
HeaderStyle-BackColor="Black"

EditItemStyle-BackColor="#eeeeee"

CellPadding="4"
Width="90%"
AutoGenerateColumns="False"

DataKeyField="pageID"
OnItemDataBound="DGPages_ItemDataBound"
OnEditCommand="DGPages_Edit"
OnUpdateCommand="DGPages_Update"
OnCancelCommand="DGPages_Cancel"
OnDeleteCommand="DGPages_Delete">

<Columns>
<asp:BoundColumn DataField="pageID" ReadOnly="True" HeaderText="ID">
<ItemStyle CssClass="txtArea"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="modificationDate" HeaderText="Date Modified" DataFormatString="{0:MM/dd/yyyy}">
<ItemStyle CssClass="txtArea"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="description" HeaderText="Description">
<ItemStyle CssClass="txtArea"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="header" HeaderText="Title">
<ItemStyle CssClass="txtArea"></ItemStyle>
</asp:BoundColumn>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete" ButtonType="LinkButton"></asp:ButtonColumn>
</Columns>
</asp:datagrid>
 
J

John Saunders

Tim::.. said:
Hi,

I keep getting the following error in my code:

System.ArgumentOutOfRangeException: Specified argument was out of the
range of valid values. Parameter name: index
I presume it is because I have the wrong control number but am not 100%
sure! How do I work this out?

Didn't I answer this one already?

You either have no cells, or you have at least one cell and no controls in
it. Try doing a Response.Write(e.Item.Cells(0).Controls.Count.ToString())
 
M

Martin Marinov

Because of using BoundColumn for your first column actually you don't have
controls in it - you have only text there
you can check it by debuging your application and check
e.Item.Cells(0).Controls.Count

also as i see from yuor code the LinkButton Control is not in the first but
in the last column, so may be you have to use

e.Item.Cells(5).Controls(1), but you have to check if the LinkButton is the
second control

Regards
Martin

Tim::.. said:
Hi,

I keep getting the following error in my code:

System.ArgumentOutOfRangeException: Specified argument was out of the
range of valid values. Parameter name: index
I presume it is because I have the wrong control number but am not 100% sure! How do I work this out?

Thanks

..::CODE::..
Sub DGPages_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
' First, make sure we're NOT dealing with a Header or Footer row
If e.Item.ItemType <> ListItemType.Header And _
e.Item.ItemType <> ListItemType.Footer Then
'Now, reference the LinkButton control that the Delete ButtonColumn
'has been rendered to
Dim deleteButton As LinkButton = e.Item.Cells(0).Controls(0) ' Error

'We can now add the onclick event handler
deleteButton.Attributes("onclick") = "javascript:return " & _
"confirm('Are you sure you want to delete Page #" & _
DataBinder.Eval(e.Item.DataItem, "ID") & "?')"
End If
End Sub

..::
<asp:datagrid id="DGPages" runat="server"
HeaderStyle-Font-Bold="True"
HeaderStyle-HorizontalAlign="Center"
HeaderStyle-ForeColor="White"
HeaderStyle-BackColor="Black"

EditItemStyle-BackColor="#eeeeee"

CellPadding="4"
Width="90%"
AutoGenerateColumns="False"

DataKeyField="pageID"
OnItemDataBound="DGPages_ItemDataBound"
OnEditCommand="DGPages_Edit"
OnUpdateCommand="DGPages_Update"
OnCancelCommand="DGPages_Cancel"
OnDeleteCommand="DGPages_Delete">

<Columns>
<asp:BoundColumn DataField="pageID" ReadOnly="True" HeaderText="ID">
<ItemStyle CssClass="txtArea"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="modificationDate" HeaderText="Date Modified" DataFormatString="{0:MM/dd/yyyy}">
<ItemStyle CssClass="txtArea"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="description" HeaderText="Description">
<ItemStyle CssClass="txtArea"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="header" HeaderText="Title">
<ItemStyle CssClass="txtArea"></ItemStyle>
</asp:BoundColumn>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update"
 
J

John Saunders

Tim::.. said:
Thanks John!

Sorry I miss understood your first reply! I did the response.write and it returned 00!
Sorry I'm new to asp.net what sort of response am I looking for????

There is data in the pageID DataField so why wouldn't it work? Perhaps
pointing at the wrong cell and wrong control!

Tim, 0 showed what I expected, that there are no controls in Cell 0. Perhaps
you are mistaken about which Cell your controls are in? Try setting a
breakpoint in the debugger and examining e.Item, or else try adding some
code like this instead of the Response.Write (untested! gotta run to a job
interview):

Dim cell As TableCell
Dim ctl As Control
Dim i As Integer
Dim j As Integer

For i = 0 to e.Item.Cells.Count -1
cell = e.Item.Cells(i)

If cell.Controls.Count = 0 Then
Page.Trace.Write(String.Format("No controls in cell {0}", i))
Else
Page.Trace.Write(String.Format("{0} controls in cell
{1}",cell.Controls.Count , i))
For j = 0 to cell.Controls.Count - 1
ctl = cell.Controls(j)

Page.Trace.Write(String.Format("Control {0} in cell {1} is {2}",
j, i, cell.Controls(j)))
Next j
End If
Next i
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top