Hyperlink as postback controller...

Last post 07-27-2007 8:34 PM by ChrisCicc. 17 replies.

Sort Posts:

  • Hyperlink as postback controller...

    07-26-2007, 7:06 PM
    Answer
    • Contributor
      2,042 point Contributor
    • ojm37
    • Member since 07-30-2003, 7:31 PM
    • AZ
    • Posts 615

    Is there any way to make a asp:hyperlink or asp:buttonlink control behave like a asp:button control for postback purposes (i.e., when click on the hyperlink it does a postback and fires a click event instead of doing the navigate function -- so that it can be the postback "button" for an updatePanel control)?

    For example, I have an UpdatePanel control with a couple of buttons on it like so: 

    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
              <ContentTemplate>
                <asp:Table ID="Table1" BorderStyle="None" runat="Server">
                   <asp:TableRow>
                   <asp:TableCell ID="tcButtonCell">
                 <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                 <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
                 </asp:TableCell>
                 </asp:TableRow>
                 <asp:TableRow>
                 <asp:TableCell>
                 <hr />
                 <asp:Label ID="lblOutput" runat="server"></asp:Label>
                 </asp:TableCell>
                 </asp:TableRow>
                 </asp:Table>
                   
              </ContentTemplate>
           </asp:UpdatePanel>
     With corresponding code-behind like this: 
       Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
          lblOutput.Text = "Button 1 pushed"
       End Sub
    
       Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
          lblOutput.Text = "Button 2"
       End Sub
    

    This updates the label without requiring the whole page to reload. What I would like to do is to replace the buttons with hyperlinks (so I don't have the button graphics to deal with and I can make the text look any way I want -- or maybe there's a way to do that with an asp:button?).

    Owen
  • Re: Hyperlink as postback controller...

    07-26-2007, 8:34 PM

    Hi There,

    You can use <asp:linkbutton it behave the same as button.

    Example:

    <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>

     Code-Behind

       Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
          lblOutput.Text = "Button 1 pushed"
       End Sub

    DC517
    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved.
    Filed under: ,
  • Re: Hyperlink as postback controller...

    07-27-2007, 10:53 AM
    • Contributor
      2,042 point Contributor
    • ojm37
    • Member since 07-30-2003, 7:31 PM
    • AZ
    • Posts 615

    This does the same thing as a hyperlink: i.e., forces the whole page to postback, even though it's inside an update-panel control. Unless there's some option to allow it to work inside the update panel....

    Owen
  • Re: Hyperlink as postback controller...

    07-27-2007, 1:17 PM
    • Member
      59 point Member
    • ChrisCicc
    • Member since 06-16-2007, 2:07 PM
    • Posts 37

    Hi, you need to add to your updatepanel declaration the following:  UpdateMode="Conditional" and ChildreAsTriggers="false". this should solve your problem. All ASP.net button controls have the same behavior in this regard.

  • Re: Hyperlink as postback controller...

    07-27-2007, 1:30 PM
    • Contributor
      2,042 point Contributor
    • ojm37
    • Member since 07-30-2003, 7:31 PM
    • AZ
    • Posts 615

    That had no effect on the page-reload.

    Owen
  • Re: Hyperlink as postback controller...

    07-27-2007, 1:47 PM
    • Member
      59 point Member
    • ChrisCicc
    • Member since 06-16-2007, 2:07 PM
    • Posts 37

    hmm that is odd, i don't think it should be happening. Try manually setting the button as a asp:asyncpostbacktrigger. i can't remember if that will work for nested controls or not. 

  • Re: Hyperlink as postback controller...

    07-27-2007, 1:56 PM
    • Member
      59 point Member
    • ChrisCicc
    • Member since 06-16-2007, 2:07 PM
    • Posts 37

    I tried replicating your problem with the code your providing, it worked as expected for me without alterations. I added a simple test to be sure, see below:

    1    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
    2    
    3    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    4    
    5    <html xmlns="http://www.w3.org/1999/xhtml" >
    6    <head runat="server">
    7        <title>Untitled Page</title>
    8    </head>
    9    <body>
    10       <form id="form1" runat="server">
    11       <div>
    12           <asp:ScriptManager ID="ScriptManager1" runat="server">
    13           </asp:ScriptManager>
    14           <asp:UpdatePanel ID="UpdatePanel2" runat="server">
    15             <ContentTemplate>
    16               <asp:Table ID="Table1" BorderStyle="None" runat="Server">
    17                  <asp:TableRow>
    18                  <asp:TableCell ID="tcButtonCell">
    19                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    20                <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
    21                </asp:TableCell>
    22                </asp:TableRow>
    23                <asp:TableRow>
    24                <asp:TableCell>
    25                <hr />
    26                <asp:Label ID="lblOutput" runat="server"></asp:Label>
    27                </asp:TableCell>
    28                </asp:TableRow>
    29                </asp:Table>
    30                  
    31             </ContentTemplate>
    32          </asp:UpdatePanel>
    33          <asp:Label ID="time" runat="server"></asp:Label>
    34       </div>
    35       </form>
    36   </body>
    37   </html>
    
       
    1    public partial class Default3 : System.Web.UI.Page
    2    {
    3        protected void Page_Load(object sender, EventArgs e)
    4        {
    5            time.Text = DateTime.Now.ToString();
    6        }
    7    
    8        protected void Button1_Click(object sender, EventArgs e)
    9        {
    10           lblOutput.Text = "Button 1 pushed";
    11       }
    12   
    13       protected void Button2_Click(object sender, EventArgs e)
    14       {
    15           lblOutput.Text = "Button 2 pushed";
    16       }
    17   }
    
     

     if you are still having that problem, I'm guessing you have a nested update panel or something else going on?

     

  • Re: Hyperlink as postback controller...

    07-27-2007, 2:06 PM
    • Contributor
      2,042 point Contributor
    • ojm37
    • Member since 07-30-2003, 7:31 PM
    • AZ
    • Posts 615

    OK, so how do I tell the updatepanel that my programmatically created link button is the async trigger for the update panel (programmatically, of course)?

    Owen
  • Re: Hyperlink as postback controller...

    07-27-2007, 2:10 PM
    • Contributor
      2,042 point Contributor
    • ojm37
    • Member since 07-30-2003, 7:31 PM
    • AZ
    • Posts 615

    EXACTLY: with asp:button controls it works fine.  I want to change the asp:button controls to either asp:HyperLink controls or asp:LinkButton controls and get the same functionality.

    Owen
  • Re: Hyperlink as postback controller...

    07-27-2007, 2:36 PM
    • Contributor
      2,460 point Contributor
    • Steve Marx
    • Member since 05-26-2006, 8:35 PM
    • Microsoft
    • Posts 643

    You can't use a HyperLink; that control navigates to a new page.

    LinkButton should work exactly the same as your button.  I copy/pasted your code and just changed "Button" to "LinkButton" and it worked fine on my machine.  If this same code is not working on your computer, it suggests that something else is wrong.

     

    <%@ Page Language="VB" AutoEventWireup="true" %>
    
    <script runat="server">
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            lblOutput.Text = "Button 1 pushed"
        End Sub
    
        Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            lblOutput.Text = "Button 2"
        End Sub
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
              <ContentTemplate>
                <asp:Table ID="Table1" BorderStyle="None" runat="Server">
                   <asp:TableRow>
                   <asp:TableCell ID="tcButtonCell">
                 <asp:LinkButton ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                 <asp:LinkButton ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
                 </asp:TableCell>
                 </asp:TableRow>
                 <asp:TableRow>
                 <asp:TableCell>
                 <hr />
                 <asp:Label ID="lblOutput" runat="server"></asp:Label>
                 </asp:TableCell>
                 </asp:TableRow>
                 </asp:Table>
                   
              </ContentTemplate>
           </asp:UpdatePanel>
        </form>
    </body>
    </html>
    
     
    Steve Marx | ASP.NET AJAX Evangelist | Microsoft Corporation
  • Re: Hyperlink as postback controller...

    07-27-2007, 2:46 PM
    • Member
      59 point Member
    • ChrisCicc
    • Member since 06-16-2007, 2:07 PM
    • Posts 37

    I use many <asp:Hyperlink> and <asp:LinkButton> and <asp:Button> and <asp:ImageButton> controls in my app within updatepanels (my entire app runs without a postback) without issue. the code you provided should run fine without alteration... 

     The only difference I think between a LinkButton and a HyperLink is how it is rendered, not how the links relate to the updatepanel

     

    Edit: Steve, I current have asp:Hyperlinks within a updatepanel that do two things without navigating to a new page, one is a trigger for a modal popup, and the other switches a formview mode without a post back... 

  • Re: Hyperlink as postback controller...

    07-27-2007, 3:11 PM
    • Contributor
      2,460 point Contributor
    • Steve Marx
    • Member since 05-26-2006, 8:35 PM
    • Microsoft
    • Posts 643

    Chris, what's the NavigateUrl for your HyperLink?  And how do you handle the click event?  (There's no "OnClick" for a HyperLink.)  I think you must be doing something special to get this to work... normally a HyperLink doesn't perform a postback.

    In either case, a LinkButton is a better fit when you're trying to do a postback.

    Steve Marx | ASP.NET AJAX Evangelist | Microsoft Corporation
  • Re: Hyperlink as postback controller...

    07-27-2007, 3:48 PM
    • Member
      59 point Member
    • ChrisCicc
    • Member since 06-16-2007, 2:07 PM
    • Posts 37

    ahhh now i see the difference, i didn't even notice because i was handling the hyperlinks with javascript! I never tried to attach an asp.net onclick handler...and i also realize the modal popup control and formview mode changers are linkbuttons, not hyperlinks as i stated earlier. thanks for filling me in...

    that makes me think, i've been trying to figure out why my asp:hyperlink with a NavigateUrl='<%# Eval("Website", "{0}") >%' or NavigateUrl='<%# Eval("Website") >%'  are always resolving back to the current directory if the link is formed say, "www.link.com", but if it is "http://www.link.com" it resolves properly....is this because of the hyperlink vs linkbutton differences?

     

     

  • Re: Hyperlink as postback controller...

    07-27-2007, 3:59 PM
    • Contributor
      2,460 point Contributor
    • Steve Marx
    • Member since 05-26-2006, 8:35 PM
    • Microsoft
    • Posts 643

    LinkButton's don't even have a NavigateUrl.  They just do a postback to the current page.

    HyperLinks point to another page.  They just render out a <a href="...">...</a> tag to the client.  Browsers interpret the href as a relative URL unless otherwise specified, so that's why you see the behavior you described.

    Steve Marx | ASP.NET AJAX Evangelist | Microsoft Corporation
  • Re: Hyperlink as postback controller...

    07-27-2007, 4:39 PM
    • Contributor
      2,042 point Contributor
    • ojm37
    • Member since 07-30-2003, 7:31 PM
    • AZ
    • Posts 615

    OK. That works:

    Steve Marx:
    <%@ Page Language="VB" AutoEventWireup="true" %>
    
    <script runat="server">
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            lblOutput.Text = "Button 1 pushed"
        End Sub
    
        Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            lblOutput.Text = "Button 2"
        End Sub
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
              <ContentTemplate>
                <asp:Table ID="Table1" BorderStyle="None" runat="Server">
                   <asp:TableRow>
                   <asp:TableCell ID="tcButtonCell">
                 <asp:LinkButton ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                 <asp:LinkButton ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
                 </asp:TableCell>
                 </asp:TableRow>
                 <asp:TableRow>
                 <asp:TableCell>
                 <hr />
                 <asp:Label ID="lblOutput" runat="server"></asp:Label>
                 </asp:TableCell>
                 </asp:TableRow>
                 </asp:Table>
                   
              </ContentTemplate>
           </asp:UpdatePanel>
        </form>
    </body>
    </html>
    

     

    However, when I add the linkButton in code, it posts-back the page....

    Here's the code to add the linkButton in code:

          objHyperLink = New LinkButton
          objHyperLink.Text = "Button 9"
          objHyperLink.Attributes.Add("SuppID", "9")
          AddHandler objHyperLink.Click, AddressOf ButtonX_Click
    
          tcButtonCell.Controls.Add(objHyperLink)
    

     

    When I add it this way (in code), it causes a full postback. (I can add a "Button" control exactly this way in code and it does not cause the full postback....

    Owen
Page 1 of 2 (18 items) 1 2 Next >