如何添加javascript代码到LinkButton通过服务器代码c#
How to add javascript code to a LinkButton via server code c#?
我正在创建一个带有链接按钮的可视化web部件,链接按钮应该打开一个模态弹出窗口。
我已经定义了函数,但我不确定如何使链接按钮触发javascript函数。
请参阅CreateChildControls方法
<asp:Content ID="Content1" ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript">
function dialogfunction(pageUrl) {
var options = { url: pageUrl, width: 900, height: 300 };
SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);
}
</script>
</asp:Content>
<asp:LinkButton ID="LnkButton" runat="server"></asp:LinkButton>
public partial class LinkButton : WebPart
{
// Visual Studio might automatically update this path when you change the
// Visual Web Part project item.
private const string _ascxPath =@"~/_CONTROLTEMPLATES/VisualWebParts/LinkButton/LinkButton.ascx";
private string _LinkText;
private Uri _Link;
[WebBrowsable(true), WebDisplayName("LinkText"), WebDescription("Text for the link"),
Personalizable(PersonalizationScope.Shared), Category("xx- xx"),
System.ComponentModel.DefaultValue("")]
public string LinkText
{
get { return _LinkText; }
set { _LinkText = value; }
}
[WebBrowsable(true), WebDisplayName("Link"), WebDescription("Link"),
Personalizable(PersonalizationScope.Shared), Category("xx- xx"),
System.ComponentModel.DefaultValue("")]
public Uri Link
{
get { return _Link; }
set { _Link = value; }
}
// Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
// using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
// for production. Because the SecurityPermission attribute bypasses the security check for callers of
// your constructor, it's not recommended for production purposes.
// [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
public LinkButton()
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
InitializeControl();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void CreateChildControls()
{
LinkButton linkButton = Page.LoadControl(_ascxPath) as LinkButton;
if (linkButton != null)
{
linkButton.Title = LinkText;
linkButton.TitleUrl = Link.ToString();
Controls.Add(linkButton);
}
}
}
只需在代码中添加一行:
linkButton.Attributes.Add("onclick", "dialogfunction('" + Link.ToString() + "');");
你可以设置你的链接按钮的onclick属性为javascript:
protected override void CreateChildControls()
{
LinkButton linkButton = Page.LoadControl(_ascxPath) as LinkButton;
if (linkButton != null)
{
linkButton.Title = LinkText;
linkButton.TitleUrl = Link.ToString();
/*************************************************************/
/* you can replace "alert('test');" with any javascript
i.e a function call */
linkButton..Attributes.Add("onclick", "alert('test'); ");
/*************************************************************/
Controls.Add(linkButton);
}
}
你可以定义一个LinkOnClientClick
属性,类似于你的LinkText
属性,并使用它来设置LinkButton的OnClientClick
属性。
public string LinkOnClientClick
{
get; set;
}
protected override void CreateChildControls()
{
LinkButton linkButton = Page.LoadControl(_ascxPath) as LinkButton;
if (linkButton != null)
{
linkButton.Title = LinkText;
linkButton.TitleUrl = Link.ToString();
linkButton.OnClientClick = LinkOnClientClick;
Controls.Add(linkButton);
}
}
相关文章:
- 从桌面读取python文件时高亮显示代码
- 如何将函数包装在函数中以避免代码重复
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 货币代码为欧元-金额的格式不应包含小数
- Regex代码只允许一个空格
- 如何将屏幕分辨率乘以 80%,然后在代码中使用
- 从var向代码隐藏函数传递值
- 如何动态插入jquery代码
- 如何在php变量中嵌入JQuery代码
- 代码不会验证
- 如何从rails中的代码中删除新行( )
- Angularjs代码未在匿名函数中运行
- 如何做到这一点,使代码在不传递条件后执行函数
- 如何在读取XLS/XLSX本地文件时,使用IE的javascript代码启用未标记为安全的ActiveX控件
- 面向对象的Javascript代码在IE7中不起作用
- 将 jQuery 代码添加到 Index.aspx 页面
- 为什么忽略了eval()代码中的语法错误
- 元素在我的代码中不会.fadeTo.Don'I don’我不知道;s错误的JavaScript、JQuery、H
- asp:LinkButton通过文字控件创建,不会在后面触发代码
- 如何添加javascript代码到LinkButton通过服务器代码c#