C#中OnClientClick出错?

时间:2012-09-27 15:05:02

标签: asp.net gridview hyperlink webforms

我希望在删除记录之前将弹出窗口填充到用户 我尝试使用 OnClientClick 来处理JavaScript / jQuery,但没有成功

错误

Type 'System.Web.UI.WebControls.HyperLinkField' does not have a public property named 'OnClientClick'.

控制

 <asp:HyperLinkField  OnClientClick="return confirm('Are you sure you would like to delete the selected landing page?')" datanavigateurlfields="id"    datanavigateurlformatstring="ViewLandingPages.aspx?id={0}&delete=yes"   HeaderText="Delete"  Text="Delete" />

编辑还尝试添加(使用jquery处理它)但没有成功

4 个答案:

答案 0 :(得分:2)

请注意,它没有OnClientClick个事件declared in its members

尝试设置NavigateUrl属性,如下所示:

NavigateUrl='<%# "javascript:return confirm('Are you sure you would like to delete the selected landing page?')" %>'

更新

尝试同样设置DataNavigateUrlFormatString

DataNavigateUrlFormatString="{0}"

答案 1 :(得分:2)

您可以使用<asp:Button/><asp:LinkButton/><asp:ImageButton/>来处理客户端和服务器端的点击事件。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
    <Columns>
       <!--- Other columns/Fields -->
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton 
                    ID="LinkButton1" 
                    runat="server" 
                    OnClientClick='return confirm("Are you sure to delete a record?")'
                    CommandName="Delete">Delete</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

答案 2 :(得分:0)

在我的项目中,我确实喜欢这个,我会在下面显示一个div来进行确认

<div id="dvDeleteCategory" title="Delete Category">
    <h5>
        Are you sure you wont to delete this category !!!</h5>
</div>

<script type="text/javascript">
    $(function () {       
        $(".lnkDelete").button();
        $("#dvDeleteCategory").dialog({
            autoOpen: false,
            width: 400,
            modal: true,
            resizable: false,
            height: 250
        });

        $(".lnkDelete").click(function (e) {
            e.preventDefault();
            var trargeturl = $(this).attr('href');
            $("#dvDeleteCategory").dialog({
                buttons: {
                    "Delete": function () {
                        window.location.href = trargeturl;
                    },
                    "Cancle": function () {
                        $(this).dialog('close');
                    }
                }
            });
            $("#dvDeleteCategory").dialog('open');
        });
    });
</script>

我认为这会对你有帮助..

答案 3 :(得分:0)

使用模板字段解决我的问题;)

<asp:TemplateField HeaderText="Delete" ShowHeader="False">
  <ItemTemplate>
     <a onclick="return confirm('Are you sure to delete a record?')" href="ViewLandingPages.aspx?delete=yes&id=<%# Eval("id") %>">Delete</a>
   </ItemTemplate>
</asp:TemplateField>