使用JavaScript单击RadGrid中的链接

Clicking a link in RadGrid using JavaScript
2020-11-25
  •  译文(汉语)
  •  原文(英语)

我使用RadGrid绑定记录,并且每行都有一个链接按钮.如果单击链接,它将打开moalpopup(根据某些条件它是动态的).当我使用关闭按钮关闭弹出窗口时,它应继续执行链接按钮事件.

背后的代码:

protected void grid_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item.ItemType == GridItemType.AlternatingItem || e.Item.ItemType == GridItemType.Item)
    {
        LinkButton link = ((LinkButton)gridDataItem.FindControl("link"));
        ModalPopupExtender popup = (ModalPopupExtender)e.Item.FindControl("popup");
        Image imageClose = (Image)e.Item.FindControl("imageClose");
        popup.TargetControlID = link.ID;
        link.Attributes.Add("onclick", "return false;"); // to avoid postback and stay in page and show popup
        imageClose.Attributes.Add("onclick", "proceed('" + link.ClientID + "')");
    }
}

Javascript:

function proceed(id)
{
    window.document.getElementById(id).removeAttribute("onclick"); // Link button - to remove "onclick" attribute
    window.document.getElementById(id).click(); //  trigger click event
}

我尝试使用上面的代码,未删除该属性,并且未继续执行链接按钮事件.

解决过程1

最后我做了.正如我所期望的那样,这一功能非常完美.

function proceed(id)
{
    document.location.href = $('#' + id).attr("href");
}

I bind records using RadGrid, and each row it has a link button. If I click the link, it will open the moalpopup (it is dynamic one based on some condition). When I close the popup using close button, it should continue with the link button event.

Code Behind:

protected void grid_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item.ItemType == GridItemType.AlternatingItem || e.Item.ItemType == GridItemType.Item)
    {
        LinkButton link = ((LinkButton)gridDataItem.FindControl("link"));
        ModalPopupExtender popup = (ModalPopupExtender)e.Item.FindControl("popup");
        Image imageClose = (Image)e.Item.FindControl("imageClose");
        popup.TargetControlID = link.ID;
        link.Attributes.Add("onclick", "return false;"); // to avoid postback and stay in page and show popup
        imageClose.Attributes.Add("onclick", "proceed('" + link.ClientID + "')");
    }
}

Javascript:

function proceed(id)
{
    window.document.getElementById(id).removeAttribute("onclick"); // Link button - to remove "onclick" attribute
    window.document.getElementById(id).click(); //  trigger click event
}

I tried with the above code, the attribute not removed and not continue with link button event.

Solutions1

Finally I have done. This one worked perfectly as I expected.

function proceed(id)
{
    document.location.href = $('#' + id).attr("href");
}
转载于:https://stackoverflow.com/questions/28060957/clicking-a-link-in-radgrid-using-javascript

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入