FormView(Insert/Update)

(ASP.NET)


The formview control defined with itemtemplate, edititemtemplate and pager settings. The formview control handling OnItemUpdating and OnModeChanging event. The datasource control is used for following actions:

1. To execute the query against the database.
2. To bind the resultset from the db to the formview control.

The OnItemUpdating event is used for validation before updating the database. If the validation fails, i am displaying the validation error and cancel the update operation.

The OnModeChange event is used to clear the validation error message when the user cancel the update operation.


Tools Used:

       1. VISUAL STUDIO EXPRESS 2013 FOR WEB
       2. SQL SERVER EXPRESS DATABASE(APP_DATA)
      

Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Fruits List</title>
  
<style type="text/css">

table.fruittbl {
    font-family: verdana,arial,sans-serif;
    font-size:11px;
    color:#333333;
    border-width: 1px;
    border-color: #999999;
    border-collapse: collapse;
}
table.fruittbl th {
    background-color:lightblue;
    border-width: 1px;
    padding: 8px;
    font-size:x-large;
    border-style: solid;
    border-color: #999999;
}
table.fruittbl td {
    background-color:lightgray;
    border-width: 1px;
    font-size:large;
    padding: 8px;
    border-style: solid;
    border-color: #999999;
}

    table.fruittbl caption {
        font-family: verdana,arial,sans-serif;
    font-size:x-large;
    color:#333333;
    border-width: 1px;
    border-color: #999999;
    border-collapse: collapse;
  
    }
</style>



</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lbl_errormsg" ForeColor="Red" runat="server"></asp:Label>
        <asp:FormView ID="frmvw_fruits" DataKeyNames="Id"  DataSourceID="sqlds_fruits" AllowPaging="true"
              runat="server" OnItemUpdating="frmvw_fruits_ItemUpdating" OnModeChanging="frmvw_fruits_ModeChanging">
           
<ItemTemplate>
               <table class="fruittbl">
                   <caption>Fruits List</caption>
                   <thead>
                       <tr>
                           <th>
                               <label>
                                   Fruit
                               </label>
                           </th>
                           <th>
                               <label>
                                   Quantity
                               </label>
                           </th>
                       </tr>
                   </thead>
                   <tbody>
                       <tr>
                           <td>
                               <label>
                                   <%# Eval("Name") %>
                               </label>
                           </td>
                           <td>
                               <label>
                                   <%# Eval("Quantity") %>
                               </label>
                           </td>
                       </tr>
                        <tr>
              <td colspan="2">
                <asp:linkbutton id="Edit"
                  text="Edit"
                  commandname="Edit"
                  runat="server"/>
              </td>
            </tr>
                   </tbody>
               </table>
            </ItemTemplate>
            <EditItemTemplate>
                 <table class="fruittbl">
                   <caption>Fruits List</caption>
                   <thead>
                       <tr>
                           <th>
                               <label>
                                   Fruit
                               </label>
                           </th>
                           <th>
                               <label>
                                   Quantity
                               </label>
                           </th>
                       </tr>
                   </thead>
                   <tbody>
                       <tr>
                           <td>
                                <asp:textbox ID="txt_name"
                  text='<%# Bind("Name") %>'
                  runat="server"/>
                          
                           </td>
                           <td>
                              <asp:textbox ID="txt_quantity"
                  text='<%# Bind("Quantity") %>'
                  runat="server"/>
                                
                           </td>
                       </tr>
                        <tr>
              <td colspan="2">
                <asp:linkbutton id="UpdateButton"
                  text="Update"
                  commandname="Update"
                  runat="server"/>
                <asp:linkbutton id="CancelButton"
                  text="Cancel"
                  commandname="Cancel"
                  runat="server"/>
              </td>
            </tr>
                   </tbody>
               </table>
            </EditItemTemplate>

            <PagerSettings   Position="Bottom"  Mode="Numeric" />
        </asp:FormView>
       
<asp:SqlDataSource ID="sqlds_fruits" 
             connectionstring="<%$ ConnectionStrings:FruitsConn%>"
            SelectCommand="SELECT Id,Name, Quantity FROM FruitsList"
             UpdateCommand="UPDATE FruitsList Set Name = @Name, Quantity = @Quantity WHERE Id = @Id"
            runat="server"></asp:SqlDataSource>

    </div>
    </form>
</body>
</html>


The sqldatasource control is defined with the selectcommand and updatecommand. The sqldatasource control is linked with the formview control using datasourceid property of the formview control.

The Eval() method is used in itemtemplate for single way binding. The bind() method is used in edititemtemplate for two way binding.

I used sqlexpress database, I attached in the project. You can download the project and run it.

Default.aspx.cs

  protected void Page_Load(object sender, EventArgs e)
        {

        }

In the ItemUpdating event i am getting the quantity value and then validating the value.

        protected void frmvw_fruits_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            try
            {
                IOrderedDictionary dicList = e.NewValues;

                foreach(DictionaryEntry item in dicList)
                {

                    //validate the quantity
                    string key = Convert.ToString(item.Key);
                   if(key.Equals("Quantity"))
                   {
                       string val = item.Value.ToString();

                       Regex regex = new Regex(@"^[0-9]+$");

                       bool isValid = regex.IsMatch(val);

                       //if it is not valid cancel the update operation
                       if (!isValid)
                       {
                           e.Cancel = true;
                             lbl_errormsg.ForeColor = Color.Red;
                           lbl_errormsg.Text = "Please type the valid quantity.";
                       }
                       else
                       {
                           lbl_errormsg.ForeColor = Color.Green;
                           lbl_errormsg.Text = "Updated Successfully.";
                       }
                   }
                  
                }
            }
            catch(Exception)
            { }
        }

        protected void frmvw_fruits_ModeChanging(object sender, FormViewModeEventArgs e)
        {
            try
            {
                if(e.CancelingEdit)
                {
                    lbl_errormsg.Text = " ";
                }
            }
            catch(Exception)
            { }
        }



Ratings


Average Rating: 0.00 by 0 users
Giri Prasad
5/4/2014
5/4/2014
Download PDF
Download Project

Comments