【转】Asp.Net服务器控件添加OnClientClick属性绑定

出处:http://www.itwis.com/html/net/aspnet/20101111/9533.html

Asp.Net服务器控件添加OnClientClick属性绑定,在应用Asp.net服务器控件时经常会用到OnClientClick执行脚本的问题,今天就遇到OnClientClick绑定数据集中的某些列,然后调用JS脚本的问题,baidu+google研究总结常用绑定方式如下:

   A: OnClientClick='<%#String.Format("return jsf(\"{0}\",\"{1}\")",Eval("domain"),Eval("siteid")) %>'>

   B: OnClientClick= '<%#Eval("domain","return jsf(\"{0}\")") %>'

   C:  OnClientClick='<%# "javascript:return jsf(" + Eval("id") + ");" %>'

  domain、siteid、id都是外层数据集中的列名,jsf为javascript函数名。

注意学会用 String.Format 不要去 ++++
 注意 " ' 转义,特别 js 与 C# 混用
==========================================================================================================================

下面是我自己做项目时的写法,可以成功!

  <ItemTemplate>
                                    <input type="checkbox" data="<%# Eval("ID") %>" name="GridItemsList" onclick="BRC.ChkBox.CheckGroup(this, 'GridItemsList_chkAll')" />
                                </ItemTemplate>
                                <HeaderStyle Width="48px" />
                            </asp:TemplateColumn>
                            <asp:BoundColumn DataField="ID" HeaderText="编号" Visible="False">
                                <HeaderStyle Width="30px" />
                            </asp:BoundColumn>
                            <asp:BoundColumn DataField="DossierName" HeaderText="卷宗名称" />
                            <asp:BoundColumn DataField="StateTypeValue" HeaderText="状态" SortExpression="StateTypeValue" />
                            <asp:BoundColumn DataField="Remark" HeaderText="备注" SortExpression="Remark" />
                            <asp:TemplateColumn HeaderText="编辑">
                                <ItemStyle Wrap="False" HorizontalAlign="Center" Width="80px" />
                                <ItemTemplate>
                                        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/App_Themes/Default/images/edit.gif"
                                        AlternateText="编辑" OnClientClick='<%# String.Format("BRC.Dialog.ControlBtn(\"{0}\",\"../Dialogs/DossierEdit.aspx?Mode=Edit&amp;Id={1}\",600,200);return false;",btnRefresh.ClientID,Eval("id"))%>' />
                                    &nbsp;
                                    <asp:ImageButton ID="btn_Delete_Confirm" runat="server" ImageUrl="~/App_Themes/Default/images/del.gif"
                                        AlternateText="删除" CommandName="Delete" CommandArgument='<%# Bind("Id") %>' OnClientClick="return confirm('确认要删除此行信息吗?')" />
                                    <asp:Button runat="server" ID="btn_Delete" CommandName="Delete" CssClass="Invisible" />
                                </ItemTemplate>
                            </asp:TemplateColumn>

==========================================================================================================================

推荐一篇文章,你也可以看看:http://www.cnblogs.com/Godblessyou/archive/2011/03/23/1992219.html

上一篇:MySQL自动测试框架介绍


下一篇:隐马尔科夫模型HMM(二)前向后向算法评估观察序列概率