views:

89

answers:

2

I have a get/post/JSON function on an aspx page. This page adds data entered in a textbox to a table populated by javascript. When the user select the submit button. If the textbox is not empty, have a popup button telling the user the data in the textbox is not saved in the table. How do I have a confirm "ok/cancel" popup display on the post action in the Controller? I made a quick summary of what my code looks like.

...
<%  using (Html.BeginForm("AddName", "Name", FormMethod.Post, new { id = "AddNameForm" })) { %>
...
<table id="displayNameTable" width= "100%">                        
    <tr>
        <th colspan="3">Names Already Added</th>
    </tr>
    <tr>
        <td style="font-size:smaller;" class="name"></td>
    </tr>
</table>  
...
<input name="Name" id="txtInjuryName" type="text" value="<%=test.Name %>" />
...
<input type="submit" name="add" value="Add"/>
<% } %>
<form id="form1" runat="server">
           string confirmNext = "";
    if (test.Name == "")
    {
       confirmNext = "return confirm('It seems you have a name not added.\n\nAre Continue?')";
    }%>
    <input type="submit" name="getNext" value="Next" onclick="<%=confirmNext%>" />
</form>
+3  A: 
<script type="text/javascript">

    $(document).ready(function(){ $("#form1").bind("submit", function(){ 
         if(!confirm('It seems you have a name not added.\n\nAre Continue?')) 
               return false;   
         });}          
     });

</script>
Gregoire
+1 for using jQuery to separate the client side interactions from the html element. Helps maintainability.
Amir
THis is a good way to go... but how do I check to see if the textbox is empty or not???
add if($("#txtInjuryName").val()==""){...}
Gregoire
Thanks... I am new to the jquery usage.
+3  A: 

You could use the form's onsubmit event:

<form id="form1" onsubmit="return confirm('It seems you have a name not added.\n\nAre Continue?')">
    <input type="submit" name="getNext" value="Next" />
</form>

Also why runat="server" in an ASP.NET MVC application? Also you might consider specifying an action that this form is posting to and use the Html.BeginForm helper as for your first form.

Darin Dimitrov