views:

196

answers:

1

Having some strange behavior here. I have some XSLT which generates some html with a few ASP.NET Link Button Controls

 String mstring = sw.ToString();
 var myctrl = Page.ParseControl(mstring);

      foreach (Control Control in myctrl.Controls)
              {
                  if (Control is LinkButton)
                      {
                         LinkButton lb = (LinkButton)Control;
                         lb.OnClientClick = "LoadPromo";

                       }
                          Panel1.Controls.Add(myctrl);
               }  

  protected void LoadPromo(object sender, EventArgs e)
        {
            Console.Write(e.ToString());

        }

now it takes this control:

<asp:LinkButton ID="LinkButton2" runat="server" OnClick="LoadPromo" Text="Get your Free 2" />

and changes it to this when I add an event:

<a onclick="LoadPromo;" id="dnn_ctr954_ViewPromotions_LinkButton2" href=" WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("dnn$ctr954$ViewPromotions$LinkButton2", "", true, "", "", false, true))">Get your Free 2 a>

Now as you can see, it changes the onclick to like javascript instead of the asp.net control.It changes "LoadPromo" to "LoadPromo;". What can i do to get around this?

Thanks In Advance

Things I have tried:

 lb.Click += LoadPromo;

I get this output:

<a id="dnn_ctr954_ViewPromotions_LinkButton2" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;dnn$ctr954$ViewPromotions$LinkButton2&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))">Get your Free 2</a>
+3  A: 

OnClientClick is the name of the property of a server control that eventually renders to an "onclick" attribute. This is meant to invoke a javascript function on the client side. So in this case, it looks fine.

For the OnClick property, you need to have that match the name of a server-side function in your Page.aspx. I'm not sure why it would even be rendering the onclick attribute for this - perhaps you don't have a function named "LoadPromo" with the right signature?

You should have a C# method like this:

protected void LoadPromo(object sender, EventArgs e)
{

}

Then you can wire this up to your button by doing

myLinkButton.Click += LoadPromo;
womp
Tried this too and it doesxn't work. When I try that I get this: a id="dnn_ctr954_ViewPromotions_LinkButton2" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("dnn$ctr954$ViewPromotions$LinkButton2", "", true, "", "", false, true))">Get your Free 2</a>
James Campbell
Yes, that's what it should do. WebForm_DoPostBackWithOptions() is the javascript function that causes a post back that will execute LoadPromo() in your codebehind.
jrummell
I also need to apply on post back also and this fixed problem: if (Page.IsPostBack || !Page.IsPostBack)
James Campbell