VB.NET code troubleshooting (CommandArgument property during OnItemDataBound event)

J

jason

i'm trying to set the commandargument property of a buttoncolumn in the
DataGrid ItemBind event. it works fine in one instance, but in the
latest page i've written it doesn't seem to work, and i can't see why.
can anyone tell me why this chunk of code works:

[from File1.aspx]
<asp:datagrid id="DataGrid1" style="Z-INDEX: 104; LEFT: 16px;
POSITION: absolute; TOP: 136px" runat="server" CellPadding="3"
OnItemDataBound="DataGridBindEvent"
OnItemCommand="DataGridCommandEvent" AutoGenerateColumns="False">
<HeaderStyle Font-Bold="True" Wrap="False"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Ad ID">
<ItemTemplate>
<a href="<%# sRentClicksURL %>display.asp?adid=<%#
DataBinder.Eval(Container.DataItem, "Ad ID") %>"
target="new"><%# DataBinder.Eval(Container.DataItem, "Ad
ID") %>
</a>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Virtual Tour URL">
<ItemTemplate>
<a href="<%# DataBinder.Eval(Container.DataItem, "Virtual
Tour URL") %>" target="new"><%# DataBinder.Eval
(Container.DataItem, "Virtual Tour URL") %>
</a>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="URLText" Runat="Server" Size="55" Text='<%#
DataBinder.Eval(Container.DataItem, "Virtual Tour URL") %
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="Approve" ButtonType="PushButton"
CommandName="Approve">
</asp:ButtonColumn>
<asp:ButtonColumn Text="Reject" ButtonType="PushButton"
CommandName="Reject">
</asp:ButtonColumn>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Save
Edits" CancelText="Cancel Edits" EditText="Edit">
</asp:EditCommandColumn>
</Columns>
</asp:datagrid>

[from File1.aspx.vb]
Protected Sub DataGridBindEvent(ByVal sender As System.Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
Dim tItemType As ListItemType
tItemType = e.Item.ItemType
If tItemType = ListItemType.Header Or tItemType = _
ListItemType.Footer Or tItemType = ListItemType.Separator Then
Exit Sub
End If

Dim oRow As Common.DbDataRecord = CType(e.Item.DataItem, _
Common.DbDataRecord)
Dim sID As String = oRow(0)

Dim oButtonCell1 As TableCell = CType(e.Item.Controls(2), _
TableCell)
Dim oB1 As Button = CType(oButtonCell1.Controls(0), Button)
oB1.CommandArgument = sID

Dim oButtonCell3 As TableCell = CType(e.Item.Controls(3), _
TableCell)
Dim oB3 As Button = CType(oButtonCell3.Controls(0), Button)
oB3.CommandArgument = sID
End Sub


but this chunk doesn't:

[from File2.aspx]
<asp:datagrid id="DataGrid1" style="Z-INDEX: 104; LEFT: 24px;
POSITION: absolute; TOP: 320px" runat="server" Font-Size="Small"
CellPadding="3" Caption="<b>Results</b>" Visible="False"
OnItemDataBound="DataGridBindEvent"
OnItemCommand="DataGridCommandEvent">
<FooterStyle Wrap="False"></FooterStyle>
<SelectedItemStyle Wrap="False"></SelectedItemStyle>
<EditItemStyle Wrap="False"></EditItemStyle>
<AlternatingItemStyle Wrap="False"></AlternatingItemStyle>
<ItemStyle Wrap="False"></ItemStyle>
<HeaderStyle Font-Bold="True" Wrap="False"></HeaderStyle>
<Columns>
<asp:HyperLinkColumn Text="Edit" Target="_blank">
</asp:HyperLinkColumn>
<asp:ButtonColumn Text="Delete" ButtonType="LinkButton"
CommandName="Delete">
</asp:ButtonColumn>
</Columns>
</asp:datagrid>

? thanks for any help,

jason
 
G

Guest

If it is giving out any error, please let me know. One more thing is, check
whether you get the right button control to set the command argument property.

jason said:
i'm trying to set the commandargument property of a buttoncolumn in the
DataGrid ItemBind event. it works fine in one instance, but in the
latest page i've written it doesn't seem to work, and i can't see why.
can anyone tell me why this chunk of code works:

[from File1.aspx]
<asp:datagrid id="DataGrid1" style="Z-INDEX: 104; LEFT: 16px;
POSITION: absolute; TOP: 136px" runat="server" CellPadding="3"
OnItemDataBound="DataGridBindEvent"
OnItemCommand="DataGridCommandEvent" AutoGenerateColumns="False">
<HeaderStyle Font-Bold="True" Wrap="False"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Ad ID">
<ItemTemplate>
<a href="<%# sRentClicksURL %>display.asp?adid=<%#
DataBinder.Eval(Container.DataItem, "Ad ID") %>"
target="new"><%# DataBinder.Eval(Container.DataItem, "Ad
ID") %>
</a>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Virtual Tour URL">
<ItemTemplate>
<a href="<%# DataBinder.Eval(Container.DataItem, "Virtual
Tour URL") %>" target="new"><%# DataBinder.Eval
(Container.DataItem, "Virtual Tour URL") %>
</a>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="URLText" Runat="Server" Size="55" Text='<%#
DataBinder.Eval(Container.DataItem, "Virtual Tour URL") %
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="Approve" ButtonType="PushButton"
CommandName="Approve">
</asp:ButtonColumn>
<asp:ButtonColumn Text="Reject" ButtonType="PushButton"
CommandName="Reject">
</asp:ButtonColumn>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Save
Edits" CancelText="Cancel Edits" EditText="Edit">
</asp:EditCommandColumn>
</Columns>
</asp:datagrid>

[from File1.aspx.vb]
Protected Sub DataGridBindEvent(ByVal sender As System.Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
Dim tItemType As ListItemType
tItemType = e.Item.ItemType
If tItemType = ListItemType.Header Or tItemType = _
ListItemType.Footer Or tItemType = ListItemType.Separator Then
Exit Sub
End If

Dim oRow As Common.DbDataRecord = CType(e.Item.DataItem, _
Common.DbDataRecord)
Dim sID As String = oRow(0)

Dim oButtonCell1 As TableCell = CType(e.Item.Controls(2), _
TableCell)
Dim oB1 As Button = CType(oButtonCell1.Controls(0), Button)
oB1.CommandArgument = sID

Dim oButtonCell3 As TableCell = CType(e.Item.Controls(3), _
TableCell)
Dim oB3 As Button = CType(oButtonCell3.Controls(0), Button)
oB3.CommandArgument = sID
End Sub


but this chunk doesn't:

[from File2.aspx]
<asp:datagrid id="DataGrid1" style="Z-INDEX: 104; LEFT: 24px;
POSITION: absolute; TOP: 320px" runat="server" Font-Size="Small"
CellPadding="3" Caption="<b>Results</b>" Visible="False"
OnItemDataBound="DataGridBindEvent"
OnItemCommand="DataGridCommandEvent">
<FooterStyle Wrap="False"></FooterStyle>
<SelectedItemStyle Wrap="False"></SelectedItemStyle>
<EditItemStyle Wrap="False"></EditItemStyle>
<AlternatingItemStyle Wrap="False"></AlternatingItemStyle>
<ItemStyle Wrap="False"></ItemStyle>
<HeaderStyle Font-Bold="True" Wrap="False"></HeaderStyle>
<Columns>
<asp:HyperLinkColumn Text="Edit" Target="_blank">
</asp:HyperLinkColumn>
<asp:ButtonColumn Text="Delete" ButtonType="LinkButton"
CommandName="Delete">
</asp:ButtonColumn>
</Columns>
</asp:datagrid>

? thanks for any help,

jason
 
J

jason

thanks for the reply. the run-time error is an invalid cast exception
on the line:

Dim oB As Button = CType(oButtonCell.Controls(0), Button)

i'm fairly sure that's the only control in the cell, because it is a
buttoncolumn with no other elements, just like the example above that
is working. if there are other controls in the cell, i have no idea
what they would be. but still, your prognosis makes sense, given the
error message. Control(0) doesn't seem to be a button during run time.
i'll see if i can spit out the types of each of the controls to see
what's going on.

thanks,

jason
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top