向aspx页面添加警告消息

时间:2014-03-31 20:46:57

标签: c# javascript jquery asp.net gridview

我有一个asp.net应用程序,当我点击Gridview中的按钮时,我必须添加警告javascript消息:

<asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="False">
        <Columns>
        <asp:TemplateField  >
                <ItemTemplate>
                    <asp:HiddenField ID="Ref"  runat="server" value='<%#Eval("Ref")%>'   ></asp:HiddenField>

                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField >
                <ItemTemplate>
                    <asp:Button ID="Savebtn" runat="server" Text="update"  OnClick="gv_RowEditing"/>
                </ItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText="Note">
                <ItemTemplate>
                    <asp:TextBox ID="note"  runat="server" Text='<%#Eval("Note")%>'  onfocus="this.style.color='red'" />
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="max">
                <ItemTemplate>
                    <asp:TextBox ID="maxtxt" runat="server" Text='<%#Eval("prix max")%>' onfocus="this.style.color='red'"/>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="min"  >
                <ItemTemplate>
                    <asp:TextBox ID="mintxt" runat="server"  Text='<%#Eval("prix min")%>' onfocus="this.style.color='red'"/>
                </ItemTemplate>
            </asp:TemplateField>
  <asp:CommandField ShowSelectButton="True" />


        </Columns>
        <EditRowStyle BorderColor="Yellow" Font-Underline="True" ForeColor="#FF3300" />
    </asp:GridView>

当我点击按钮savebtn时,我需要添加提醒,如果maxtxt值小于mintxt值,则会显示警告消息。

  • 我该怎么做?
  • 向aspx页面添加警报消息的不同方法是什么?

4 个答案:

答案 0 :(得分:1)

使用OnClientClick事件调用Javascript方法来验证字段。

<asp:Button ID="Savebtn" runat="server" Text="update"  OnClick="gv_RowEditing"
       OnClientClick="if ( ! ValidateFields()) return false;"/>

你的javascript方法:(不是100%确定语法,但你明白了)

function ValidateFields() {

    var maxtxt = document.getElementById("<%=maxtxt.ClientID%>").Value;
    var mintxt = document.getElementById("<%=mintxt.ClientID%>").Value; 

     if ( maxtxt < mintxt ) {
        alert("This is bad");
        return false;
     }

     return true;


}

答案 1 :(得分:1)

使用Button的OnClientClick函数指定在回发发生之前运行的一些JavaScript。

<asp:Button ID="Button1" runat="server" Text="Delete" OnClientClick='MyFunction();' OnClick="Button1_Click" />


function MyFunction()
{
var maxtxt = document.getElementById("<%=maxtxt.ClientID%>").Value;
var mintxt = document.getElementById("<%=mintxt.ClientID%>").Value; 

if ( maxtxt < mintxt ) {
    alert("You did something wrong!");
   }
}

通常,您只想在到达服务器端之前确认客户端上的某些操作

<asp:Button ID="Button1" runat="server" Text="Delete" OnClientClick='return confirm("Are you sure you want to delete this item?");' OnClick="Button1_Click" />

你可以将两者结合起来......

<asp:Button ID="Button1" runat="server" Text="Delete" OnClientClick='return MyFunction();' OnClick="Button1_Click" />

function MyFunction()
{
var maxtxt = document.getElementById("<%=maxtxt.ClientID%>").Value;
var mintxt = document.getElementById("<%=mintxt.ClientID%>").Value; 

 if ( maxtxt < mintxt ) {
    return confirm("Are you sure you want to do that");
 }

 return true;
}

答案 2 :(得分:0)

 <asp:Button ID="Savebtn" runat="server" Text="update"  OnClick="gv_RowEditing"/>

 function gv_RowEditing(){
     if(parseInt($('#maxTxt').val()) < parseInt($('#minTxt').val())){
        alert("you message");
     }
 }

$("#Savebtn").click(function() {
   if(parseInt($('#maxTxt').val()) < parseInt($('#minTxt').val())){
        alert("you message");
     }
});

这是使用jquery。

答案 3 :(得分:0)

按如下方式定义gridview:

<asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="False">
    <Columns>
    <asp:TemplateField  >
            <ItemTemplate>
                <asp:HiddenField ID="Ref"  runat="server" value='<%#Eval("Ref")%>'   ></asp:HiddenField>

            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField >
            <ItemTemplate>
                <asp:Button ID="Savebtn" runat="server" Text="update"  OnClick="gv_RowEditing"/>
            </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="Note">
            <ItemTemplate>
                <asp:TextBox ID="note"  runat="server" Text='<%#Eval("Note")%>'  onfocus="this.style.color='red'" />
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="max">
            <ItemTemplate>
                <asp:TextBox ID="maxtxt" maxtxt="1" runat="server" Text='<%#Eval("prix max")%>' onfocus="this.style.color='red'"/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="min"  >
            <ItemTemplate>
                <asp:TextBox ID="mintxt" mintxt="1" runat="server"  Text='<%#Eval("prix min")%>' onfocus="this.style.color='red'"/>
            </ItemTemplate>
        </asp:TemplateField>

         <asp:TemplateField>
             <ItemTemplate>
                   <asp:Button CommandName="Update" Text="Update" OnClientClick="return Validate();"  />
             </ItemTemplate>
         </asp:TemplateField>

    </Columns>
    <EditRowStyle BorderColor="Yellow" Font-Underline="True" ForeColor="#FF3300" />
</asp:GridView>

然后包含以下js(使用jquery):

function Validate(){
   var max = $("input[maxtxt]").val();
   var min = $("input[mintxt]").val();
   if (maxtxt < mintxt){
       alert("Something wrong!!");
       return false;
   }
   else{
       return true;
   }
}