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

时间:2016-07-07 16:13:29

标签: c# webforms code-behind asplinkbutton

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

再次感谢你。

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:

<%@ 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>

2 个答案:

答案 0 :(得分:0)

您的链接按钮位于formview项目模板中,如下所示,因此您将无法获得单独的控制事件(链接按钮的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 
    }
}

答案 1 :(得分:0)

您的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