0

I have a row of buttons in a gridview that are created dynamically. When the button in a cell is clicked, the row disappears. Not sure why. Does anyone have a solution to this?
Here is my code:

Protected Sub GridView2_RowDataBound(sender As Object, e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            For Each cell As TableCell In e.Row.Cells

                Dim lb As New Button()
                lb.Text = "Apply"
                lb.CommandName = "applicationlink"
                AddHandler lb.Command, AddressOf button_Command
                cell.Controls.Add(lb)
            Next
        End If
    End Sub
    Protected Sub button_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
        If e.CommandName = "applicationlink" Then
            'This is to test  
            Dim lb As Button = DirectCast(sender, Button)
            'lb.Text = "OK"
            Response.Redirect("http://www..com")
        End If
    End Sub

Thanks

3
Contributors
7
Replies
22
Views
4 Years
Discussion Span
Last Post by ggamble
0

Thanks LastMitch
Here is the entrie code for the page. I didn't think it would help tpo put the entire coue up. My apology.

Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Sql
Imports System.Data.SqlClient

Partial Class ThirdPage_OR
    Inherits System.Web.UI.Page


    'Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
    '    GridView2.DataBind()
    'End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ' Run only once a postback has occured
        'If Page.IsPostBack Then
        'Set the Rows and Columns property with the value
        'entered by the user in the respective textboxes
        'Me.Rows = Int32.Parse(txtRows.Text)
        'Me.Columns = Int32.Parse(txtCols.Text)
        'End If
        'CreateDynamicTable()
        '-------------------End Test Area-------------------
        If Not Page.IsPostBack Then

            AccessDataSourceResult.DataFile = ConfigurationManager.AppSettings("ConnectionString1")
            If Me.PreviousPage Is Nothing = False Then
                Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ConfigurationManager.AppSettings("ConnectionString1")
                Dim MyCon As New OleDbConnection(strConn)

                Dim MySqlDelete As String = "delete from SelectionsWEB"

                Dim MySqlInsert As String = "INSERT INTO WEBs (Selection1,Selection2,Selection3,Selection4,Selection5) VALUES (@Selection1,@Selection2,@Selection3,@Selection4,@Selection5)"

                Dim cmd As New OleDbCommand(MySqlDelete, MyCon)
                With cmd.Parameters
                    .Add(New OleDbParameter("@Selection1", Me.PreviousPage.PlanIDs_Checkboxes(0)))
                    .Add(New OleDbParameter("@Selection2", Me.PreviousPage.PlanIDs_Checkboxes(1)))
                    .Add(New OleDbParameter("@Selection3", Me.PreviousPage.PlanIDs_Checkboxes(2)))
                    .Add(New OleDbParameter("@Selection4", Me.PreviousPage.PlanIDs_Checkboxes(3)))
                    .Add(New OleDbParameter("@Selection5", Me.PreviousPage.PlanIDs_Checkboxes(4)))
                End With

                Dim result As Integer = -1

                If MyCon.State = ConnectionState.Closed Then
                    MyCon.Open()
                    cmd.CommandText = MySqlDelete
                    result = cmd.ExecuteNonQuery()
                    MyCon.Close()
                End If
                If MyCon.State = ConnectionState.Closed Then
                    MyCon.Open()
                    cmd.CommandText = MySqlInsert
                    cmd.ExecuteNonQuery()
                    MyCon.Close()
                End If


            End If
            BindGridView()
            'Adds button column
            'Addbutton()
        End If
    End Sub
    Protected Overrides Sub OnInit(ByVal e As EventArgs)
        'AddHandler Me.Init, AddressOf Page_Init 
        MyBase.OnInit(e)
    End Sub

    Protected Sub AccessDataSourceResult_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles AccessDataSourceResult.Selecting
        AccessDataSourceResult.DataFile = "L:\website\database.mdb"
    End Sub
    Private Function PivotTable(origTable As DataTable) As DataTable
        Dim newTable As New DataTable()
        Dim dr As DataRow = Nothing

        'Add Columns to new Table
        For i As Integer = 0 To origTable.Rows.Count
            newTable.Columns.Add(New DataColumn(origTable.Columns(i).ColumnName, GetType([String])))
        Next

        'Execute the Pivot Method
        For cols As Integer = 0 To origTable.Columns.Count - 1
            dr = newTable.NewRow()
            For rows As Integer = 0 To origTable.Rows.Count - 1
                If rows < origTable.Columns.Count Then
                    dr(0) = origTable.Columns(cols).ColumnName
                    ' Add the Column Name in the first Column
                    dr(rows + 1) = origTable.Rows(rows)(cols)
                End If
            Next
            'add the DataRow to the new Table rows collection
            newTable.Rows.Add(dr)
        Next
        Return newTable
    End Function
    Private Sub BindGridView()

        Dim connString As String = ConfigurationManager.ConnectionStrings("ConnectionString1").ConnectionString
        Dim connection As New OleDbConnection(connString)
        Dim dt As New DataTable
        Try
            connection.Open()
            Dim sqlStatement As String = "SELECT * FROM WEB"
            Dim sqlCmd As New OleDbCommand(sqlStatement, connection)
            Dim sqlDa As New OleDbDataAdapter(sqlCmd)
            sqlDa.Fill(dt)
            If dt.Rows.Count > 0 Then
                'Bind the First GridView with the original data from the DataTable
                'GridView1.DataSource = dt
                'GridView1.DataBind()

                'Pivot the Original data from the DataTable by calling the
                'method PivotTable and pass the dt as the parameter
                Dim pivotedTable As DataTable = PivotTable(dt)
                GridView2.DataSource = pivotedTable

                GridView2.DataBind()
            End If
        Catch ex As System.Data.SqlClient.SqlException
            Dim msg As String = "Fetch Error:"
            msg += ex.Message
            Throw New Exception(msg)
        Finally
            connection.Close()
        End Try
    End Sub

    Protected Sub GridView2_RowDataBound(sender As Object, e As GridViewRowEventArgs)
        If e.Row.RowIndex = 0 Then
                For Each cell As TableCell In e.Row.Cells
                    Dim imagePath As [String] = cell.Text

                    Dim image As New Image()
                    image.ImageUrl = imagePath

                    cell.Text = ""
                    cell.Controls.Add(image)
                Next
            End If
            If e.Row.RowIndex = 1 Then
                For Each cell As TableCell In e.Row.Cells

                    Dim lb As New Button()
                    lb.Text = "Apply"
                    lb.CommandName = "applicationlink"
                    AddHandler lb.Command, AddressOf button_Command
                    cell.Controls.Add(lb)
                Next
            End If


    End Sub
    Protected Sub button_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
        If e.CommandName = "applicationlink" Then
            'This is to test  
            Dim lb As Button = DirectCast(sender, Button)
            'lb.Text = "OK"
            Response.Redirect("http://www.test.com")
        End If
    End Sub

End Class

The link you send seems to have what I need to make this work, but I'm missing something. I'm not very bright at this. It would be simple if it were not a pivoted gridview.
Thanks

0

The link you send seems to have what I need to make this work, but I'm missing something. I'm not very bright at this. It would be simple if it were not a pivoted gridview.

I'm not that bright either, but it's good that you use the ASP.net forum. I also go there to look for answer because the forum is ASP.net.

The issue is the image path to the button when you click the button it delete the row it shouldn't do that.

Does the Page.IsPostBack works?

Edited by LastMitch: grammer

0

Try this code:

Dim intGRID as Integer = 1 'Global declaration

Protected Sub GridView2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2_RowDataBound

e.Row.ID = "GridView2_" & Cstr(intGRID)
intGRID += 1 'Increase intGRID by one

End Sub

or you can used a ID to make sure that it won't dissapear when you click the button:

protected void gridView_RowCreated(object sender, GridViewRowEventArgs e)
{
    e.Row.ID = RowId.ToString();
    ++RowId;
}

Edited by LastMitch

0

Thanks LastMitch. I have tried this code with no change. Still that same behavior. It appears that the page.postback does work.
Thanks

0

Still that same behavior. It appears that the page.postback does work.

OK, Let try a minor debug. Can you add another column called numbers so all the rows appear a number:

<asp:TemplateField>
  <ItemTemplate>
    <%# Container.DataItemIndex + 1 %>
  </ItemTemplate>
</asp:TemplateField>

The code is from this link: How to display row numbers in GridView

Then click on the button to see whether it delete the row that has numbers. If it delete the row that has numbers on it.

Then try to used this function GridView OnSelectedIndexChanged add this to your button:

<asp:GridView OnSelectedIndexChanged="EventHandler" />

You can also take a look at this link which shows GridView: Add-Edit-Update-Delete-and-Paging-the-AJAX

http://www.aspsnippets.com/Articles/GridView---Add-Edit-Update-Delete-and-Paging-the-AJAX-way.aspx

That link is my reference for using GridView. I know you are not using AJAX but you can still look at those button links to compare what you are using differently.

Edited by LastMitch: grammer

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.