Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 13th, 2012, 03:49 PM
Authorized User
 
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default Problem calling RegisterStartupScript

Problem calling RegisterStartupScript

Hi,

I need to populate a javascript message box on saving data from the Gridview to the database.

I'm calling the RegisterStartupScript in the RowUpdate event as shown below.
But the problem is the Javascript doesn't work.
I wanted the result of the confirmation message based on that, the changes will be saved into the database.

Any help?
Thanks in advance
Code:
using System;
using System.IO;
using System.Data;
using System.Data.Odbc;
using System.Configuration;
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.Web.Mail;
using System.Net.Mail;

public partial class Gem : System.Web.UI.Page
{

    OdbcDataAdapter dadapter;
    DataSet dset;
    DataTable dt = new DataTable();
    DateTime dtDBDa;
    DateTime dtDBA;

    OdbcConnection myConnectionString =
        new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            try
            {
            myConnectionString.Open();
            GridViewBind();
            }
            catch (Exception ex)
            {
            throw ex;
            }
            finally
            {
            myConnectionString.Close();
            myConnectionString.Dispose();
            }
        }
    }



    public void GridViewBind()
    {
        sql1 = " SELECT DISTINCT ... ";
        dadapter = new OdbcDataAdapter(sql1, myConnectionString);
        dset = new DataSet();
        dset.Clear();
        dadapter.Fill(dset);
        GridView1.PageIndex = 0;
        GridView1.DataSource = dset.Tables[0];
        GridView1.DataBind();
        dadapter.Dispose();
        myConnectionString.Close();
        myConnectionString.Dispose();
    }


    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewBind();
        GridView1.DataSource = dset.Tables[0];
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Pager)
        {
            DropDownList ddl = (DropDownList)(e.Row.FindControl("ddlpages"));
            Label lblPageCount = (Label)e.Row.FindControl("lblPageCount");

            if (lblPageCount != null)
                lblPageCount.Text = GridView1.PageCount.ToString();

            for (int i = 1; i <= GridView1.PageCount; i++)
            {
                ddl.Items.Add(i.ToString());
            }

            ddl.SelectedIndex = GridView1.PageIndex;

            if (GridView1.PageIndex == 0)
            {
                ((ImageButton)e.Row.FindControl("ImageButton1")).Visible = false;
                ((ImageButton)e.Row.FindControl("ImageButton2")).Visible = false;
            }

            if (GridView1.PageIndex + 1 == GridView1.PageCount)
            {
                ((ImageButton)e.Row.FindControl("ImageButton3")).Visible = false;
                ((ImageButton)e.Row.FindControl("ImageButton4")).Visible = false;
            }
        }
    }


    protected void ddlPages_SelectedIndexChanged(Object sender, EventArgs e)
    {
        GridViewRow gvrPager = GridView1.BottomPagerRow;
        DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");

        GridViewBind();
        GridView1.DataSource = dset.Tables[0];
        GridView1.PageIndex = ddlPages.SelectedIndex;
        GridView1.DataBind();
    }


    protected void Paginate(object sender, CommandEventArgs e)
    {
        int intCurIndex = GridView1.PageIndex;

        switch (e.CommandArgument.ToString().ToLower())
        {
            case "First":
                GridView1.PageIndex = 0;
                break;
            case "Prev":
                GridView1.PageIndex = intCurIndex - 1;
                break;
            case "Next":
                GridView1.PageIndex = intCurIndex + 1;
                break;
            case "Last":
                GridView1.PageIndex = GridView1.PageCount - 1;
                break;
        }
        GridView1.DataBind();
    }


    protected void CheckBox1_CheckedChanged
                                (object sender, EventArgs e)
    {
        CheckBox chkTest = (CheckBox)sender;
        GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;

        TextBox pd = (TextBox)grdRow.FindControl
                                            ("pd");        

        if (chkTest.Checked)
        {
            pd.ReadOnly = false;
            pd.ForeColor = System.Drawing.Color.Red;
            pd.Font.Bold = true;            
            grdRow.BackColor = System.Drawing.Color.Yellow;
        }
        else
        {
            pd.ReadOnly = true;
            pd.ForeColor = System.Drawing.Color.Blue;
            pd.Font.Bold = false;            
            grdRow.BackColor = System.Drawing.Color.White;
        }

        btnUpdate.Enabled = false;
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chk = (CheckBox)row.FindControl("chkSelect");
            if (chk.Checked)
                btnUpdate.Enabled = true;
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        OdbcCommand cmd = new OdbcCommand();
        int val = 0;

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chkUpdate = (CheckBox)
               GridView1.Rows[i].Cells[0].FindControl("chkSelect");

            if (chkUpdate != null)
            {
                if (chkUpdate.Checked)
                {
                    strQ = ((TextBox)
                        GridView1.Rows[i].FindControl("pd")).Text;                    
                    strQ_Hidden = ((HiddenField)
                        GridView1.Rows[i].FindControl("pd_hidden")).Value;

                    int num1;
                    bool result1 = int.TryParse(strQ, out num1);

                    int num2;
                    bool result2 = int.TryParse(strQ_Hidden, out num2);

                    if (num1 <= num2)
                    {
                        int diff;
                        diff = (num2 - num1);
                        Cache["diffmycache"] = diff;

                        cmd.Parameters.Clear();
                        cmd.CommandText = "INSERT INTO .....";
                        cmd.Parameters.Add("param1", strQ);
                        cmd.Connection = myConnectionString;
                        myConnectionString.Open();
                        cmd.ExecuteNonQuery();
                        myConnectionString.Close();

                        cmd.Parameters.Clear();
                        cmd.CommandText = "UPDATE .... ";
                        cmd.CommandText = cmd.CommandText + "tbl_g ";
                        cmd.CommandText = cmd.CommandText + "SET Disp = 1 ";
                        cmd.CommandText = cmd.CommandText + "WHERE ....";
                        cmd.Parameters.Add("param3", OdbcType.Int, 10, strQ);
                        cmd.Connection = myConnectionString;
                        myConnectionString.Open();
                        cmd.ExecuteNonQuery();
                        myConnectionString.Close();


                    }
                    else
                    {
                        string myStringVariable = string.Empty;
                        myStringVariable = "Error!";
                        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);
                        val = 1;
                    }
                }
            }

        }

        if (val == 0)
        {
            UncheckAll();
        }
    }


    private void UncheckAll()
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chkUncheck = (CheckBox)
                         row.FindControl("chkSelect");
            TextBox pd = (TextBox)
                           row.FindControl("pd");
            chkUncheck.Checked = false;
            pd.ReadOnly = true;
            pd.ForeColor = System.Drawing.Color.Blue;
            row.BackColor = System.Drawing.Color.White;
        }
    }


    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        if (e.Exception != null)
        {
            string x = e.Exception.Message;
            ClientScript.RegisterClientScriptBlock(this.GetType(), "script", x);

        }
        else
        {

            string someScript = "";
            someScript = "<script language='javascript'>alert('Called from CodeBehind');</script>";
            ClientScript.RegisterClientScriptBlock(this.GetType(), "script", someScript);
            GridViewBind();

        }
    }


}





Similar Threads
Thread Thread Starter Forum Replies Last Post
problem calling between frames beetle_jaipur Javascript How-To 0 November 12th, 2009 01:32 AM
Page.RegisterStartupScript shanwaj ASP.NET 2.0 Basics 4 October 6th, 2008 01:46 AM
Page.ClientScript.RegisterStartupScript & Threads VerbatimBOT ASP.NET 2.0 Professional 0 July 23rd, 2008 05:39 AM
Problem calling RegisterStartupScript. KalpanaT ASP.NET 1.0 and 1.1 Basics 1 January 14th, 2008 03:52 AM
RegisterStartupScript - Postback from script gp_mk General .NET 4 April 14th, 2004 05:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.