Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > VB.NET code troubleshooting (CommandArgument property during OnItemDataBound event)

Reply
Thread Tools

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

 
 
jason
Guest
Posts: n/a
 
      06-13-2005
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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?UHJha2FzaC5ORVQ=?=
Guest
Posts: n/a
 
      06-14-2005
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" wrote:

> 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
>
>

 
Reply With Quote
 
 
 
 
jason
Guest
Posts: n/a
 
      06-14-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem assigning selectedItem.Value to a dropdownlist filled with OnItemDataBound Antonio D'Ottavio ASP .Net 1 09-19-2005 09:41 AM
What is most efficient? OnItemDataBound or using a function Anders ASP .Net 4 07-19-2004 11:29 AM
Re: How to access DataSource rows onItemDatabound? ashelley@inlandkwpp.com ASP .Net 0 07-08-2004 04:27 PM
OnItemDataBound issue... Maureen Black ASP .Net 0 04-04-2004 05:36 AM
DataGrid OnItemDataBound problem in APS.NET C# Steven ASP .Net 1 07-09-2003 05:17 PM



Advertisments