Linking a Button in Gridview to Codebehind

Last post 01-15-2008 9:28 PM by RJA. 6 replies.

Sort Posts:

  • Linking a Button in Gridview to Codebehind

    01-14-2008, 4:48 PM
    • Loading...
    • FZero
    • Joined on 11-29-2007, 9:12 PM
    • Posts 24

    I have a gridview setup with a link, which performs as it should.

    I would like perform the same functionallity with a button.

     Here's what I have so far (SOURCE- Link code is at the bottom which works correctly):

    <asp:GridView BorderWidth="0px" Width="650px" ID="GridView2" Font-Size="8pt" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" >

    <Columns>

     

              <asp:BoundField DataField="ContractID" Visible="False" HeaderText="Contract ID" SortExpression="ContractID" />
             
    <asp:BoundField DataField="ContractName" HeaderText="Contract Name" SortExpression="ContractName" />

             
    <asp:ButtonField ButtonType="Button" Text="Renew" >
                    
    <ControlStyle Font-Size="7pt" />
             
    </asp:ButtonField>

     

     

               <asp:TemplateField>
                      
    <ItemTemplate>
                               
    <a href="ContractInformation.aspx?cid=<%#Eval("ContractID")%>">Renew</a>
                       
    </ItemTemplate>
              
    </asp:TemplateField>

     

           </Columns>

     

           <HeaderStyle Font-Bold="True" ForeColor="#FFFDFF" />
          
    <RowStyle ForeColor="#FFFDFF" />

    </asp:GridView>

     

     How do I get my button Field to act like the <a href> that I have setup?

    TIA!

     -S

    I'm using: ASP.NET 2005, VB.NET

    www.fightingzero.com
    Filed under: ,
  • Re: Linking a Button in Gridview to Codebehind

    01-14-2008, 5:13 PM
    Answer
    • Loading...
    • datagrid
    • Joined on 05-23-2007, 1:01 PM
    • USA
    • Posts 461

    Fzero,

    Have you tried the control: LinkButton

    <asp:LinkButton Text="click me" runat="server"></asp:LinkButton>

    Das (http://aspalliance.com/author.aspx?uId=697)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Dont forget to click "Mark as Answer" on the post that helped you. This marks your thread as Resolved so we will all know you have been helped.
  • Re: Linking a Button in Gridview to Codebehind

    01-14-2008, 5:16 PM
    Answer
    • Loading...
    • mosessaur
    • Joined on 07-11-2002, 6:40 PM
    • Cairo
    • Posts 352

    My suggestion to you in your case do not use ButtonField Replace it with this
    <asp:TemplateField>
     
    <ItemTemplate>
       <
    asp:Button id="btnRenew" CommandName="Renew" CommandArgument='<%#String.Format("ContractInformation.aspx?cid={0}",Eval("ContractID"))%>' />
     
    </ItemTemplate>
    </asp:TemplateField>

    Now in your code Behind Handle your GridView RowCommand Event

    void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if(e.CommandName=="Renew")
        {
         string url = (string)e.CommandArgument;
         Response.Redirect(url);
        }
        
    }

    Hope this would match your needs. 

    Muhammad M. Mosa Soliman
    Software Engineer
    Moses's Blog
    Filed under: ,
  • Re: Linking a Button in Gridview to Codebehind

    01-15-2008, 10:06 AM
    • Loading...
    • FZero
    • Joined on 11-29-2007, 9:12 PM
    • Posts 24

    datagrid - The LinkButton control isn't really what I was going for.  Thank you tho!

    mosessaur - That's the idea that I'm after, however, I need to work the codebehind in VB.  I'm afraid I'm not that fluent in java.

    Do you know how it would translate to VB?

    Thanks!

    -S

    I'm using: ASP.NET 2005, VB.NET

    www.fightingzero.com
    Filed under: ,
  • Re: Linking a Button in Gridview to Codebehind

    01-15-2008, 9:08 PM
    • Loading...
    • mosessaur
    • Joined on 07-11-2002, 6:40 PM
    • Cairo
    • Posts 352

    sorry I didn't notice here is the equivalent code:
    Protected Sub GridView_RowCommand(sender As Object, e As GridViewCommandEventArgs)
    Begin
        If e.CommandName == "Renew" Then  
         url As string = CType(e.CommandArgument,String)
         Response.Redirect(url);
        End If
    End

    Muhammad M. Mosa Soliman
    Software Engineer
    Moses's Blog
  • Re: Linking a Button in Gridview to Codebehind

    01-15-2008, 9:23 PM
    • Loading...
    • vinz
    • Joined on 10-05-2007, 11:47 AM
    • Cebu Philippines
    • Posts 7,422

    Try this

    Private Sub GridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
        If e.CommandName = "Renew" Then
            Dim url As String = DirectCast(e.CommandArgument, String)
            Response.Redirect(url)
        End If
    End Sub 

    Cheers,
    Vincent Maverick Durano

    "Life is like music; it must be composed by ear, feeling, and instinct, not by rule..."

  • Re: Linking a Button in Gridview to Codebehind

    01-15-2008, 9:28 PM
    • Loading...
    • RJA
    • Joined on 07-09-2003, 8:26 AM
    • Pittston, Pa
    • Posts 206

    If you want to try it with no code behind and just the .aspx page, try this:

    <asp:hyperlinkfield datatextfield="ProductName" datatextformatstring="{0:c}"

    datanavigateurlfields="ProductID" datanavigateurlformatstring="~\details.aspx?ProductID={0}"         

    headertext="Product"/>

    Rachel Appel
    MVP - ASPInsider
Page 1 of 1 (7 items)
Microsoft Communities
Page view counter