How to use commandName in hyperlink control similar to Link button for some backend process.
I want Text of Hyperlink Which is clicked From a group of Hyperlinks to identify which hyperlink is clicked and based on it some backend code should be executed.How to retrieve the Text of Hyperlink when clicked.

Thanks,
A.Sushmaja

Dani AI

Generated

For the master-page menu / submenu scenario described by , the most reliable server-side pattern is to pass a stable ID (not the visible text) and trigger a server event. That preserves styling while making backend logic simple and robust. This follows 's direction but expands with practical snippets and alternatives for cases where strict HyperLink controls must be kept.

A common, clean solution is LinkButton inside the data repeater and a Command handler. LinkButton renders as an anchor, so existing CSS can be reused and an UpdatePanel or AJAX can be added for a partial update:

<asp:Repeater ID="rptMain" runat="server">
  <ItemTemplate>
    <asp:LinkButton
      ID="lnkMenu"
      runat="server"
      Text='<%# Eval("MenuText") %>'
      CommandArgument='<%# Eval("MenuID") %>'
      CommandName="SelectMenu"
      OnCommand="MainMenu_Command"
      CssClass="mainMenuLink" />
  </ItemTemplate>
</asp:Repeater>
protected void MainMenu_Command(object sender, CommandEventArgs e)
{
    if (e.CommandName == "SelectMenu")
    {
        int menuId = Convert.ToInt32(e.CommandArgument);
        BindSubMenu(menuId); // load submenu from DB and bind to placeholder/repeater
    }
}

If an actual HyperLink must be used for markup reasons, alternatives include: 1) Navigate to the same page with a query string (read Request.QueryString and bind), 2) emit a small client script that calls doPostBack with an ID and inspect EVENTARGUMENT on postback, or 3) use client-side AJAX to fetch submenu JSON and render it. Important note: prefer passing an ID/Key rather than link text (localization, whitespace, casing and HTML entities make text unreliable). 's data-binding attempts may help set attributes, but passing a numeric ID is safer.

Recommended Answers

All 4 Replies

The problem is that the hyperlink control will redirect to some other page when you click on it. that's how hyperlinks work so you wouldnt be able to capture that information on the postback because there isnt a post back with a hyperlink control.

You need to use a linkbutton control instead.

Yes But i need it in Hyperlink as the styles are written accordingly.
The scenario is that In master page I have one main Menu.Based on the selection of Menu the submenu should be populated below it from database.SO i need the text which was selected.Please provide alternate solution for this problem.(The main Menu is written in Hyperlinks for having various atyles for it.)

LinkButton controls render as HTML <a> hyperlinks on the browser so that is the appropriate solution.

ASP.NET handles all of this via javascript and the post back via the form. Take a look at the source view using your browser.

The other alternative is to do something client side using javascript and AJAX. When a hyperlink is clicked, rather than allowing the normal event to trigger, send the hyperlinks ID back to the server, perform some process, send the results back to the browser, finish processing client side.

anthor solv:
not working hyperlink command name

:: hyperlink1.ToolTip='<%#Eval("ID")%>' ::

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.