Dotnetnuke ASP.NET中的ParseControl将onclick更改为javascript而不是提供的c#方法,奇怪的行为 [英] ParseControl in Dotnetnuke ASP.NET changing onclick to javascript instead of c# method provided, strange behavior
问题描述
这里有一些奇怪的行为.我有一些XSLT,它会生成带有一些ASP.NET链接按钮控件的html
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());
}
现在就可以控制了:
<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>
现在您可以看到,它将onclick更改为类似于javascript而不是asp.net控件.它将"LoadPromo"更改为"LoadPromo;".我该怎么办才能解决这个问题?
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?
预先感谢
我尝试过的事情:
lb.Click += LoadPromo;
我得到以下输出:
<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>
推荐答案
OnClientClick
是服务器控件的属性名称,该控件最终呈现为"onclick"属性.这是为了在客户端调用javascript函数.因此,在这种情况下,看起来不错.
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.
对于OnClick属性,您需要使其与Page.aspx中服务器端函数的名称匹配.我不知道为什么还要为此渲染onclick属性-也许您没有带有正确签名的名为"LoadPromo"的函数?
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?
您应该具有这样的C#方法:
You should have a C# method like this:
protected void LoadPromo(object sender, EventArgs e)
{
}
然后您可以通过
myLinkButton.Click += LoadPromo;
这篇关于Dotnetnuke ASP.NET中的ParseControl将onclick更改为javascript而不是提供的c#方法,奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!