Using GetPostBackClientHyperlink to raise LinkButton's OnCommand e

G

Guest

Hello,

I have a scenario whereby a LinkButton control needs to be called by a
client-side HtmlAnchor element in ASP.NET 2.0. I am using the
GetPostBackClientHyperlink() method to retrieve the postback JavaScript
expected by the LinkButton. I wish to pass an argument to the LinkButton so I
use the second overloaded GetPostBackClientHyperlink() method with a
particular value. I have an event handler to capture the OnCommand event
being raised on the LinkButton and write the associated CommandArgument to
the Page.

The problem is that the argument specified in the
GetPostBackClientHyperlink() method is not getting through to the OnCommand
event. The value specified originally in the LinkButton (in the code below
this is empty) is retrieved instead.

The Web Form code is as follows:

1 <%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="WebForm1.aspx.cs" Inherits="Stuff.WebForm1" %>
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:LinkButton ID="LinkButton1" runat="server"
OnCommand="LinkButton1_Command">LinkButton</asp:LinkButton>
13 <asp:placeHolder ID="PlaceHolder1"
runat="server"></asp:placeHolder>
14 </div>
15 </form>
16 </body>
17 </html>

and the code behind:

1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 namespace Stuff
13 {
14 public partial class WebForm1 : System.Web.UI.Page
15 {
16 protected override void Render(HtmlTextWriter writer)
17 {
18 // allow another control to postback to this control with
the argument 'Hello World'
19
Page.ClientScript.RegisterForEventValidation(LinkButton1.UniqueID, "Hello
World");
20
21 base.Render(writer);
22 }
23
24 protected void Page_Load(object sender, EventArgs e)
25 {
26 HtmlAnchor l_anchor = new HtmlAnchor();
27 l_anchor.ID = "Anchor";
28 l_anchor.InnerText = "Click Me";
29 // get the javascript for the postback to the LinkButton
30 l_anchor.HRef =
Page.ClientScript.GetPostBackClientHyperlink(LinkButton1, "Hello World");
31 PlaceHolder1.Controls.Add(l_anchor);
32 }
33
34 protected void LinkButton1_Command(object sender,
CommandEventArgs e)
35 {
36 // write the value to the page
37 Response.Write("-" + e.CommandArgument.ToString() + "-");
38 }
39 }
40 }

Please can someone advise how I may successfully pass the argument through
to the LinkButton's OnCommand event? I have read through some of the examples
on MSDN (e.g. http://msdn2.microsoft.com/en-us/library/ms153108(vs.80).aspx)
but can't find a solution to this particular problem.

Thanks in advance,

Marc
 
M

Marc Woolfson

I was attempting to retrieve the wrong value in the LinkButton1_Command
handler. The CommandArgument will always be the server-side provided value
(i.e. always blank above) and what I should have been retrieving was the
Event Argument posted back to the page by the client. To get this argument, I
checked and retrieved the appropriate values from the Request.Params
collection as can be seen below:

1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 namespace Stuff
13 {
14 public partial class WebForm1 : System.Web.UI.Page
15 {
16 private string m_eventArgument = "";
17
18 public string EventArgument
19 {
20 get
21 {
22 return m_eventArgument;
23 }
24 set
25 {
26 m_eventArgument = value;
27 }
28 }
29
30 protected override void Render(HtmlTextWriter writer)
31 {
32 // allow another control to postback to this control with
the argument 'Hello World'
33
Page.ClientScript.RegisterForEventValidation(LinkButton1.UniqueID, "Hello
World");
34
35 base.Render(writer);
36 }
37
38 protected void Page_Load(object sender, EventArgs e)
39 {
40 HtmlAnchor l_anchor = new HtmlAnchor();
41 l_anchor.ID = "Anchor1";
42 l_anchor.InnerText = "Click Me";
43 // get the javascript for the postback to the LinkButton
44 l_anchor.HRef =
Page.ClientScript.GetPostBackClientHyperlink(LinkButton1, "Hello World");
45 PlaceHolder1.Controls.Add(l_anchor);
46
47 // get the value from the postback parameters
48 if (Request.Params["__EVENTTARGET"] ==
LinkButton1.UniqueID) {
49 EventArgument = Request.Params["__EVENTARGUMENT"];
50 }
51 }
52
53 protected void LinkButton1_Command(object sender,
CommandEventArgs e)
54 {
55 // write the value to the page
56 Response.Write("-" + EventArgument + "-");
57 }
58 }
59 }

The handler on the LinkButton can now be changed to the OnClick instead of
OnCommand, as the CommandEventArgs are no longer being used.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top