如何在ASP.Net中使用commandName以编程方式在FormView中执行链接按钮?

时间:2013-08-27 18:53:52

标签: c# asp.net formview linkbutton

我在linkbutton控件的InsertItemTemplate内有以下FormView

<InsertItemTemplate>
    <asp:Table runat="server" id="tabInst">
        <asp:TableRow>
            <asp:TableCell CssClass="PropertyLabel">Instrument ID:</asp:TableCell>
            <asp:TableCell CssClass="PropertyValue">
                <asp:TextBox id="tbID" runat="server" Text='<%# Eval("InstID")%>' MaxLength="20"/>
                <asp:RequiredFieldValidator ID="reqValID" runat="server" ControlToValidate="tbID" CssClass="Validator" Display="Dynamic" ErrorMessage="Please enter an Instrument ID">*</asp:RequiredFieldValidator>
                <asp:CustomValidator ID="compValID" runat="server" ControlToValidate="tbID" CssClass="Validator" Display="Dynamic" OnServerValidate="compValID_ServerValidate" ErrorMessage="That InstrumentID already exists">*</asp:CustomValidator>
            </asp:TableCell><asp:TableCell CssClass="PropertyLabel">Instrument Name:</asp:TableCell><asp:TableCell CssClass="PropertyValue">
                <asp:TextBox id="tbName" runat="server" Text='<%# Eval("Name")%>' />
                <asp:RequiredFieldValidator ID="reqValName" runat="server" ControlToValidate="tbName" CssClass="Validator" Display="Dynamic" ErrorMessage="Please enter an Instrument Name">*</asp:RequiredFieldValidator>
            </asp:TableCell></asp:TableRow><asp:TableRow>
            <asp:TableCell CssClass="PropertyLabel">Exchange Symbol:</asp:TableCell><asp:TableCell CssClass="PropertyValue">
                <asp:TextBox ID="tbExchSym" runat="server" Text=<%# Eval("ExchSymbol") %> MaxLength="20"></asp:TextBox>
            </asp:TableCell><asp:TableCell CssClass="PropertyLabel">Instrument Type:</asp:TableCell><asp:TableCell CssClass="PropertyValue">
                <asp:DropDownList ID="ddInstType" runat="server" DataSourceID="edsInstType" DataTextField="Name" DataValueField="Code" SelectedValue=<%# Bind("InstType") %>></asp:DropDownList>
            </asp:TableCell></asp:TableRow><asp:TableRow>
            <asp:TableCell CssClass="PropertyLabel">Currency:</asp:TableCell><asp:TableCell CssClass="PropertyValue">
                <asp:DropDownList ID="ddCurr" runat="server" DataSourceID="edsCurr" DataTextField="Name" DataValueField="Code" OnInit="ddCurr_Init" SelectedValue=<%# Bind("CurrCode") %>></asp:DropDownList>
            </asp:TableCell><asp:TableCell CssClass="PropertyLabel">Exchange:</asp:TableCell><asp:TableCell CssClass="PropertyValue">
                <asp:DropDownList ID="ddExch" runat="server" DataSourceID="edsExch" DataTextField="Name" DataValueField="Code" OnInit="ddExch_Init" SelectedValue=<%# Bind("ExchCode") %>></asp:DropDownList>
            </asp:TableCell></asp:TableRow><asp:TableRow>
            <asp:TableCell CssClass="PropertyLabel">Price Divisor:</asp:TableCell><asp:TableCell CssClass="PropertyValue">
                <asp:TextBox id="tbPriceDiv" runat="server" Text='<%# Eval("PriceDiv")%>' />
                <asp:RequiredFieldValidator ID="reqValPriceDiv" runat="server" ControlToValidate="tbPriceDiv" CssClass="Validator" Display="Dynamic" ErrorMessage="Please enter a Price Divisor">*</asp:RequiredFieldValidator>
                <asp:CompareValidator ID="compValPriceDiv" runat="server" ControlToValidate="tbPriceDiv" CssClass="Validator" Display="Dynamic" ErrorMessage="Price Divisor must be greater than 0" Operator="GreaterThan" ValueToCompare="0" Type="Integer">*</asp:CompareValidator>
            </asp:TableCell><asp:TableCell CssClass="PropertyLabel">Tick Size:</asp:TableCell><asp:TableCell CssClass="PropertyValue">
                <asp:TextBox id="tbTickSize" runat="server" Text='<%# Eval("TickSize")%>' />
                <asp:RequiredFieldValidator ID="reqValTickSize" runat="server" ControlToValidate="tbTickSize" CssClass="Validator" Display="Dynamic" ErrorMessage="Please enter a Tick Size">*</asp:RequiredFieldValidator>
                <asp:CompareValidator ID="compValTickSize" runat="server" ControlToValidate="tbTickSize" CssClass="Validator" Display="Dynamic" ErrorMessage="Tick Size must be greater than 0" Operator="GreaterThan" ValueToCompare="0" Type="Double">*</asp:CompareValidator>
            </asp:TableCell></asp:TableRow><asp:TableRow>
            <asp:TableCell CssClass="PropertyLabel">Settle Day Type:</asp:TableCell><asp:TableCell CssClass="PropertyValue">
                <asp:RadioButtonList ID="rblSettType" runat="server" RepeatDirection="Horizontal" SelectedValue=<%# Bind("InstType") %>>
                    <asp:ListItem Selected="True" Text="Business" Value="B" />
                    <asp:ListItem Text="Calendar" Value="C" />
                </asp:RadioButtonList>
            </asp:TableCell><asp:TableCell CssClass="PropertyLabel">Settle Days:</asp:TableCell><asp:TableCell CssClass="PropertyValue">
                <asp:TextBox ID="tbSettDays" runat="server" Text=<%# Eval("SettleDays") %>></asp:TextBox>
                <asp:RequiredFieldValidator ID="reqValSettDays" runat="server" ControlToValidate="tbSettDays" CssClass="Validator" Display="Dynamic" ErrorMessage="Please enter a Settle Days">*</asp:RequiredFieldValidator>
                <asp:CompareValidator ID="compValSettDays" runat="server" ControlToValidate="tbSettDays" CssClass="Validator" Display="Dynamic" ErrorMessage="Settle Days must be greater than or equal to 0" Operator="GreaterThanEqual" ValueToCompare="0" Type="Double">*</asp:CompareValidator>
            </asp:TableCell></asp:TableRow></asp:Table><asp:EntityDataSource ID="edsCurr" runat="server" ConnectionString="name=CQPosManEntities" DefaultContainerName="CQPosManEntities" EnableFlattening="False" EntitySetName="Currencies">
    </asp:EntityDataSource>
    <asp:EntityDataSource ID="edsExch" runat="server" ConnectionString="name=CQPosManEntities" DefaultContainerName="CQPosManEntities" EnableFlattening="False" EntitySetName="Exchanges">
    </asp:EntityDataSource>
    <asp:EntityDataSource ID="edsInstType" runat="server" ConnectionString="name=CQPosManEntities" DefaultContainerName="CQPosManEntities" EnableFlattening="False" EntitySetName="InstTypes">
    </asp:EntityDataSource>
    <br />
    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" Visible="false" OnClick="InsertButton_Click"/>
    &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" Visible="false" />
    <asp:ValidationSummary ID="valSum1" runat="server" CssClass="Validator" HeaderText="Please correct the following errors:"  />
</InsertItemTemplate>

调用OnClick事件的代码是:

    public bool Insert()
    {
        LinkButton lbInsert = (LinkButton) fvInst.FindControl("InsertButton");
        InsertButton_Click(lbInsert, EventArgs.Empty);
    }

我已经确认我有正确的按钮并且输入了InsertButton_Click函数。但是,不会执行验证,也不会触发在物理单击按钮时发生的ItemInserting事件。

感谢。

1 个答案:

答案 0 :(得分:0)

您可以将代码移动到单独的方法中,并以编程方式调用该方法。 linkbutton的onclick方法也可以调用该方法。