简体   繁体   English

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

[英]LinkButton C# OnClick event not firing within Formview ItemTemplate

I am having an issue with my "save" linkbutton firing and am at a loss. 我的“保存”链接按钮触发出现问题,茫然无措。 I have included the code for the form and code-behind. 我已经包含了表单的代​​码和代码隐藏。 I have added some testing to see if the event is firing and it doesn't appear to be so. 我添加了一些测试,以查看事件是否正在触发,但事实并非如此。 Any help is appreciated. 任何帮助表示赞赏。 I am a novice coder so excuse me if there are obvious issues or a better way to proceed. 我是新手编码员,所以如果有明显的问题或更好的处理方法,请原谅。 Ultimate goal is to update the entry in the database for the given screen info and then redisplay to updated info. 最终目标是针对给定的屏幕信息更新数据库中的条目,然后重新显示为更新的信息。

Again thank you in advance. 再次感谢您。

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

CODEBEHIND: 代码隐藏:

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>

Your link button is present inside a formview item template as can be seen below and thus you will not get the individual control event ( onclick event of linkbutton). 您的链接按钮位于formview项模板内,如下所示,因此您将不会获得单个控件事件(linkbutton的onclick事件)。 Rather you need to handle the FormView.ItemCommand and do your processing like 而是您需要处理FormView.ItemCommand并进行类似的处理

<asp:FormView ID="fvdoc" runat="server" 
 DataSourceID="gvdb" onitemcommand="itemCommandClick">

<ItemTemplate>

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

In code behind handle this like 在后面的代码中这样处理

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

Your FormView is missing the EditItemTemplate , like so: 您的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>

For the click on the Edit Button, change to Edit mode, then Handle the Save button to save the new values. 要单击“编辑”按钮,请切换到“编辑”模式,然后处理“保存”按钮以保存新值。

See the MSDN docs for more information. 有关更多信息,请参见MSDN文档

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

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