[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1071
  • Last Modified:

The Gridview, The UpdatePanel and the Fullpostback

Hi Experts,

I'm getting crazy with an asp.net (C# code) page a client asked me update to implement with Ajax (the page was created by some else).

I tried to implement Ajax: inserted a scriptmanager in the page (web.config automatically and correctly updated), put the gridview inside an UpdatePanel (with ChildrenAsTriggers true). Now when I click edit on the gridview, it still reloads the page doing a full postback, so it seems that either the ajax panel isn't working or it's not causing the expected partial postback.

The last bit of info is that this website has a masterpage, do I need to configure something there too for ajax to work properly?

Tell me if you need more code snippets to evaluate the problem.
GRIDVIEW
<asp:GridView ID="gvCoefficienti" runat="server"  CssClass="gvStyle" 
            GridLines="None"    CellPadding="0" CellSpacing ="2" 
            AutoGenerateColumns="false" AutoGenerateEditButton="True" 
            SkinID="ResultGridViewFast"   EmptyDataText ="Nessun Record trovato" 
            EmptyDataRowStyle-Font-Names ="Verdana" EmptyDataRowStyle-ForeColor="#336699" 
            EmptyDataRowStyle-HorizontalAlign ="Center" 
            EmptyDataRowStyle-Font-Size ="Medium" 
            OnRowDataBound="gvCoefficienti_RowCreated" RowStyle-Height="25" 
            onrowediting="gvCoefficienti_RowEditing" > 

UPDATEPANEL
<asp:UpdatePanel ID="UP1" runat="server" Visible="True" UpdateMode="Conditional" ChildrenAsTriggers="true"><ContentTemplate>

Open in new window

0
DuskDweller
Asked:
DuskDweller
1 Solution
 
jkofteCommented:
you should place your grid inside the update panel.
0
 
DuskDwellerAuthor Commented:
Sorry the code snippet is misleading, the GridView is actually inside the updatepanel tags. I'm going to edit the snippet.
0
 
DuskDwellerAuthor Commented:
Whatever, I can't edit the code snippet in the question, but anyway, the GridView IS in the UpdatePanel.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
DuskDwellerAuthor Commented:
Still need help on this!
0
 
Miguel OzSoftware EngineerCommented:
The script manager should be defined in one place and that should be your child page where your update panel is.
Check that master page does not contain a script manager. (you have to use this one if it exits there)
Check this example:
http://www.4guysfromrolla.com/articles/102407-1.aspx
especially "Closing Comments on the UpdatePanel"
If you need more help plese post complete markup of both master and child page and check code behind for any javascript that is being registered and put those snippets as well.
0
 
DuskDwellerAuthor Commented:
Here's the page code.
<%@ Page Language="C#" MasterPageFile="~/PrincipalMP.master" AutoEventWireup="true" CodeFile="CoefficienteRegressione.aspx.cs" Inherits="Sistema_rpt_Dati_Sist_Temp_M_Curr" Title="" %><%@ Register Src="~/src/rptNome.ascx" TagName="rptNome" TagPrefix="rptNome" %>
<asp:Content ID="Content1"
ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  <table style="width:100%;height :100%" border="0" cellpadding="0"
  cellspacing="0">
    <tr>
      <td style="width :100%;height :100%" valign="top">
        <table border="0" cellpadding="0" cellspacing="0"
        style="width:100%;">
          <tr>
            <td class="tdNomeReport">
            <asp:Label id="lblNome" runat="server" Text="">
            </asp:Label>&#160;&#160;&#160;&#160;</td>
          </tr>
          <tr>
            <td></td>
          </tr>
          <asp:ScriptManager ID="ScriptManager1" runat="server">
          </asp:ScriptManager>
          <tr>
            <td class="tdGriglia"
            style="height:40%;text-align :center;vertical-align:top;width:100%">

              <table border="0" cellpadding="0" cellspacing="0"
              style="width:100%;height:100%;vertical-align :middle">

                <tr>
                  <td style="height:8px">&#160;</td>
                </tr>
                <tr>
                  <td style="height:15%;text-align :left;vertical-align :bottom">

                    <asp:Panel ID="Panel2" runat="server"
                    CssClass="EtichettaSezione" Height="90%"
                    style="text-align :left;vertical-align :bottom">
                    &#160;Filtri ricerca</asp:Panel>
                  </td>
                </tr>
                <tr>
                  <td style="height:20%;text-align :left;vertical-align:top">

                    <asp:Panel ID="Panel3" runat="server"
                    CssClass="FiltroPanel"
                    style="text-align:left;height:100%">
                      <table border="0" cellpadding="0"
                      cellspacing="0" width="100%"
                      style="vertical-align:middle;height :100%">
                        <tr id="st" style="height:38px">
                          <td class="tdTestoFiltro"
                          style="text-align:left ;width:25%;height:5%;vertical-align :middle">
                          &#160; Selezionare Anno &#160; 
                          <asp:DropDownList ID="ddlAnno"
                          runat="server" CssClass="ddlText">
                          </asp:DropDownList></td>
                          <td style="text-align:right ;vertical-align :middle;width:100%">
                          <asp:ImageButton ID="ImageButton3"
                          runat="server"
                          ImageUrl="~/Images/update_2.jpg"
                          AlternateText="Calcola Stima"
                          OnClick="imgCerca_Click" />&#160; &#160;
                          &#160; &#160; &#160;</td>
                        </tr>
                      </table>
                    </asp:Panel>
                  </td>
                </tr>
              </table>
            </td>
          </tr>
          <tr id="2">
            <td class="tdGriglia" style="text-align:left">
              <table border="0" cellpadding="0" cellspacing="0"
              style="vertical-align :top;width:100%;"
              class="tbTab">
                <tr>
                  <td style="height:10px;text-align :center;font-family: Verdana; color: #336699; font-size:12pt; font-weight: bold;">
                  </td>
                </tr>
                <tr>
                  <td style="height:7%;text-align :center;font-family: Verdana; color: #336699; font-size:12pt; font-weight: bold;">

                    <asp:Label ID="lblFormula" runat="server"
                    Text=""></asp:Label>
                  </td>
                </tr>
                <tr>
                  <td style="height:10px;text-align :left;font-family: Verdana; color: #336699; font-size:12pt; font-weight: bold;">
                  &#160;&#160;&#160; 
                  <asp:ImageButton ID="imgAnnulla" runat="server"
                  ImageUrl="~/Images/Cancella.gif" />&#160;&#160;&#160;
                  
                  <asp:ImageButton ID="imgSalva" runat="server"
                  ImageUrl="~/Images/Salva.gif" />&#160;&#160;&#160;
                  
                  <asp:ImageButton ID="imgNuovo" runat="server"
                  ImageUrl="~/Images/collapse_info.gif" />&#160;&#160;&#160;</td>
                </tr>
                <asp:UpdatePanel ID="UP1" runat="server"
                Visible="True" UpdateMode="Conditional"
                ChildrenAsTriggers="True">
                  <ContentTemplate>
                    <tr>
                      <td style="vertical-align :top">
                        <asp:GridView ID="gvCoefficienti"
                        runat="server" CssClass="gvStyle"
                        GridLines="None" CellPadding="0"
                        CellSpacing="2" AutoGenerateColumns="false"
                        AutoGenerateEditButton="True"
                        SkinID="ResultGridViewFast"
                        EmptyDataText="Nessun Record trovato"
                        EmptyDataRowStyle-Font-Names="Verdana"
                        EmptyDataRowStyle-ForeColor="#336699"
                        EmptyDataRowStyle-HorizontalAlign="Center"
                        EmptyDataRowStyle-Font-Size="Medium"
                        OnRowDataBound="gvCoefficienti_RowCreated"
                        RowStyle-Height="25"
                        onrowediting="gvCoefficienti_RowEditing"
                        onrowcancelingedit="gvCoefficienti_RowCancelingEdit"
                        onrowupdating="gvCoefficienti_RowUpdating"
                        EnableViewState="True">
                          <RowStyle Height="25px"></RowStyle>
                          <EmptyDataRowStyle HorizontalAlign="Center"
                          Font-Names="Verdana" Font-Size="Medium"
                          ForeColor="#336699"></EmptyDataRowStyle>
                          <Columns>
                            <asp:BoundField DataField="descrizione"
                            ReadOnly="True" HeaderText="Ambito"
                            HeaderStyle-HorizontalAlign="Center"
                            ItemStyle-HorizontalAlign="left"
                            HtmlEncode="false">
                              <HeaderStyle HorizontalAlign="Center">
                              </HeaderStyle>
                              <ItemStyle HorizontalAlign="Left">
                              </ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="P3"
                            ReadOnly="False" HeaderText="P3"
                            HeaderStyle-HorizontalAlign="Center"
                            ItemStyle-HorizontalAlign="center"
                            HtmlEncode="false">
                              <HeaderStyle HorizontalAlign="Center">
                              </HeaderStyle>
                              <ItemStyle HorizontalAlign="Center">
                              </ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="P2"
                            ReadOnly="False" HeaderText="P2"
                            HeaderStyle-HorizontalAlign="Center"
                            ItemStyle-HorizontalAlign="center">
                              <HeaderStyle HorizontalAlign="Center">
                              </HeaderStyle>
                              <ItemStyle HorizontalAlign="Center">
                              </ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="P1"
                            ReadOnly="False" HeaderText="P1"
                            HeaderStyle-HorizontalAlign="Center"
                            ItemStyle-HorizontalAlign="center">
                              <HeaderStyle HorizontalAlign="Center">
                              </HeaderStyle>
                              <ItemStyle HorizontalAlign="Center">
                              </ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="P0"
                            ReadOnly="False" HeaderText="P0"
                            HeaderStyle-HorizontalAlign="Center"
                            ItemStyle-HorizontalAlign="center">
                              <HeaderStyle HorizontalAlign="Center">
                              </HeaderStyle>
                              <ItemStyle HorizontalAlign="Center">
                              </ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="tipoCons"
                            ReadOnly="True" HeaderText="tipocons"
                            HeaderStyle-HorizontalAlign="Center"
                            ItemStyle-HorizontalAlign="center"
                            visible="false">
                              <HeaderStyle HorizontalAlign="Center">
                              </HeaderStyle>
                              <ItemStyle HorizontalAlign="Center">
                              </ItemStyle>
                            </asp:BoundField>
                          </Columns>
                        </asp:GridView>
                      </td>
                    </tr>
                  </ContentTemplate>
                </asp:UpdatePanel>
                <tr>
                  <td class="tdTestoFiltro"
                  style="text-align:left ;height:10px;vertical-align :middle">
                  </td>
                </tr>
                <tr>
                  <td style="text-align :left;font-family: Verdana; color: #336699; font-size:10pt; font-weight: bold;">
                  </td>
                </tr>
                <tr>
                  <td class="tdTestoFiltro"
                  style="text-align:left ;height:5px;vertical-align :middle">
                  </td>
                </tr>
              </table>
            </td>
          </tr>
          <tr id="3">
            <td style="text-align :center;vertical-align:top">
              <table border="0" cellpadding="0" cellspacing="0"
              style="vertical-align :top; width:100%;text-align :center;"
              class="tbTab">
                <tr>
                  <td style="height:7%;text-align :center;vertical-align :top">

                    <asp:Panel ID="Panel1" runat="server"
                    CssClass="EtichettaSezione" Height="90%"
                    style="text-align :center;vertical-align :bottom"
                    Visible="false">
                      <asp:Label ID="LblTemp" runat="server"
                      Text=""></asp:Label>
                    </asp:Panel>
                  </td>
                </tr>
                <tr>
                  <td style="text-align:center ;height:5px;vertical-align :middle">
                  </td>
                </tr>
                <tr>
                  <td style="text-align:center">
                    <asp:Table runat="server" ID="tbDatiTemp"
                    CellPadding="2" CellSpacing="2"
                    BorderColor="black"></asp:Table>
                  </td>
                </tr>
              </table>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</asp:Content>

Open in new window

0
 
DuskDwellerAuthor Commented:
This is the master page code:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="PrincipalMP.master.cs" Inherits="PrincipalMP" EnableTheming="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head id="Head1" runat="server">
    <title>Project</title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
  </head>
  <body style="position: absolute; width: 100%; height: 98%; background-color: #ffffff; margin-top: 0; margin-left: 0; margin-bottom: 0; margin-right: 0;"
  id="body_Main">
    <form id="form1" runat="server">
      <table style="width: 100%; height: 100%;" align="center"
      border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td style="empty-cells:show;height:5px"></td>
        </tr>
        <tr>
          <td style="width:100%; height: 6%;vertical-align :top"
          align="left">
            <table border="0"
            style="width: 100%; height: 100%; border: 0; background-color: #ffffff;"
            cellpadding="0" cellspacing="0">
              <tr style="height :90%">
                <td style="width: 20%;vertical-align :middle">
                  <asp:Image ID="imgLogo" runat="server"
                  ImageUrl="~/images/logo-New.jpg" />
                </td>
                <td style="vertical-align : top;width: 50%;">
                  <table border="0" cellpadding="0"
                  cellspacing="0">
                    <tr>
                      <td align="left"
                      style="width: 70%; height: 10%;"
                      class="tdDatiTestata">&#160; 
                      <asp:Label ID="lblUtente" runat="server"
                      Text="Versione:"></asp:Label></td>
                    </tr>
                    <tr>
                      <td align="left"
                      style="width: 8%; height: 10%;"
                      class="tdDatiTestata">&#160; 
                      <asp:Label ID="Label1" runat="server"
                      Text="Database:"></asp:Label>Project Name</td>
                    </tr>
                    <tr>
                      <td align="left"
                      style="width: 8%; height: 10%;"
                      class="tdDatiTestata">&#160; 
                      <asp:Label ID="Label2" runat="server"
                      Text="Data:"></asp:Label>&#160; 
                      <asp:Label ID="Data" runat="server" Text="">
                      </asp:Label></td>
                    </tr>
                  </table>
                </td>
                <td style="width: 30%;vertical-align :top;"
                align="right" class="tdDatiTestata">
                  <table border="0" cellpadding="0"
                  cellspacing="0">
                    <tr>
                      <td align="right" style="width: 70%;"
                      class="tdDatiTestataBold">
                        <asp:Label ID="Label3" runat="server"
                        Text=" UTR/C1 E.ACE"></asp:Label>
                      </td>
                    </tr>
                    <tr>
                      <td align="right" style="width: 8%;"
                      class="tdDatiTestata">
                        <asp:Label ID="Label4" runat="server"
                        Text="UTR CENTRO 1"></asp:Label>
                      </td>
                    </tr>
                    <tr>
                      <td align="right" style="width: 8%;"
                      class="tdDatiTestataBold">
                        <asp:Label ID="Label5" runat="server"
                        Text="Amministratore"></asp:Label>
                      </td>
                    </tr>
                  </table>
                </td>
                <td align="right" style="vertical-align :top">
                  <asp:Image ID="Image1" runat="server"
                  ImageUrl="~/images/user1.jpg" />
                </td>
              </tr>
            </table>
          </td>
        </tr>
        <tr>
          <td style="width:100%; height:10px;empty-cells:show"
          valign="top" align="left"></td>
        </tr>
        <tr>
          <td style="width:100%; height: 2%;vertical-align :top"
          align="left">
            <table style="width:100%;height :100%" border="0"
            cellpadding="0" cellspacing="0">
              <tr style="background-color:#e8f1ff">
                <td style="height :5px;vertical-align :top;"
                class="menu">
                  <asp:Menu ID="masterMenu"
                  StaticEnableDefaultPopOutImage="False"
                  DataSourceID="SiteMapDataSource1"
                  MaximumDynamicDisplayLevels="4" runat="Server"
                  DynamicHorizontalOffset="2" Font-Names="Verdana"
                  Font-Bold="true" Font-Size="0.9em"
                  ForeColor="#345381" StaticSubMenuIndent="10px"
                  Orientation="Horizontal" StaticDisplayLevels="2">
                    <StaticMenuStyle HorizontalPadding="4" />
                    <StaticMenuItemStyle HorizontalPadding="2" />
                    <StaticSelectedStyle />
                    <DynamicMenuStyle Font-Names="Verdana"
                    Font-Size="10" BackColor="#ffffcc"
                    BorderColor="#e8f1ff" BorderStyle="solid"
                    BorderWidth="1" />
                    <DynamicMenuItemStyle VerticalPadding="3"
                    HorizontalPadding="3" Font-Underline="true" />
                    <DynamicHoverStyle BackColor="#ffffcc" />
                  </asp:Menu>
                  <asp:SiteMapDataSource ID="SiteMapDataSource1"
                  runat="server" />
                </td>
              </tr>
            </table>
          </td>
        </tr>
        <tr>
          <td style="width:100%; height: 78%" valign="top"
          align="left">
            <table style="width: 100%; height: 100%" border="0"
            cellpadding="0" cellspacing="0">
              <tr>
                <td style="width: 100%; height: 100%" valign="top"
                align="left">
                  <asp:ContentPlaceHolder ID="ContentPlaceHolder1"
                  runat="server"></asp:ContentPlaceHolder>
                </td>
              </tr>
            </table>
          </td>
        </tr>
        <tr>
          <td style="width:100%; height: 5%;background-color:#e8f1ff"
          align="left">
            <table style="height:100%;width:100%" cellpadding="0"
            cellspacing="0" border="0">
              <tr>
                <td class="cellaLinea"
                style="width :100%; empty-cells:show;background-repeat :repeat-x">
                </td>
              </tr>
              <tr>
                <td style="width:100%;height:100%">
                  <table style="width:100%; height: 100%;background-color:#e8f1ff">

                    <tr>
                      <td align="left">
                        <font class="fDati">Project Name
                        Project</font>
                      </td>
                      <td align="right">
                        <font class="fDati">
                        Project Name &amp;
                        Project</font>
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </form>
  </body>
</html>

Open in new window

0
 
Miguel OzSoftware EngineerCommented:
The gridview binding is done on code behind. The markup seems OK.
Is there code behind generated javascript or handling of the edit event? I am looking for any custom C#/VB.net code that is doing gv edit different from default implementation.
0
 
DuskDwellerAuthor Commented:
It's using the built in edit function, intercepting RowEditing, RowCancelingEdit, RowUpdating as needed.

Example:
 
protected void gvCoefficienti_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        DataTable table = Session["Table"] as DataTable;
        gvCoeff.EditIndex = -1;
        gvCoeff.DataSource = table;
        gvCoeff.DataBind();
    }

Open in new window

0
 
DuskDwellerAuthor Commented:
In the previous code snippet, don't mind the gridview different name in the code, I was just trying something and forgot to put the correct name back there.
0
 
Miguel OzSoftware EngineerCommented:
If there is no JS/code behind code. Can you post all code snippets that do the gridview binding.
Unfortunately solution depends on how the prevous developer build the binding, some people just develop with version 1.0 in mind not with future expansion maintenace in mind. If this is your case, it may become a major headache for you to alter it. If that is the case you are better off redesigning the page rather than hacking your way for a quick fix.
0
 
DuskDwellerAuthor Commented:
This is where the grid is populated and bound:
protected int CaricaGrigliaCoefficienti(out string errmessage, out DataTable dtReturn)
    {
        string strSql = "";
        DataTable dt;
        int anno = 0;
        DataRow dr;

        try
        {
            lblFormula.Text = "Coefficienti di Regressione";
            anno = int.Parse(ViewState["Anno"].ToString());
            errmessage = "";
            cdb.InitConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionStrings"].ToString());
            EsitoMetodo = cdati.Set_Table_Coeff(out errMessage, out dtCoeff);   
            EsitoMetodo = cdati.Get_Coefficienti(out errMessage, out  strSql, anno);
            EsitoMetodo = cdb.SelectCommand(out  errMessage, out dt, strSql);

            foreach (DataRow drCoeff in dt.Rows)
            {
              dr = dtCoeff.NewRow();

              dr[0] = drCoeff["descrizione"];
              dr[1] = drCoeff["P3"];
              dr[2] = drCoeff["P2"];
              dr[3] = drCoeff["P1"];
              dr[4] = drCoeff["P0"];
              dr[5] = drCoeff["tipoCons"];

              dtCoeff.Rows.Add(dr);
            }

            Session["Table"] = dtCoeff;
            gvCoeff.DataSource = dtCoeff;
            gvCoeff.DataBind();
            dtReturn = dtCoeff;

            return 0;
        }
        catch (Exception ex)
        {
            dtReturn = null;
            errmessage = ex.Message;
            return -1;
        }
    }

Open in new window

0
 
GlobaLevelCommented:
The last bit of info is that this website has a masterpage, do I need to configure something there too for ajax to work properly?

>>I'm in coming a little late here...but masterpages are used to control layout...themes are used for controls...if you want your AJAX to cascade to various pages use themes/skins...heres an example:

http://stackoverflow.com/questions/1973240/net-gridview-themes-examples
0
 
Miguel OzSoftware EngineerCommented:
Your posted code does not help. I need to check how this method is call within the page and if there any bindings in the code elsewhere.
Can you post al your code behind removing any sensitive info?
0
 
DuskDwellerAuthor Commented:
Here's the full code-behind:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;

public partial class CoefficienteRegressione : System.Web.UI.Page
{
    string sNomeReport = "";
    int EsitoMetodo;
    string errMessage;
    cDB cdb = new cDB();
    CaricaDatiReportTemp cdati = new CaricaDatiReportTemp();
    DataTable dt;
    DataTable dtCoeff;
    DataTable dtCoeffMolt;
    string strSQL = "";
   
    protected void Set_NomeReport(string NomeReport)
    {
        Label lb;
        UserControl uc;
        ContentPlaceHolder c;
        MasterPage a;

        a = Page.Master;

        c = (ContentPlaceHolder)a.FindControl("ContentPlaceHolder1");
        uc = (UserControl)c.FindControl("rptNomeR");
        lb = (Label)uc.FindControl("lblNome");
        lb.Text = NomeReport;
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
           return;
        }

        sNomeReport = "Coefficienti di Regressione";
        lblNome.Text = sNomeReport;
        cdb.InitConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionStrings"].ToString());
        EsitoMetodo = cdati.List_ANNO(out errMessage, out strSQL);
        EsitoMetodo = cdb.SelectCommand(out  errMessage, out dt, strSQL);
        ddlAnno.DataValueField = "anno";
        ddlAnno.DataTextField = "anno";
        ddlAnno.DataSource = dt;
        ddlAnno.DataBind();

        Cerca();
    }

    protected void imgCerca_Click(object sender, ImageClickEventArgs e)
    {
        Cerca();
    }

    protected void Cerca()
    {
        ViewState["Anno"] = ddlAnno.SelectedValue;
        EsitoMetodo = CaricaGrigliaCoefficienti(out errMessage, out dtCoeffMolt);    
    }

    protected int CaricaGrigliaCoefficienti(out string errmessage, out DataTable dtReturn)
    {
        string strSql = "";
        DataTable dt;
        int anno = 0;
        DataRow dr;
        int i = 0;
        string newYear = "";

        try
        {
            anno = int.Parse(ViewState["Anno"].ToString());
            errmessage = "";
            cdb.InitConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionStrings"].ToString());
            EsitoMetodo = cdati.Set_Table_Coeff(out errMessage, out dtCoeff);   
            EsitoMetodo = cdati.Get_Coefficienti(out errMessage, out  strSql, anno);
            EsitoMetodo = cdb.SelectCommand(out  errMessage, out dt, strSql);

            if (dt.Rows.Count <= 0)
            {
                EsitoMetodo = cdati.Get_Coefficienti(out errMessage, out  strSql, anno - 1);
                EsitoMetodo = cdb.SelectCommand(out  errMessage, out dt, strSql);

                if (dt.Rows.Count > 0)
                {
                    string[] valori = new string[4];

                    for (int j = 1; j <= 4; j++)
                    {
                        i = 0;
                        foreach (DataRow drCons in dt.Rows)
                        {
                            valori[i++] = drCons["P3"].ToString();
                            valori[i++] = drCons["P2"].ToString();
                            valori[i++] = drCons["P1"].ToString();
                            valori[i++] = drCons["P0"].ToString();
                        }

                        SalvaGrigliaCoefficienti(j, valori);
                    }
                    newYear = " (Dati inizializzati dal " + (anno - 1) + ")";
                }
            }

            lblFormula.Text = "Coefficienti di Regressione " + anno + newYear;

            foreach (DataRow drCoeff in dt.Rows)
            {
              dr = dtCoeff.NewRow();

              dr[0] = drCoeff["descrizione"];
              dr[1] = drCoeff["P3"];
              dr[2] = drCoeff["P2"];
              dr[3] = drCoeff["P1"];
              dr[4] = drCoeff["P0"];
              dr[5] = drCoeff["tipoCons"];

              dtCoeff.Rows.Add(dr);
            }

            Session["Table"] = dtCoeff;
            gvCoeff.DataSource = dtCoeff;
            gvCoeff.DataBind();
            dtReturn = dtCoeff;

            return 0;
        }
        catch (Exception ex)
        {
            dtReturn = null;
            errmessage = ex.Message;
            return -1;
        }
    }

    protected int SalvaGrigliaCoefficienti(int tipo, string[] valori)
    {
        string strSql = "";
        int anno = 0;

        try
        {
            anno = int.Parse(ViewState["Anno"].ToString());
            errMessage = "";
            cdb.InitConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionStrings"].ToString());
            cdb.OpenConnection();
            cdb.BeginTrans();

            EsitoMetodo = cdati.Update_Coefficienti(out errMessage, out strSql, anno, tipo, valori);
            EsitoMetodo = cdb.ExecSql(out errMessage, strSql);

            if (EsitoMetodo < 0)
                throw new Exception("Impossibile aggiornare la tabella LPC");
            for (int i = 0; i<= 3; i++)
            {
                if (valori[i].Length > 0)
                {
                    EsitoMetodo = cdati.Set_Coefficienti(out errMessage, out strSql, anno, valori[i], tipo, "C" + (3 - i));
                    EsitoMetodo = cdb.ExecSql(out errMessage, strSql);
                    if (EsitoMetodo < 0)
                        throw new Exception("Impossibile inserire dati nella tabella LPC");
                }
            }
            cdb.CommitTrans();
            return 0;
        }
        catch (Exception ex)
        {
            cdb.RollbackTrans();
            errMessage = ex.Message;
            return -1;
        }
    }

    protected void Page_Unload(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }

        cdb.CloseConnection();
        cdb = null;
    }

    public void gvCoeff_RowCreated(Object sender, GridViewRowEventArgs e)
    {

    }

    protected void gvCoeff_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }

    protected void gvCoeff_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }

    protected void gvCoeff_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

    }

    protected void gvCoeff_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if ((e.CommandName == "cmdUpd") && (gvCoeff.EditIndex >= 0))
        {
            string[] valori = new string[4];
            int i = 0;
            string prova = "";
            int mod = 0;

            dtCoeff = Session["Table"] as DataTable;

            i = 0;

            for (int j = 1; j <= 4; j++)
            {
                prova = (gvCoeff.Rows[gvCoeff.EditIndex].Cells[j].Controls[1] as TextBox).Text;

                if (!((dtCoeff.Rows[gvCoeff.EditIndex][j] is System.DBNull) && (prova.Trim() == string.Empty)))
                {
                    if (prova.Trim() == string.Empty)
                    {
                        Utils.PopupAlertMsg("<br><br><br>Tutti i valori devono essere presenti!", this);
                        return;
                    }
                    else
                    {
                        if (dtCoeff.Rows[gvCoeff.EditIndex][j].ToString() != prova)
                        {
                            dtCoeff.Rows[gvCoeff.EditIndex][j] = prova;
                            valori[i++] = prova.Replace(",", ".");
                        }
                        else
                        {
                            mod++;
                            valori[i++] = "";
                        }
                    }
                }
                else
                {
                    mod++;
                    valori[i++] = "";
                }
            }

            if (mod < 4)
            {
                SalvaGrigliaCoefficienti(gvCoeff.EditIndex + 1, valori);
            }

            gvCoeff.EditIndex = -1;
            Session["Table"] = dtCoeff;
            gvCoeff.DataSource = dtCoeff;
            gvCoeff.DataBind();
        }
        else if (e.CommandName == "cmdUpd")
        {
            DataTable table = Session["Table"] as DataTable;
            gvCoeff.EditIndex = Convert.ToInt32(e.CommandArgument);
            gvCoeff.DataSource = table;
            gvCoeff.DataBind();
        }
        else if (e.CommandName == "cmdCanc")
        {
            DataTable table = Session["Table"] as DataTable;
            gvCoeff.EditIndex = -1;
            gvCoeff.DataSource = table;
            gvCoeff.DataBind();
        }
    }
    protected void gvCoeff_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {

    }
}

Open in new window

0
 
DuskDwellerAuthor Commented:
This is the full code of the MasterPage:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class PrincipalMP : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }

        Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));

        if (!CurrentUser.IsAuthenticated)
            Response.Redirect(CurrentApplication.pgDefault);


        Label2.Text = "Data accesso al sistema: " + CurrentUser.DataAccesso;
        Label3.Text = " Benvenuto <b> " + CurrentUser.Cognome + " " + CurrentUser.Nome + "</b>";
        // Label4.Text = CurrentUser.Nome;
        Label4.Text = "";
        Label5.Text = CurrentUser.DescUtente;
        lblUtente.Text = "Versione: " + CurrentApplication.Versione;  
    }
}

Open in new window

0
 
Miguel OzSoftware EngineerCommented:
Ok, some points here:
1) In Ajax, the Page_load will be executed regardless is full or asynch postback. This is the way Ajax works in Asp.net, check:
http://stackoverflow.com/questions/2008882/ajax-vs-server-side-execution-with-c-asp-net
http://www.java2s.com/Tutorial/ASP.NET/0400__Ajax/UpdatePanelServerSidePageExecutionLifecycle.htm

2) Your gridview will never work in Ajax unless your update panel includes the ImageButton ID="ImageButton3"
This button is triggering the gridview binding using imgCerca_Click event. This event is doing the postaback because it is not part of the update panel.

3) ScriptManager1.IsInAsyncPostBack is true when you are doing async postback, you can use that value on your page_load to do some required coding. you can easy verified that your gridview is using ajax, by adding this line to all your gridview events.
Debug.WriteLine("Event name apv value: " + sm1.IsInAsyncPostBack); // the result will show in your VS output window.


0
 
DuskDwellerAuthor Commented:
Thank you for you help!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now