Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

Script in a DataList

$
0
0

Hello everybody,

In the ItemTemplate of a DataList, I display a user control that contains an AjaxControlToolkit Slider to input a numeric data.

At the second attempt to modify the value, this makes the page display an exception :

Invalid postback or callback argument.  
Event validation is enabled using <pages enableEventValidation="true"/>
in configuration or <%@ Page EnableEventValidation="true" %>
in a page.
For security purposes, this feature verifies that arguments to
postback or callback events originate from the server control
that originally rendered them. If the data is valid and expected,
use the ClientScriptManager.RegisterForEventValidation method
in order to register the postback or callback data for validation.

As advised in a previous thread, I referred to this page, and inserted this in the page, at the beginning of the Content control :

<script runat="server" >

    string _cbMessage = "";
    // Define method that processes the callbacks on server.
    public void RaiseCallbackEvent(String eventArgument)
    {
        if (DataList1.Controls.Count > 0)
        {
            try
            {
                UserControls_AffichePlat ap = (UserControls_AffichePlat)DataList1.Controls[0].FindControl("AffichePlat1");
                TextBox txb = (TextBox)ap.FindControl("txbCuissonLineaire");
                AjaxControlToolkit.SliderExtender sli = (AjaxControlToolkit.SliderExtender)ap.FindControl("SliderExtender1");
                Page.ClientScript.ValidateEvent(txb.UniqueID, this.ToString());
                Page.ClientScript.ValidateEvent(sli.UniqueID, this.ToString());
                _cbMessage = "Correct event raised callback.";
            }
            catch(Exception ex1)
            {
                _cbMessage = "Error when raising callback : " + ex1.Message;
            }
        }
    }

    // Define method that returns callback result.
    public string GetCallbackResult()
    {
        return _cbMessage;
    }

    protected override void Render(HtmlTextWriter writer)
    {
        if (DataList1.Controls.Count > 0)
        {
            UserControls_AffichePlat ap = (UserControls_AffichePlat)DataList1.Controls[0].FindControl("AffichePlat1");
            TextBox txb = (TextBox)ap.FindControl("txbCuissonLineaire");
            AjaxControlToolkit.SliderExtender sli = (AjaxControlToolkit.SliderExtender)ap.FindControl("SliderExtender1");
            Page.ClientScript.RegisterForEventValidation(sli.UniqueID, this.ToString());
            Page.ClientScript.RegisterForEventValidation(txb.UniqueID, this.ToString());
        }
        base.Render(writer);
    }</script><script type="text/javascript">
    var value1 = new Date();

    function ReceiveServerData(arg, context)
    {
        alert('ReceiveServerData');
        Message.innerText = arg;
        Label1.innerText = "Callback completed at " + value1;
        value1 = new Date();
    }
    function ProcessCallBackError(arg, context)
    {
        alert('ProcessCallBackError');
        Message.innerText = "An error has occurred.";
    }</script>

and that in the code behind :

    private List<PlatPourCommande> lst = new List<PlatPourCommande>();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.dnlCategorie.SelectedValue = "4";

            using (SqlConnection cnx = new SqlConnection(ConfigurationManager.ConnectionStrings["DataConnectionString"].ConnectionString))
            {
                while (cnx.State == ConnectionState.Closed)
                {
                    try
                    {
                        cnx.Open();
                    }
                    catch
                    {
                    }
                }
                using (SqlCommand cmd = new SqlCommand("SELECT id FROM Plat WHERE idCategorie=4", cnx))
                {
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        PlatPourCommande p = new PlatPourCommande(reader.GetInt32(0));
                        lst.Add(p);
                    }
                    reader.Close();
                }
                cnx.Close();
            }
            ClientScriptManager cs = Page.ClientScript;
            String cbReference = cs.GetCallbackEventReference("'" +
                Page.UniqueID + "'", "arg", "ReceiveServerData", "","ProcessCallBackError", false);
            String callbackScript = "function CallTheServer(arg, context) {" +
                cbReference + "; }";
            cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer",
                callbackScript, true);

            DataList1.DataSource = lst;
            dtbc(DataList1);
        }
    }

dtbc and dtbs are for DataBind(), with exception management.

RaiseCallbackEvent is executed, but I am afraid :

  • ReceiveServerData and ProcessCallbackError never display the alert
  • the page still displays the same exception at the second attempt to modify the value with the slider.

Any idea of what I made wrong ?


Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>