Click here to Skip to main content
16,007,932 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Programmers
i have a formView inside updatepanel .
and i have two divs (one for success insert and the other for failed insertion) their styles display = none .

and i have two javascript functions tom call two divs with id and make their styles = block

i want to call these function after user click form view insert button inside the update panel

Note: the code works correctly without using update panel

here is the aspx code
<%@ Page Language="C#" MasterPageFile="MasterPage2.master" AutoEventWireup="true" CodeFile="schools1.aspx.cs" Inherits="Adminstration_schools1" Title="Untitled Page" %>

<%@ Register src="usercontrols/Print.ascx" tagname="Print" tagprefix="uc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
  
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
  
  <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <br />
  <script type="text/javascript">
    function ok()
    {
    document.getElementById("ok").style.display = "block";
    }
    
    function error()
    {
    document.getElementById("error").style.display = "block";
    }
    
    </script>
  <div class="alert_success" id="ok" style="display:none">
						<p>
							<img src="images/icon_accept.png" alt="success" class="mid_align"/>
							 OK Inserted Sucessfully
						</p>
					</div>
					
					<div class="alert_error" id="error" style="display:none">
						<p>
							<img src="images/icon_error.png" alt="delete" class="mid_align"/>
							Error Record Not inserted
						</p>
					</div>

    <asp:FormView ID="FormView1" runat="server" DataKeyNames="schoolId" 
    DataSourceID="schoolsDataSource1" DefaultMode="Insert" 
        oniteminserting="FormView1_ItemInserting" Width="98%" dir="rtl" 
        CellPadding="10" ForeColor="#333333" Caption="Add School" 
        CellSpacing="10" BorderStyle="Dashed" 
        oniteminserted="FormView1_ItemInserted" >
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#EFF3FB" />
    <EditItemTemplate>
        schoolId:
        <asp:Label ID="schoolIdLabel1" runat="server" Text='<%# Eval("schoolId") %>' />
        <br />
        schname:
        <asp:TextBox ID="schnameTextBox" runat="server" Text='<%# Bind("schname") %>' />
        <br />
        address:
        <asp:TextBox ID="addressTextBox" runat="server" Text='<%# Bind("address") %>' />
        <br />
        tel:
        <asp:TextBox ID="telTextBox" runat="server" Text='<%# Bind("tel") %>' />
        <br />
        scdate:
        <asp:TextBox ID="scdateTextBox" runat="server" Text='<%# Bind("scdate") %>' />
        <br />
        <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
            CommandName="Update" Text="Update" />
         <asp:LinkButton ID="UpdateCancelButton" runat="server" 
            CausesValidation="False" CommandName="Cancel" Text="Cancel" />
    </EditItemTemplate>
    <InsertItemTemplate>
        Name:
        <asp:TextBox ID="schnameTextBox" runat="server" 
            Text='<%# Bind("schname") %>' />
          <br />
        <br />
        Address:
        <asp:TextBox ID="addressTextBox" runat="server" 
            Text='<%# Bind("address") %>' />
         
        <br />
        <br />
        tel:
        <asp:TextBox ID="telTextBox" runat="server" Text='<%# Bind("tel") %>' />
         <br />
        <br />
        Date:
        <asp:TextBox ID="scdateTextBox" runat="server" Text='<%# Bind("scdate") %>' 
            ondatabinding="scdateTextBox_DataBinding" ReadOnly="True" />
        <br />
        <br />
                    
        <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
            CommandName="Insert" Text="Insert" Font-Bold="True" /> 
                       
        <asp:LinkButton ID="InsertCancelButton" runat="server" 
            CausesValidation="False" CommandName="Cancel" Text="Cancel" 
            Font-Bold="True" />
    </InsertItemTemplate>
    <ItemTemplate>
        schoolId:
        <asp:Label ID="schoolIdLabel" runat="server" Text='<%# Eval("schoolId") %>' />
        <br />
        schname:
        <asp:Label ID="schnameLabel" runat="server" Text='<%# Bind("schname") %>' />
        <br />
        address:
        <asp:Label ID="addressLabel" runat="server" Text='<%# Bind("address") %>' />
        <br />
        tel:
        <asp:Label ID="telLabel" runat="server" Text='<%# Bind("tel") %>' />
        <br />
        scdate:
        <asp:Label ID="scdateLabel" runat="server" Text='<%# Bind("scdate") %>' />
        <br />
    </ItemTemplate>
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" 
            BorderColor="Red" BorderStyle="Solid" HorizontalAlign="Center" />
        <InsertRowStyle HorizontalAlign="Right" BorderColor="#669900" 
            BorderStyle="Dashed" BorderWidth="1px" Font-Names="tahoma" 
            VerticalAlign="Middle" Wrap="True" />
        <EditRowStyle BackColor="#2461BF" />
</asp:FormView>
    
            <br />fhh
            <br />
        </ContentTemplate>
    </asp:UpdatePanel>
    <p>
    </p>
    <p>
         </p>
    <p>
<asp:SqlDataSource ID="schoolsDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:studentsdbConnectionString %>" 
    DeleteCommand="DELETE FROM [Schools] WHERE [schoolId] = @schoolId" 
    InsertCommand="INSERT INTO [Schools] ([schname], [address], [tel], [scdate]) VALUES (@schname, @address, @tel, @scdate)" 
    SelectCommand="SELECT * FROM [Schools]" 
    
    
    
    UpdateCommand="UPDATE [Schools] SET [schname] = @schname, [address] = @address, [tel] = @tel, [scdate] = @scdate WHERE [schoolId] = @schoolId" >
    <DeleteParameters>
        <asp:Parameter Name="schoolId" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="schname" Type="String" />
        <asp:Parameter Name="address" Type="String" />
        <asp:Parameter Name="tel" Type="String" />
        <asp:Parameter DbType="DateTime" Name="scdate" />
        <asp:Parameter Name="schoolId" Type="Int32" />
    </UpdateParameters>
    <InsertParameters>
    <asp:Parameter Name="schoolId" Type="Int32" />
        <asp:Parameter Name="schname" Type="String" />
        <asp:Parameter Name="address" Type="String" />
        <asp:Parameter Name="tel" Type="String" />
        <asp:Parameter DbType="DateTime" Name="scdate" />
    </InsertParameters>
</asp:SqlDataSource>

        <br />
    </p>
    <p>
      <div id="schools">
       <asp:GridView ID="GridView1" runat="server" DataSourceID="schoolsDataSource1" 
    AutoGenerateColumns="False" DataKeyNames="schoolId" CellPadding="4" 
            ForeColor="#333333" GridLines="None" Width="100%" AllowPaging="True" 
            AllowSorting="True" onselectedindexchanged="GridView1_SelectedIndexChanged">
           <RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" 
               Font-Names="tahoma" />
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField DataField="schoolId" HeaderText="schoolId" 
            InsertVisible="False" ReadOnly="True" SortExpression="schoolId" 
            Visible="False" />
        <asp:BoundField DataField="schname" HeaderText="name" 
            SortExpression="schname" />
        <asp:BoundField DataField="address" HeaderText="address" 
            SortExpression="address" />
        <asp:BoundField DataField="tel" HeaderText="tel" SortExpression="tel" />
        <asp:TemplateField HeaderText="date" SortExpression="scdate">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("scdate") %>' 
                    ondatabinding="scdateTextBox_DataBinding"></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("scdate") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
           <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
           <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
           <EmptyDataTemplate>
               schoolId
           </EmptyDataTemplate>
           <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
           <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" 
               HorizontalAlign="Center" Font-Names="tahoma" />
           <EditRowStyle BackColor="#999999" HorizontalAlign="Center" />
           <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
    </div>
 </p>
    <uc1:Print ID="Print1"  runat="server" />
    <p>
         </p>
    <p>
        <input id="Button1" type="button" value="طباعة"  önclick="javascript:CallPrint('schools')" />  </p>
</asp:Content>

<asp:Content ID="Content3" runat="server" 
    contentplaceholderid="ContentPlaceHolder2">
    <p>
    add new schhol</p>

</asp:Content>


and this is the .CS Code

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Adminstration_schools1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

            filldate();
     
    }

    private void filldate()
    {
        if (FormView1.CurrentMode == FormViewMode.Insert)
        {
            TextBox dateadded = FormView1.FindControl("scdateTextBox") as TextBox;

                dateadded.Text = DateTime.Now.ToString();
        }
    }

    protected void FormView1_DataBound(object sender, EventArgs e)
    {
        filldate();
    }
    protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
       // FormView1.ChangeMode(FormViewMode.Insert);
       // filldate();

        foreach (DictionaryEntry entry in e.Values)
        {
            if (entry.Value.Equals(""))
            {
                // Use the Cancel property to cancel the 
                // insert operation.
                e.Cancel = true;

                ClientScript.RegisterStartupScript(GetType(), "hwa", "error();", true);

            }
        }
    }

    protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        //FormView1.ChangeMode(FormViewMode.Insert);
        //filldate();

        if (e.AffectedRows > 0)
        {

            ClientScript.RegisterStartupScript(GetType(), "hwa", "ok();", true);
        }


    }
    protected void scdateTextBox_DataBinding(object sender, EventArgs e)
    {
        TextBox scdateTextBox = (TextBox)sender;

        // Retrieve and assign default number
        scdateTextBox.Text = DateTime.Now.ToString();

        
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
Posted

1 solution

Hello

you had written code for adding javascript while post back, your code as below

C#
if (e.AffectedRows > 0)
        {

            ClientScript.RegisterStartupScript(GetType(), "hwa", "ok();", true);
        }



Update your code with below code


C#
if (e.AffectedRows > 0)
        {
              ScriptManager.RegisterStartupScript(this, this.GetType(), "hwa", "ok();", true);
           
        }



Regards
Gaurav Dhol
 
Share this answer
 
Comments
progahmed 30-Aug-12 7:14am    
ohhhhhhh, You are right

It is working successfully .

Thank you Gaurav Dhol Very much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900