LinkBut​​ton C# OnClick 事件未在 Formview ItemTemplate 中触发

LinkButton C# OnClick event not firing within Formview ItemTemplate

我的 "save" 链接按钮触发有问题,我不知所措。我已经包含了表单和代码隐藏的代码。我已经添加了一些测试来查看事件是否正在触发,但似乎没有。任何帮助表示赞赏。我是一名新手编码员,所以如果有明显的问题或更好的方法,请原谅。最终目标是为给定的屏幕信息更新数据库中的条目,然后重新显示更新的信息。

再次感谢您。

UPDATE: I have included the FULL-ish CODE: (removed the sensitive info)

代码隐藏:

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class matter : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      ...        
    }

    protected void fvdoc_ItemCommand(object sender, FormViewCommandEventArgs e)
    {
        if (e.CommandName == "Update")
        {
            throw new Exception("Clicked");
        }
        throw new Exception("i've been Clicked");
    }
}

页数:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="matter.aspx.cs" Inherits="matter" %>

<!DOCTYPE html>

<html>
<head>
        <title>Wasatch Client Matter Index</title>
        <link rel="shortcut icon" href="~/favicon.ico" type="image/x-icon" />
        <meta name="viewport" charset="utf-8" content="width=device-width, initial-scale=1, user-scalable=no"/>

        <!-- Latest compiled and minified CSS -->
        <link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Ubuntu+Condensed' type='text/css' />
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous" />
        <link rel="stylesheet" href="Content/themes/Site.css" />
        <!-- Latest compiled JavaScript -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"/>

    </head>
<body>
    <div class="navbar">
            <div class="row">
                <h1 class="col-lg-8 col-lg-offset-2 text-center " style="align-content:center;">Matter Index </h1>
            </div>
        </div>

    <div class="container-fluid">
    <form id="form1" runat="server">
        <div class="row">
            <div class="col-lg-10 col-lg-offset-1 form-group">
                <asp:FormView ID="fvdoc" runat="server" DataSourceID="gvdb" OnItemCommand="fvdoc_ItemCommand">
                    <ItemTemplate>
                        <h2 class="col-md-12"><asp:Label ID="tbname" runat="server" Text=<%# Bind("docid") %> /> - <asp:Label ID="lbID" runat="server" Text=<%# Bind("sName") %> /></h2>

                                <div class="left col-md-10"> 
                                    <legend>Matter Info:</legend>
                                       <div class="form-group"><asp:Label runat="server" Text="Matter" AssociatedControlID="dcname"/>
                                       <asp:TextBox ID="dcname" runat="server" CssClass="form-control" Text=<%# DataBinder.Eval(Container.DataItem,"sDocname") %> Enabled="true"/></div>
                                </div>

                                <div class="left col-md-10">
                                    <hr />
                                        <div class="form-group"><asp:Label runat="server" Text="Notes/Comments" AssociatedControlID="dcnotes" />
                                        <asp:TextBox ID="dcnotes" runat="server" Rows="3" TextMode="MultiLine" Text=<%# Bind("sdocdesc") %> Enabled="true"/></div>
                                </div>

                                <div class="left col-md-6 col-md-offset-5 txsmall"> 
                                    <asp:Label runat="server" Text="Filed: " Font-Bold="true" /><asp:Label ID="lblfiledate" runat="server" Text=<%# Bind("dtFiledate") %> CssClass="txsmall" Font-Italic="true" />
                                    <asp:Label runat="server" Text="Modified: " Font-Bold="true" /><asp:Label ID="lblmodify" runat="server" Text=<%# Bind("dtLastModified") + " - " + Bind("susermodified") %> CssClass="txsmall" Font-Italic="true"/>

                                    <asp:Label runat="server" CssClass="txsmall" id="lbltest"/>
                                </div>
                                <div class="clear-fix col-md-12">
                                   <div class="form-group"> 
                                        <asp:LinkButton runat="server" Text="Save" ID="SaveButton" CommandName="Update" CssClass="clear-fix btn btn-primary" />&nbsp;
                                        <asp:LinkButton runat="server" Text="Move" ID="MoveButton" CssClass="clear-fix btn btn-primary" CausesValidation="False" href="m.aspx" />&nbsp;
                                        <asp:LinkButton runat="server" Text="Home" ID="HomeButton" CssClass="clear-fix btn btn-primary" CausesValidation="False" href="default.aspx"/>
                                    </div>
                               </div>
                    </ItemTemplate>

                </asp:FormView>
            </div>
        </div>
        <hr class="col-lg-10 col-lg-offset-1" />

    </form>

    </div>
   ...
</body>
</html>

您的 link 按钮出现在表单视图项目模板中,如下所示,因此您不会获得单独的控件事件(link 按钮的 onclick 事件)。相反,您需要处理 FormView.ItemCommand 并像

那样进行处理
<asp:FormView ID="fvdoc" runat="server" 
 DataSourceID="gvdb" onitemcommand="itemCommandClick">

<ItemTemplate>

.......
<asp:LinkButton runat="server" Text="Save" ID="SaveButton" CommandName="Update" .........

在后面的代码中这样处理

void itemCommandClick(Object sender, FormViewCommandEventArgs e)
  {
    if (e.CommandName == "Update")
    {
        LinkButton button = e.CommandSource as LinkButton;
        //Do rest of the processing 
    }
}

您的 FormView 缺少 EditItemTemplate,像这样:

            <asp:FormView ID="fvdoc" runat="server" DataSourceID="gvdb" OnItemCommand="fvdoc_ItemCommand">
                <ItemTemplate>
                    <!--... Use readonly controls like Label etc...-->
                    <asp:LinkButton runat="server" Text="Save" ID="EditButton" CommandName="Edit" CssClass="clear-fix btn btn-primary" />
                </ItemTemplate>
                <EditItemTemplate>
                    <!--... Use editable controls like TextBox etc...-->
                    <asp:LinkButton runat="server" Text="Save" ID="SaveButton" CommandName="Update" CssClass="clear-fix btn btn-primary" />
                </EditItemTemplate>
            </asp:FormView>

对于单击编辑按钮,更改为编辑模式,然后处理保存按钮以保存新值。

有关详细信息,请参阅 MSDN docs