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();
}
}
}