asp .net图像按钮验证

时间:2015-10-17 03:42:11

标签: asp.net validation imagebutton

我在表单中有一些asp .net服务器控件,我想在不使用java脚本按下图像按钮的情况下验证一个asp.net 文本框空白,由于此图像按钮不是数据发布按钮,因此相关的错误标签根本不显示任何错误。

代码

protected void ImageButtonAddItem_Click(object sender, ImageClickEventArgs e)
        {
            if (!TextBoxItemCode.Text.Trim().Equals(string.Empty))
            {
                poDetails.Add(
                               new PurchaseOrderDetail
                                   {
                                       ItemDetail = new ItemService().GetItemByID(Convert.ToInt32(TextBoxItemCode.Text.Trim())),
                                       POQty = Convert.ToInt32(TextBoxQty.Text),
                                       ItemUOM = DropDownListUOM.SelectedItem.Text,
                                       ActiveStatus = true
                                   }
                                       );
            }
            else
            {
                labelerror.Text = "Please select an item to Add .";

            }

            GridViewPurchaseOrder.DataSource = poDetails;
            GridViewPurchaseOrder.DataBind();
        }

labelerror.Text ="请选择要添加的项目。&#34 ;;部分没有得到更新,即使它被断点击中

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Session["SessionID"].ToString().Equals(Session.SessionID.ToString()))
            {
                Response.Redirect("~/User/Login.aspx");
            }
            if (!new UserPermitionLevelsServices().GetPermissionByID(
            Convert.ToInt16(Session["LoginGroupID"].ToString()),
            Convert.ToInt16(Session["LoginUserID"].ToString())
            , new FormServices().GetFormByName("PurchaseOrder"), 0))
            {
                labelerror.Text = "No Authority To Use This Resource";
                LockControls(false);
            }
            else
            {
                labelerror.Text = "";
                LockControls(true);
            }
            supplierID = Convert.ToInt16(Request.QueryString["supplierID"]);
            DropDownLocationList.Items.Add("--Select--");
            if (!IsPostBack)
            {
                MultiViewMain.ActiveViewIndex = 0;
                MultiViewItem.ActiveViewIndex = 0;
                if (supplierID != 0)
                {
                    GetSupplierByID(supplierID);
                }
                PopulateUOMList();
                PopulateLocations();
            }
        } 

html



<%@ Page Title="" Language="C#" MasterPageFile="~/Classic.Master" AutoEventWireup="true"
    CodeBehind="PurchaseOrder.aspx.cs" Inherits="NewClient.OrderProcessing.UI.Transaction.PurchaseOrder" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="body" runat="server">
   <%-- <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> --%>
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePartialRendering="true" CombineScripts="false">
   </asp:ToolkitScriptManager>
    <div style="text-align: center">
       <asp:Label ID="labelerror" runat="server" Text="Error" CssClass="errorLabel" 
            EnableViewState="False"></asp:Label>
    </div>
    <br />
    <div style="width: 750px; height: 524px">
        <asp:UpdatePanel runat="server" ID="upnlSerachSupplier">
            <ContentTemplate>
                <asp:MultiView ID="MultiViewMain" runat="server">
                    <asp:View ID="View1" runat="server">
                        <div style="z-index: 100; left: 1px; width: 700px; position: relative; top: 1px;
                            height: 230px">
                            <asp:Label ID="Label1" runat="server" Style="z-index: 103; left: 10px; position: absolute;
                                top: 20px" Width="92px">Order No</asp:Label>
                            <asp:TextBox ID="TextBoxOrderNo" runat="server" Width="100px" Style="z-index: 100;
                                left: 80px; position: absolute; top: 20px" >0001</asp:TextBox>
                            <asp:Label ID="Label2" runat="server" Style="z-index: 103; left: 200px; position: absolute;
                                top: 20px" Width="92px">Order Ref. No</asp:Label>
                            <asp:TextBox ID="TextBoxOrderRefNo" runat="server" Width="100px" Style="z-index: 100;
                                left: 300px; position: absolute; top: 20px" />
                            <asp:RequiredFieldValidator ID="RV1" runat="server" ErrorMessage="*" Style="z-index: 100;
                                left: 405px; position: absolute; top: 20px; width: 10px" ForeColor="Red" ControlToValidate="TextBoxOrderRefNo"
                                ValidationGroup="save" Font-Bold="True" Font-Size="Larger" />
                            <asp:Label ID="Label3" runat="server" Style="z-index: 103; left: 425px; position: absolute;
                                top: 20px" Width="92px">Location</asp:Label>
                            <asp:DropDownList ID="DropDownLocationList" runat="server" Style="z-index: 100; left: 485px;
                                position: absolute; top: 20px" Width="109px" />
                            <asp:RequiredFieldValidator ID="RV2" runat="server" ErrorMessage="Select Location"
                                InitialValue="--Select--" Style="z-index: 100; left: 600px; position: absolute;
                                top: 20px; width: 125px" ForeColor="Red" ControlToValidate="DropDownLocationList"
                                ValidationGroup="save" />
                            <asp:Label ID="Label6" runat="server" Style="z-index: 103; left: 10px; position: absolute;
                                top: 60px; width: 50px;">Supplier Code</asp:Label>
                            <asp:TextBox ID="TextBoxSupCode" runat="server" Width="100px" Style="z-index: 100;
                                left: 80px; position: absolute; top: 60px" />
                            <asp:Label ID="Label4" runat="server" Style="z-index: 103; left: 200px; position: absolute;
                                top: 60px; width: 95px;">Supplier Name</asp:Label>
                            <asp:TextBox ID="TextBoxSupplierName" runat="server" Style="z-index: 100; left: 300px;
                                position: absolute; top: 60px; width: 300px;" />
                            <asp:ImageButton ID="ImageFindSupplier" runat="server" Style="z-index: 103; left: 625px;
                                position: absolute; top: 60px; width: 25px; height: 25px;" ImageUrl="~/Images/Find.gif"
                                CommandName="MainSearch" OnCommand="ImageFindSupplier_Command" />
                           
                            
                            <asp:Label ID="Label9" runat="server" Style="z-index: 103; left: 10px; position: absolute;
                                top: 100px; width: 50px;">Address</asp:Label>
                            <asp:TextBox ID="TextBoxAddress" runat="server" Style="z-index: 100; left: 80px;
                                position: absolute; top: 100px; width: 550px;" />

                            <asp:Label ID="Label8" runat="server" Style="z-index: 103; left: 10px; position: absolute;
                                top: 140px; width: 50px;">Invoice Date</asp:Label>
                            <asp:TextBox ID="TextBoxInvoiceDate" runat="server" Style="z-index: 100; left: 80px;
                                position: absolute; top: 140px; width: 100px; right: 520px;" />
                            <asp:CalendarExtender ID="TextBoxInvoiceDate_CalendarExtender" runat="server" 
                                Enabled="True" TargetControlID="TextBoxInvoiceDate">
                            </asp:CalendarExtender>

                             <asp:Label ID="Label18" runat="server" Style="z-index: 103; left: 200px; position: absolute;
                                top: 140px; width: 50px;">Delivery Date</asp:Label>
                                   <asp:TextBox ID="TextBoxDeliveryDate" runat="server" Style="z-index: 100; left: 300px;
                                position: absolute; top: 140px; width: 100px; right: 520px;" />
                            
                            <asp:CalendarExtender ID="TextBoxDeliveryDate_CalendarExtender" runat="server" 
                                Enabled="True" TargetControlID="TextBoxDeliveryDate">
                            </asp:CalendarExtender>
                            
                            <asp:Label ID="Label10" runat="server" Style="z-index: 103; left: 10px; position: absolute;
                                top: 180px; width: 50px;">Comment</asp:Label>
                            <asp:TextBox ID="TextBoxComment" runat="server" Style="z-index: 100; left: 80px;
                                position: absolute; top: 180px; width: 550px;" TextMode="MultiLine" Rows="2" />
                            <%-- <asp:Button ID="ButtonSave" runat="server" Text="Save" Style="z-index: 100; left: 50px;
position: relative; top: 250px" Width="50px" ValidationGroup="save"/>--%>
                            <%-- <asp:Button ID="ButtonSave" runat="server" Text="Save" Style="z-index: 100; left: 50px;
position: absolute; top: 250px" Width="50px" ValidationGroup="save"/>--%>
                        </div>
                    </asp:View>
                    <asp:View ID="View2" runat="server">
                        <div style="border-style: solid; border-width: 1px; border-color: inherit; z-index: 100;
                            left: 1px; width: 725px; position: relative; top: 1px; height: 250px;">                                                      
                                <asp:Label ID="Label7" runat="server" Style="z-index: 103; left: 10px; position: absolute;
                                    top: 20px; width: 100px; right: 615px;">Supplier Code</asp:Label>
                                <asp:TextBox ID="TextBoxSupplierCode" runat="server" Width="100px" Style="z-index: 100;
                                    left: 125px; position: absolute; top: 20px" />
                                <asp:Label ID="Label5" runat="server" Style="z-index: 103; left: 315px; position: absolute;
                                    top: 20px; width: 100px;">Supplier Name</asp:Label>
                                <asp:TextBox ID="TextBoxSupp_Name" runat="server" Style="z-index: 100; left: 420px;
                                    position: absolute; top: 20px; width: 250px;" />
                                <asp:ImageButton ID="ImageButtonSupSearch" runat="server" Style="z-index: 103; left: 675px;
                                    position: absolute; top: 20px; width: 25px;" ImageUrl="~/Images/Find.gif" OnClick="ImageButtonSupSearch_Click"
                                    CommandName="SubSearch" TabIndex="1" />
                                <asp:ImageButton ID="ImageButtonClose" runat="server" Style="z-index: 103; left: 700px;
                                    position: absolute; top: 1px; height: 15px;" ImageUrl="~/Images/close.gif" CommandName="closeSearch"
                                    OnCommand="ImageButtonClose_Command" />
                            <asp:UpdatePanel ID="updatepnlSupplier" runat="server" UpdateMode="Conditional">
                            <ContentTemplate>
                                <asp:GridView ID="GridViewSupplier" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                                    SkinID="GridView" Style="z-index: 111; left: 175px; position: absolute; top: 50px"
                                    Width="317px" ShowFooter="True" PageSize="5" DataKeyNames="supplierID" OnPageIndexChanging="GridViewSupplier_PageIndexChanging"
                                    OnSelectedIndexChanged="GridViewSupplier_SelectedIndexChanged">
                                    <columns>
                                    <%--<asp:HyperLinkField DataNavigateUrlFields="supplierID" runat="server" DataNavigateUrlFormatString="PurchaseOrder.aspx?supplierID={0}"
Text="Select" />--%>
                                    <asp:CommandField ButtonType="Link" ShowSelectButton="True">
                                        <ItemStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                                    </asp:CommandField>
                                    <asp:TemplateField HeaderText="Supplier ID" Visible="false">
                                        <ItemTemplate>
                                            <%# Eval("supplierID")%>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Supplier Name">
                                        <ItemTemplate>
                                            <%# Eval("supplierName")%>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Contact No">
                                        <ItemTemplate>
                                            <%# Eval("supplierContactNo")%>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Email">
                                        <ItemTemplate>
                                            <%# Eval("supplierEmail")%>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="">
                                        <ItemTemplate>
                                            <asp:CheckBox runat="server" ID="chbRL" Text="" Checked='<%# Eval("active")%> ' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </columns>
                                </asp:GridView>                                
                                </ContentTemplate>
                                </asp:UpdatePanel>
                        </div>
                    </asp:View>
                </asp:MultiView>
                <asp:MultiView ID="MultiViewItem" runat="server">
                    <asp:View ID="View3" runat="server">
                        <div style="z-index: 100; left: 1px; width: 725px; position: relative; top: 5px;
                            height: 250px;">
                            <asp:Label ID="Label11" runat="server" Style="z-index: 103; left: 10px; position: absolute;
                                top: 20px; width: 100px;">Item Code</asp:Label>
                            <asp:TextBox ID="TextBoxItemCode" runat="server" Width="100px" Style="z-index: 100;
                                left: 125px; position: absolute; top: 20px" />
                            
                            <asp:Label ID="Label12" runat="server" Style="z-index: 103; left: 315px; position: absolute;
                                top: 20px; width: 100px;">Item Name</asp:Label>
                            <asp:TextBox ID="TextBoxItemName" runat="server" Style="z-index: 100; left: 420px;
                                position: absolute; top: 20px; width: 250px;" />
                            <asp:ImageButton ID="ImageButtonFindItem" runat="server" Style="z-index: 103; left: 675px;
                                position: absolute; top: 20px; width: 25px;" ImageUrl="~/Images/Find.gif" CommandName="SubSearchItem"
                                TabIndex="1" OnCommand="ImageButtonFindItem_Command" />
                            <asp:Label ID="Label15" runat="server" Style="z-index: 103; left: 10px; position: absolute;
                                top: 50px; width: 100px;">UOM</asp:Label>
                           <%-- <asp:TextBox ID="TextBoxUOM" runat="server" Width="100px" Style="z-index: 100; left: 125px;
                                position: absolute; top: 50px" />--%>
                                 <asp:DropDownList ID="DropDownListUOM" runat="server" Style="z-index: 100; left: 125px;
                                position: absolute; top: 50px" Width="109px" />
                            <asp:Label ID="Label16" runat="server" Style="z-index: 103; left: 235px; position: absolute;
                                top: 50px; width: 50px;">Quantity</asp:Label>
                            <asp:TextBox ID="TextBoxQty" runat="server" Width="100px" Style="z-index: 100; left: 300px;
                                position: absolute; top: 50px" />
                            <asp:Label ID="Label17" runat="server" Style="z-index: 103; left: 420px; position: absolute;
                                top: 50px; width: 50px;">Price</asp:Label>
                            <asp:TextBox ID="TextBoxPrice" runat="server" Width="100px" Style="z-index: 100;
                                left: 475px; position: absolute; top: 50px" />
                            <asp:ImageButton ID="ImageButtonAddItem" runat="server" Style="z-index: 103; left: 675px;
                                position: absolute; top: 50px; width: 25px; height: 25px" ImageUrl="~/Images/smallnew.gif"
                                CommandName="AddItem" OnClick="ImageButtonAddItem_Click"  CausesValidation="true"/>
                            <asp:GridView ID="GridViewPurchaseOrder" runat="server" AutoGenerateColumns="False"
                                SkinID="GridView" Style="z-index: 111; left: 175px; position: absolute; top: 80px"
                                Width="317px" ShowFooter="True" PageSize="2" 
                                onrowdeleting="GridViewPurchaseOrder_RowDeleting">
                                <Columns>
                                    <asp:CommandField ShowDeleteButton="True">
                                        <ItemStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                                    </asp:CommandField>
                                    <asp:CommandField ButtonType="Link" ShowSelectButton="True">
                                        <ItemStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                                    </asp:CommandField>
                                    <asp:TemplateField HeaderText="Item ID" Visible="false">
                                        <ItemTemplate>
                                            <%# Eval("ItemDetail.itemID")%>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Item Name">
                                        <ItemTemplate>
                                            <%# Eval("ItemDetail.ItemDesc")%>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Item Sales Price">
                                        <ItemTemplate>
                                            <%# DataBinder.Eval(Container.DataItem, "ItemDetail.itemSalesPrice", "{0,0:0.00}")%>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="UOM">
                                        <ItemTemplate>
                                            <%# Eval("ItemUOM")%>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Quantity">
                                        <ItemTemplate>
                                            <%# Eval("POQty")%>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    
                                    <%-- <asp:TemplateField HeaderText="">
                                        <ItemTemplate>
                                            <asp:CheckBox runat="server" ID="chbRL" Text="" Checked='<%# Eval("active")%> ' />
                                        </ItemTemplate>
                                    </asp:TemplateField>--%>
                                </Columns>
                            </asp:GridView>
                            <asp:Button ID="ButtonSave" runat="server" Text="Save" Style="z-index: 100; left: 170px;
                position: absolute; top: 275px; height: 26px;" Width="50px" ValidationGroup="save"
                OnClick="ButtonSave_Click" />
            <asp:Button ID="ButtonUpdate" runat="server" Text="Update" Style="z-index: 100; left: 230px;
                position: absolute; top: 275px" Width="60px" ValidationGroup="save" OnClick="ButtonUpdate_Click" />
            <asp:Button ID="ButtonRest" runat="server" Text="Reset" Style="z-index: 100; left: 300px;
                position: absolute; top: 275px; height: 26px;" Width="50px" OnClick="ButtonRest_Click" />
            <asp:Button ID="ButtonCancel" runat="server" Text="Cancel" Style="z-index: 100; left: 360px;
                position: absolute; top: 275px" Width="50px" OnClick="ButtonCancel_Click" />
            <asp:Label ID="lblMsg" runat="server" Style="z-index: 103; left: 180px; position: absolute;
                top: 250px; width: 325px; right: 431px;" Font-Bold="True" ForeColor="Blue"></asp:Label>
                        </div>
                    </asp:View>
                    <asp:View ID="View4" runat="server">
                        <div style="border-style: solid; border-width: 1px; border-color: inherit; z-index: 100;
                            left: 1px; width: 725px; position: relative; top: 5px; height: 250px;">
                            <asp:Label ID="Label13" runat="server" Style="z-index: 103; left: 10px; position: absolute;
                                top: 20px; width: 100px;">Item Code</asp:Label>
                            <asp:TextBox ID="TextBoxSearchItemCode" runat="server" Width="100px" Style="z-index: 100;
                                left: 125px; position: absolute; top: 20px" />
                            <asp:Label ID="Label14" runat="server" Style="z-index: 103; left: 315px; position: absolute;
                                top: 20px; width: 100px;">Item Name</asp:Label>
                            <asp:TextBox ID="TextBoxSearchItemName" runat="server" Style="z-index: 100; left: 420px;
                                position: absolute; top: 20px; width: 250px;" />
                            <asp:ImageButton ID="ImageButtonIt_Search" runat="server" Style="z-index: 103; left: 675px;
                                position: absolute; top: 20px; width: 25px;" ImageUrl="~/Images/Find.gif" CommandName="SubSearchItem"
                                TabIndex="1" OnClick="ImageButtonIt_Search_Click" />
                            <asp:ImageButton ID="ImageButtonItemClose" runat="server" Style="z-index: 103; left: 700px;
                                position: absolute; top: 1px; height: 15px;" ImageUrl="~/Images/close.gif" CommandName="closeItSearch"
                                OnCommand="ImageButtonItemClose_Command" />
                            <asp:GridView ID="GridViewItem" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                                SkinID="GridView" Style="z-index: 111; left: 175px; position: absolute; top: 50px"
                                Width="317px" ShowFooter="True" PageSize="5" DataKeyNames="itemID" OnPageIndexChanging="GridViewItem_PageIndexChanging"
                                OnSelectedIndexChanged="GridViewItem_SelectedIndexChanged">
                                <Columns>
                                    <asp:CommandField ButtonType="Link" ShowSelectButton="True">
                                        <ItemStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                                    </asp:CommandField>
                                    <asp:TemplateField HeaderText="Item ID" Visible="false">
                                        <ItemTemplate>
                                            <%# Eval("itemID")%>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Item Name">
                                        <ItemTemplate>
                                            <%# Eval("ItemDesc")%>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Item Category Name">
                                        <ItemTemplate>
                                            <%# Eval("ItemCategory.itemCatName")%>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Item Sales Price">
                                        <ItemTemplate>
                                            <%# DataBinder.Eval(Container.DataItem, "itemSalesPrice", "{0,0:0.00}")%>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Reorder Level">
                                        <ItemTemplate>
                                            <%# Eval("itemReorder")%>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Item Cost Price">
                                        <ItemTemplate>
                                            <%# DataBinder.Eval(Container.DataItem, "itemCostPrice", "{0,0:0.00}")%>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="">
                                        <ItemTemplate>
                                            <asp:CheckBox runat="server" ID="chbRL" Text="" Checked='<%# Eval("active")%> ' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>
                        </div>
                    </asp:View>
                </asp:MultiView>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
</asp:Content>
&#13;
&#13;
&#13;

需要一些指导

2 个答案:

答案 0 :(得分:0)

我想你可以做这样的事情......添加断点并检查它是否进入其他部分

protected void ImageButtonAddItem_Click(object sender, ImageClickEventArgs e)
    {
        if (TextBoxItemCode.Text == "")
        {
          labelerror.Text = "Please select an item to Add .";
        }
        else
        {
          poDetails.Add(
                           new PurchaseOrderDetail
                               {
                                   ItemDetail = new ItemService().GetItemByID(Convert.ToInt32(TextBoxItemCode.Text.Trim())),
                                   POQty = Convert.ToInt32(TextBoxQty.Text),
                                   ItemUOM = DropDownListUOM.SelectedItem.Text,
                                   ActiveStatus = true
                               }
                         );

        GridViewPurchaseOrder.DataSource = poDetails;
        GridViewPurchaseOrder.DataBind();
        }
    }

你的页面加载其他部分刷新你的标签错误,这部分..

          else
          {
            labelerror.Text = "";
            LockControls(true);
          }

最好使用不同的标签,或在

中写下你的代码
protected void Page_Load(object sender, EventArgs e)
    {
if(!IsPostBack)
{
//all ur page load code
}
}

答案 1 :(得分:0)

因为您的标签位于更新面板“upnlSerachSupplier”之外。您必须在标签周围使用更新面板,如下所示:

<asp:UpdatePanel runat="server" ID="upnl1" UpdateMode="Always">
     <ContentTemplate>
<asp:Label ID="labelerror" runat="server" Text="Error" CssClass="errorLabel" 
            EnableViewState="False"></asp:Label>
     <ContentTemplate>
</asp:UpdatePanel>

看看:

https://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.updatemode(v=vs.110).aspx