Hi All,
We can use Command name and argument for web controls such as imagbutton or button etc..
But Can we use Command name and argument for html tags such as(span,a)?

It's urgent.
Please put some light on it..
Thanks

Dani AI

Generated

Brief answer tied to the existing thread: pure HTML elements (for example span or a) do not provide the Web Forms "Command" pattern out of the box because that pattern comes from server controls that implement ICommandSource. As pointed out, making an element server-accessible gives you server-side access to its properties, but it does not automatically add CommandName/CommandArgument semantics. There are three practical approaches depending on whether you can switch to a server control or must keep the original HTML element.

Use a server control that supports commands (recommended)

<asp:LinkButton ID="lnkAction" runat="server"
    CommandName="DoIt" CommandArgument="42" OnCommand="HandleCommand">
    Run
</asp:LinkButton>

protected void HandleCommand(object sender, CommandEventArgs e)
{
    // e.CommandName / e.CommandArgument available here
}

Keep the HTML element but forward a command to the server

<a id="aCmd" runat="server" href="#" OnServerClick="A_ServerClick" data-arg="42">Run</a>

protected void A_ServerClick(object sender, EventArgs e)
{
    var arg = ((HtmlAnchor)sender).Attributes["data-arg"];
    // handle arg
}

Client-driven postback / single handler

<a href="javascript:__doPostBack('MyTarget','cmd:42')">Run</a>

// server: read Request["__EVENTTARGET"] and Request["__EVENTARGUMENT"]

Troubleshooting notes (why converting might "not work"): ensure the control sits inside the server form, event handler signatures match (e.g., CommandEventArgs vs EventArgs), client-side JS isn't preventing postback, and IDs/naming containers are what you expect. If you need many clickable semantic elements, prefer generating them as server LinkButtons or use a single AJAX/WebMethod endpoint and send a small command payload from the client. This keeps intent clear and is easiest to maintain.

Recommended Answers

All 4 Replies

If you add the runat="server" to the HTML elements, you can access them in your code behind. That doesn't mean you will have access to methods and properties for those elements.

For example on the span element, you'd be able to access the innerHTML property.

Hi JorgeM,
First of all thanks for your answer.
You are totally right but i want to use Properties too. i have tried with converting HTML tag into aspx but it didn't work for me.
If you have any better suggestion please let me know.

So, i dont beleive that you can do this with HTML controls. Why not use a <asp:label /> control in place of a <span> element. They will both render as <span> elements in the browser... However, i am not really clear on what you are trying to do... With regard to the button, imagebutton, linkbutton, there are command name and arguments because it makes sense on that type of control. You could use one control and do different things within the event based on the parameter you pass. I dont see how that will be of any use on a span element.

Yes jorgeM
You are right, i have to use aspx controls only. just i wanted to know is there any way to do with html tags. i have tried many things but didn't get it properly.. anyways thanks

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.