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

Discussion in 'ASP .Net' started by jason, Jun 13, 2005.

  1. jason

    jason Guest

    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
    jason, Jun 13, 2005
    #1
    1. Advertising

  2. RE: VB.NET code troubleshooting (CommandArgument property during OnIte

    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
    >
    >
    =?Utf-8?B?UHJha2FzaC5ORVQ=?=, Jun 14, 2005
    #2
    1. Advertising

  3. jason

    jason Guest

    Re: VB.NET code troubleshooting (CommandArgument property during OnIte

    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
    jason, Jun 14, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmVuamFtaW4gSm9sZGVyc21h?=

    LinkButton CommandArgument bound property loses value on postback

    =?Utf-8?B?QmVuamFtaW4gSm9sZGVyc21h?=, Aug 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    4,094
    =?Utf-8?B?QmVuamFtaW4gSm9sZGVyc21h?=
    Aug 21, 2004
  2. Replies:
    1
    Views:
    1,913
    Brock Allen
    Sep 29, 2005
  3. TS
    Replies:
    14
    Views:
    658
  4. Nathan Sokalski
    Replies:
    1
    Views:
    679
    Alexey Smirnov
    May 27, 2007
  5. Luigi
    Replies:
    0
    Views:
    448
    Luigi
    Mar 6, 2008
Loading...

Share This Page