Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 13th, 2004, 05:24 PM
Authorized User
 
Join Date: Oct 2003
Location: , , .
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
Default Datagrid ButtonColumn

Hi,

Currently I have in my datagrid a itemtemplate colume with a href that is working.
However, instead of using itemTemplate i want to use a button but I am not sure how to send the parameters sid &U_ID to the next page.

Here is my code so far:
aspx.vb
  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        Dim isAuth As Boolean = False
        Try

            sid = Request.QueryString("sid")
            UD = (US.GetUserDataByGuid(sid))

        Catch
            Response.Redirect("default.asp")

        End Try
        If Not UD.IsAdmin Then
            isAuth = False
            Response.Redirect("default.asp")
        End If

    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        If txtSearch.Text = "" Then
            Response.Write("Enter text")
        Else
            fillTable()
        End If

    End Sub
    Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DataGrid1.ItemCommand

        If e.CommandSource.commandname = "Update" Then

            Response.Redirect("../practice/databinding.aspx")

        End If

        If e.CommandSource.commandname = "ShowAll" Then

        End If
    End Sub
    Sub fillTable()

U_ID = txtSearch.Text
cmd = oConn.CreateCommand
cmd.CommandText = "SELECT *, 'UserMsg.aspx?sid=' + @sid +'&'+'U_ID=' + CAST(u_id AS NVARCHAR(16)) AS url_str, (SELECT TOP 1 ad_idnum FROM YBAY_MDB...ADS WHERE ad_poster=users.u_id) AS idnum FROM ybay_mdb...users USERS WHERE U_ID like '%'+@U_ID+'%'"
        cmd.Parameters.Add("@U_ID", U_ID)
        cmd.Parameters.Add("@sid", sid)
        Dim da As New SqlDataAdapter(cmd)
        Dim dt As New DataTable("ads")
        da.Fill(dt)
        DataGrid1.DataSource = dt
        DataGrid1.DataBind()
    End Sub

ASPX:
<asp:datagrid CssClass="GridWidth" id="DataGrid1" runat="server" HorizontalAlign="Right" AutoGenerateColumns="False"
                                BackColor="White" BorderWidth="2px" BorderStyle="Ridge" BorderColor="White" PagerStyle-HorizontalAlign="Center"
                                AllowPaging="True" OnPageIndexChanged="doPaging" CellPadding="3" Width="100%" Height="100%"
                                GridLines="None" CellSpacing="1">
                                <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#9471DE"></SelectedItemStyle>
                                <ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
                                <HeaderStyle Font-Bold="True" ForeColor="#E7E7FF" BackColor="#4A3C8C"></HeaderStyle>
                                <FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
                                <Columns>
                                    <asp:BoundColumn DataField="idnum" HeaderText="id"></asp:BoundColumn>
                                    <asp:TemplateColumn HeaderText ="fullname">
                                        <ItemTemplate>
<a href ="<%#container.dataitem ("url_str")%>"><%#container.dataitem ("u_first")%><%#container.dataitem ("u_last")%></a>



  #2 (permalink)  
Old January 14th, 2004, 10:00 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I'm kind of confused by your post. What part of that code should we be looking at? Are you unclear on how to send the values? (Put them on the redirect querystring.) Or are you unclear on how to get the values from the row in the datagrid in the button click handler?

Peter
------------------------------------------------------
Work smarter, not harder.
  #3 (permalink)  
Old January 15th, 2004, 04:36 AM
Authorized User
 
Join Date: Oct 2003
Location: , , .
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
i don't know how to get the values from the row in the datagrid in the button click handler.

  #4 (permalink)  
Old January 15th, 2004, 10:35 AM
Registered User
 
Join Date: Jan 2004
Location: Oakland, KY, USA.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't know if this helps or not. I use the following line:
Label28.Text = CFG1.GetDataDisplay(CFG1.RowSel, CFG1.ColSel)
to display a selected cell's data elsewhere.
  #5 (permalink)  
Old January 15th, 2004, 10:56 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You can access any value in the datagrid from within a datagrid event handler like this:

e.Item.Cells(<index of the column>).Text

There will be some variations to this however. If the item is in edit mode, .Cells(...).Text doesn't work because the data is in an editing control (textbox by default). So then the syntax becomes slightly more complicated. You need to access the edit control and convert it to the right type then access the value:

CType(e.Item.Cells(<index of the column>).Controls(0), TextBox).Text


If all you have to start is a plain drawn out datagrid (you don't have edit/update capabilities) and all you want is to have a "select this item" button that sends you off to another page, then the first syntax will work.

Another important note about this is that you must take ALL columns into account when determining the column index. If you need data from the data source but don't want to show it, you can create a hidden column by putting Hidden="True" in the markup for the column. This column will still be part of the Cells(...) collection but you just won't see it on the page. This is an easy way to save row identifiers.

Peter
------------------------------------------------------
Work smarter, not harder.
  #6 (permalink)  
Old January 15th, 2004, 11:40 AM
Authorized User
 
Join Date: Oct 2003
Location: , , .
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
Thanks for the reply.
 it works it sends the correct u_id value to the next page via the querystring
 but i have a problem when i want to go to the next page in my datagrid (i have paging).

when i press the link to the next page i get the following error:
Specified argument was out of the range of valid values. Parameter name: index
Source Error:

Line 105: Dim userCell As TableCell = e.Item.Cells(3)



my aspx.vb code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

Dim isAuth As Boolean = False

Try


sid = Request.QueryString("sid")

UD = (US.GetUserDataByGuid(sid))

Catch

Response.Redirect("default.asp")

End Try

If Not UD.IsAdmin Then

isAuth = False

Response.Redirect("default.asp")

End If


End Sub

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

If txtSearch.Text = "" Then

Response.Write("Enter text")

Else

fillTable()

End If

End Sub


Sub fillTable()

U_ID = txtSearch.Text

cmd = oConn.CreateCommand

cmd.CommandText = "SELECT *, 'UserMsg.aspx?sid=' + @sid +'&'+'U_ID=' + CAST(u_id AS NVARCHAR(16)) AS url_str, (SELECT TOP 1 ad_idnum FROM YBAY_MDB...ADS WHERE ad_poster=users.u_id) AS idnum FROM ybay_mdb...users USERS WHERE U_ID like '%'+@U_ID+'%'"

cmd.Parameters.Add("@U_ID", U_ID)

cmd.Parameters.Add("@sid", sid)

Dim da As New SqlDataAdapter(cmd)

Dim dt As New DataTable("ads")

da.Fill(dt)

DataGrid1.DataSource = dt

DataGrid1.DataBind()



End Sub

Sub doPaging(ByVal s As Object, ByVal e As DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged

DataGrid1.CurrentPageIndex = e.NewPageIndex

fillTable()

End Sub

Private Sub btnMsg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMsg.Click

Response.Redirect("edituser.aspx?sid=" & sid)

End Sub

Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DataGrid1.ItemCommand

Dim userCell As TableCell = e.Item.Cells(3)

Dim user As String = userCell.Text

If e.CommandSource.commandname = "Update" Then

Response.Redirect("editUser.aspx?u_id=" & user & "&sid=" & sid)

ASPX:
asp:datagrid CssClass="GridWidth" id="DataGrid1" runat="server" HorizontalAlign="Right" AutoGenerateColumns="False"
                                BackColor="White" BorderWidth="2px" BorderStyle="Ridge" BorderColor="White" PagerStyle-HorizontalAlign="Center"
                                AllowPaging="True" OnPageIndexChanged="doPaging" CellPadding="3" Width="100%" Height="100%"
                                GridLines="None" CellSpacing="1">
                                <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#9471DE"></SelectedItemStyle>
                                <ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
                                <HeaderStyle Font-Bold="True" ForeColor="#E7E7FF" BackColor="#4A3C8C"></HeaderStyle>
                                <FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
                                <Columns>
                                    <asp:BoundColumn DataField="idnum" HeaderText="ú.æ."></asp:BoundColumn>
                                    <asp:TemplateColumn HeaderText="ùí îìà">
                                        <ItemTemplate>
<a href ="<%#container.dataitem ("url_str")%>"><%#container.dataitem ("u_first")%> <%#container.dataitem ("u_last")%></a>

</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="u_password" HeaderText="ñéñîä"></asp:BoundColumn>
<asp:BoundColumn DataField="U_ID" HeaderText="ùí îùúîù"></asp:BoundColumn>
<asp:ButtonColumn Text="øùéîú îåãòåú" ButtonType="PushButton" DataTextFormatString="url_str" HeaderText="øùéîú îåãòåú" CommandName="ShowAll"></asp:ButtonColumn>
                                    <asp:ButtonColumn Text="òãëï ôøèé îùúîù" ButtonType="PushButton" HeaderText="òãëï ôøèé îùúîù" CommandName="Update"></asp:ButtonColumn>
                                </Columns>
                                <PagerStyle HorizontalAlign="center" ForeColor="Black" BackColor="#C6C3C6" CssClass="Paging"
                                    Mode="NumericPages"></PagerStyle>



I will appreciate the help.

Thanks


  #7 (permalink)  
Old January 15th, 2004, 12:04 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The documentation for the ItemCommand event says:

"The ItemCommand event is raised when any button is clicked in the DataGrid control. This event is commonly used to handle buttons controls with a custom CommandName value, such as Add, in the DataGrid control."

Whether this covers the paging "buttons" or not, I don't know. But maybe what's happening is that this event is firing for the paging buttons but the e.Item that the paging buttons are in is not one of your data rows.

Try changing DataGrid1_ItemCommand to check for the CommandName first before you try to access the cells.

Peter
------------------------------------------------------
Work smarter, not harder.
  #8 (permalink)  
Old January 19th, 2004, 09:38 AM
Authorized User
 
Join Date: Oct 2003
Location: , , .
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Thanks for the response. I added If e.Item.ItemIndex >= 0 Then
code
end if

and it works.



Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom paging in Datagrid with datagrid page count madhusrp ASP.NET 1.0 and 1.1 Professional 12 June 2nd, 2008 01:15 PM
User COntrol Datagrid inside datagrid rodmcleay ASP.NET 1.0 and 1.1 Professional 3 April 14th, 2007 10:11 AM
How to change text on a ButtonColumn in a datagrid fidelio ASP.NET 1.0 and 1.1 Professional 4 March 21st, 2006 05:51 PM
Edit/Delete ButtonColumn not working planoie ASP.NET 1.0 and 1.1 Professional 9 August 30th, 2004 07:39 AM
buttonColumn and target=_blank collie VB.NET 2002/2003 Basics 2 March 15th, 2004 05:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.