【问题标题】:call javascript from server side on link button telerik在链接按钮telerik上从服务器端调用javascript
【发布时间】:2017-05-08 18:41:07
【问题描述】:

我是 Telerik 和 javascript 的新手。我有一个链接按钮。在按钮单击时,我有来自服务器端的两个条件。根据我需要调用javascript。

aspx代码:

<asp:LinkButton ID="TestLinkButton" runat="server" OnClick="Test_Click" 
SkinID="SmallCommandItemTemplateLinkButton">Test</asp:LinkButton>                                                           

服务器端代码:

protected void Test_Click(object sender, EventArgs e)
{
    if (a == 0)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptsKey", "<script type=\"text/JavaScript\" language=\"javascript\">ShowAlert();</script>");
    }
    else
    {
        TestLinkButton.Attributes["onclick"] = String.Format("return ShowEditForm('{0}')", test);
    }
}

这里找不到TestLinkBut​​ton。如果我在按钮单击参数中使用 GridItemEventArgs,那么它会给出错误,因为“testLinkBut​​ton_click”的无重载与委托“System.Eventhandler”匹配。

【问题讨论】:

  • 我不熟悉 Telerik,但我认为这与事件委托有关。 google.nl/amp/s/davidwalsh.name 。阅读它,您可能会找到解决方案!
  • 你能分享一些想法吗
  • 我给了它一个赏金,所以比我更有经验的人可以回答这个问题。就像我说的,我不熟悉 Telerik。
  • 这与telerik无关。
  • @DIPALISAVALIYA - 一个或多个答案是否对您有任何帮助?如果是这样,您可以投票赞成您喜欢的答案并接受您喜欢的答案(带有复选标记)。我从您的个人资料中看到,您对 StackOverflow 还比较陌生,还没有养成支持/接受问题答案的习惯。

标签: javascript asp.net telerik


【解决方案1】:

可以从Test_Click 事件处理程序的sender 参数中检索LinkBut​​ton,并且可以将Javascript 代码附加到其OnClientClick 属性:

LinkButton lnkButton = sender as LinkButton;
lnkButton.OnClientClick = String.Format("return ShowEditForm('{0}')", test);

【讨论】:

    【解决方案2】:

    我可能错了,但我认为您正在寻找一种将 javascript onclick 附加到 GridView 中的 LinkButton 的方法。您似乎是在 LinkBut​​ton 的服务器 OnClick 上执行此操作,但这需要往返服务器 (PostBack)。

    您可以在 GridView 的 OnItemDataBound 事件中执行相同的操作。您需要将OnItemDataBound 添加到GridView。

    <telerik:RadGrid ID="RadGrid1" runat="server" OnItemDataBound="RadGrid1_ItemDataBound" >
    

    后面的代码

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        //check if the item is a GridDataItem
        if (e.Item is GridDataItem)
        {
            //if 'a' needs to come from the dataset that is bound to the gridview, you can do this
            GridDataItem item = (GridDataItem)e.Item;
            string a = Convert.ToInt32(item["ID"].Text);
    
            //or another grid value
            string b = item["name"].Text;
    
            //find the linkbutton with findcontrol and cast it back to one
            LinkButton linkbutton= e.Item.FindControl("TestLinkButton") as LinkButton;
    
            if (a == 0)
            {
                //attach the OnClientClick click function
                linkbutton.OnClientClick = string.Format("ShowDeleteForm('{0}'); return false;", test2);
            }
            else
            {
                //attach the OnClientClick click function
                linkbutton.OnClientClick = string.Format("ShowEditForm('{0}'); return false;", test);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      • 2011-11-03
      • 2021-03-27
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      相关资源
      最近更新 更多