简体   繁体   English

GridView OnSelectedIndexChanged调用JavaScript函数

[英]GridView OnSelectedIndexChanged call JavaScript function

Can I call a JS function on the SelectedIndexChanged event of a GridView? 我可以在GridView的SelectedIndexChanged事件上调用JS函数吗? I referred these 2 SO posts - Post 1 and Post 2 that address this but they aren't helping. 我提到了这两个SO帖子 - 帖子1帖子2解决了这个问题,但他们没有帮助。

I tried 我试过了

OnSelectedIndexChanged="selectedindexchanged()"

however, in return I recieved - 然而,作为回报,我收到了 -

'ASP.default_aspx' does not contain a definition for 'selectedindexchanged' and no 
extension method 'selectedindexchanged' accepting a first argument of type  
'ASP.default_aspx' could be found (are you missing a using directive or an 
assembly reference?)

Can anyone confirm whether this can be done and if it can the right way to do it? 任何人都可以确认这是否可以做到,是否可以正确的方式做到这一点?

OnSelectedIndexChanged is a server event , not a javascript event. OnSelectedIndexChanged服务器事件 ,而不是javascript事件。

the Server Control GridView does not allow, out-of-the-box javascript methods to be hooked on. Server Control GridView不允许使用开箱即用的javascript方法。

You need to extend it and create such methods, or use other Grid Control available as a 3rd party. 您需要扩展它并创建此类方法,或使用其他网格控件作为第三方提供。


What you can do, so you don't mess up to much in the code, is, using jQuery, for example, hook up to all rows and fire an event when something on that row was clicked, holding the return (submission of the form back to the server). 你可以做什么,所以你不要在代码中搞砸了,例如,使用jQuery,挂钩所有行并在点击该行上的某些内容时触发事件,保持返回(提交形成回服务器)。

For that, you need to see what's the output HTML and start from there. 为此,您需要查看输出HTML的内容并从那里开始。

This was one of many features that I moved to MVC instead of keep using WebForms, in MVC you have total control in what's happening in your page, and you can do everything by yourself without ever wondering of such things. 这是我转移到MVC的许多功能之一,而不是继续使用WebForms,在MVC中,您可以完全控制页面中发生的事情,并且您可以自己做所有事情,而不会想到这些事情。

Use Row Data Bound Event to bind your java script function, 使用行数据绑定事件绑定您的java脚本函数,

protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                e.Row.Attributes.Add("onclick", "selectedindexchanged()");
            }
        }

Now when you click on a grid view row, the selectedindexchanged will be called. 现在,当您单击网格视图行时,将调用selectedindexchanged

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM