[英]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() + "');");
您可以將linkbutton的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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.