We help IT Professionals succeed at work.

how to create a custom edit button in a vb.net gridview

bschave2
bschave2 asked
on
523 Views
Last Modified: 2012-05-08
I have a gridview and would like to have a custom edit button, not to edit within the gridview, but to send the user to a different page, but also need to send the PK along with it to grab the record on the other page. How do I do this?


FRONT END...ASPX PAGE
 
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" CellPadding="3" Font-Overline="False" 
        Font-Size="Small" ForeColor="#003366" HorizontalAlign="Left" Width="609px">
        <PagerSettings FirstPageImageUrl="~/images/first_btn.jpg" 
            LastPageImageUrl="~/images/last_btn.jpg" 
            NextPageImageUrl="~/images/next_btn.jpg" 
            PreviousPageImageUrl="~/images/prev_btn.jpg" Mode="NextPreviousFirstLast" />
        <Columns>
            <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>
                    <asp:Button ID="edit_it" OnCommand="edit_Click" CommandArgument='<%#Eval("idpressrelease")%>' runat="server" CssClass="edit_btn" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>
                    <asp:Button ID="delete" OnCommand="delete_Click" CommandArgument='<%#Eval("idpressrelease")%>' runat="server" Text="Delete" CssClass="standardButton" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField AccessibleHeaderText="ID" DataField="idpressrelease" 
                HeaderText="ID" />
            <asp:BoundField DataField="visible" HeaderText="Visible" />
            <asp:BoundField DataField="headline" HeaderText="Headline" />
            <asp:BoundField DataField="datestamp" HeaderText="Date Stamp" />
        </Columns>
    </asp:GridView>
 
 
BACK END.....VBS PAGE
 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            binddata()
        Catch ex As Exception
            lblStatus.Text = ex.Message
        End Try
    End Sub
 
    Sub binddata()
        conn.Open()
        cmd = New MySqlCommand("SELECT p.idpressrelease,CAST(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(p.Headline, char(145), ""'""), char(146), ""'""), char(147), '""'), char(148), '""'), char(150), '&ndash;'), char(151), '&mdash;') AS CHAR) AS Headline,CAST((CASE p.visible WHEN 1 THEN 'Yes' WHEN 2 THEN 'No' ELSE 0 END) as char) as visible, cast(date_format(p.DateStamp, '%m/%d/%Y')as char) as datestamp from pressrelease as p order by p.DateStamp DESC", conn)
        da = New MySqlDataAdapter(cmd)
        ds = New DataSet()
        da.Fill(ds)
        dt = ds.Tables(0)
        GridView1.DataSource = dt
        Session("dtSess") = dt
        GridView1.DataBind()
        cmd.Connection.Close()
        cmd.Connection.Dispose()
        conn.Close()
        conn.Dispose()
        ds.Dispose()
    End Sub
 
    Protected Sub edit_Click(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles Me.Load
 
    End Sub
 
    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        GridView1.PageIndex = e.NewPageIndex
        GridView1.DataSource = Session("dtSess")
        GridView1.DataBind()
 
 
    End Sub

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Add a button field and on the ItemCommand event, grab the PK value and redirect the page.

Author

Commented:
could you give me an example of how to grab the pk?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Consultant
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.