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 <asplaceHolder ID="PlaceHolder1"
runat="server"></asplaceHolder>
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
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 <asplaceHolder ID="PlaceHolder1"
runat="server"></asplaceHolder>
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