This is my javascript function ,
function returnToParent() {
var oArg = new Object();
var oWnd = GetRadWindow();
oArg.ReturnValue = "Submit";
oWnd.close(oArg);
}
And this is how I call this function on client side
<button title="Submit" runat="server" id="close" onclick="returnToParent(); return false;">
OK</button>
I want to fire this function in server side button click event .
What I've done is add new button
<asp:Button runat="server" ID="rtxtSubmitChange" OnClick="rtxtSubmitChange_Click" Text="Submit" />
and in ButtonClick Event ,
protected void rtxtSubmitChange_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(GetType(),
"MyKey",
"returnToParent();",
false);
}
But It doesn't work . What I am wrong in my code ?
Try
ScriptManager.RegisterStartupScript(this, this.GetType(), this.ClientID, "returnToParent()", true);
OR
ScriptManager.RegisterStartupScript(Page, Page.GetType(), this.ClientID, "returnToParent()", true);
For more details refer :ScriptManager.RegisterStartupScript Method
Related
I have a button inside a repeater, and this is the event:
protected void Delete_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
string id = btn.CommandArgument;
string client = "011";
try
{ //When is done
ScriptManager.RegisterStartupScript(this, GetType(), "del", "Del(" + id + ");", true);
//do it
this.Repeater.DataSource = dal.GetT(client);
this.Repeater.DataBind();
}
catch { }
}
I need to update the repeater, only after executing the javascript function Del(id)
How can this be done in the best way?
I wanted to call a javascript function from my server side but it reload my page it's normal I have to use event.preventDefault(). When I try to use event.preventDefault() it doesn't work I have an error. So how can I prevent the reload of my page for a function which is called from server side ?
talk.js
function talk(event){
event.preventDefault()
console.log("toto")
}
.ascx file
<script>
function checkUser(event){
event.preventDefault()
var answer = confirm("answer ?")
if(answer)
$("#Btn_Modify").click()
}
function modify(event){
event.preventDefault()
talk(event)
}
</script>
<button runat="server" ID="Btn_check" onclick="checkUser(event)">
Validation
</button>
<asp:Button runat="server" Visible="false" ID="Btn_Modify" OnClick="Btn_Modify_Click"/>
.ascx.cs
protected void Btn_Modify_Click(object sender, EventArgs e)
{
// SOME ACTION BEFORE CALL THE FUNCTION
Page.ClientScript.RegisterStartupScript(this.GetType(), "script", "<script type='text/javascript'>modify(event);</script>");
}
Uncaught TypeError: Cannot read property 'preventDefault' of undefined
I am not sure what my problem is because when i click the button no error in console and the javascript is not triggered.
asp
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick='<%# "show("+Eval("I") +");" %>' />
server side
public partial class _Default : Page
{
public string ClientID = "1";
public string I = "2";
public string J = "3";
protected void Page_Load(object sender, EventArgs e)
{
//Page.DataBind();
}
}
javascript
function show(str) {
//var str = "test";
console.log(str);
event.preventDefault();
alert(str);
}
I've been referring to below post, but I still couldnt figure it out... will someone help?
How to pass bind or eval arguments with the client function "OnClientClicking"
I have a LinkButton in masterpage and on click of LinkButton , I am redirecting to, say, Page1.aspx . On Page1.aspx , I have a button1. On click of that button1, I am opening new window, not affecting data of the Page1.aspx.
But when I click on LinkButton of masterpage, redirecting to Page1.aspx and from code behind,clicking button1 , Page1.aspx 's data gets changed.
How to prevent this. I am providing my code.
LinkButton on Masterpage :
<asp:LinkButton ID="lnkAppointMent" runat="server" OnClick="lnkAppointMent_Click"><span>Appointment Scheduler </span></asp:LinkButton>
click Event of LinkButton :
protected void lnkAppointMent_Click(object sender, EventArgs e)
{
Session["PhoneCenter"] = "Appointment";
Response.Redirect("PhoneMessage.aspx");
}
PageLoad of redirecting page(PhoneMessage.aspx) :
protected void Page_Load(object sender, EventArgs e)
{
fillCustomTypeMessages();
if (!Page.IsPostBack)
{
.
.
.
else if (Session["PhoneCenter"].ToString() == "Appointment")
{
btnScheduleAppointments_Click(btnScheduleAppointments, null);
}
.
.
.
Button on PhoneMessage.aspx :
<div style="float: right; padding-right: 120px">
<asp:Button ID="btnScheduleAppointments" runat="server" OnClick="btnScheduleAppointments_Click"
CssClass="button" Text="Schedule Appointments" ToolTip="Open appointment scheduler" />
</div>
RaisPostBack method on PhoneMessage.aspx :
protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
try
{
base.RaisePostBackEvent(source, eventArgument);
}
catch (Exception ex)
{
}
.
.
p.s : when I click on btnScheduleAppointment , source will be Schedule Appointments and if I click on lnkAppointMent , source will be <span>Appointment Scheduler </span> even if I am calling btnScheduleAppointment on click of lnkAppointMent.
Click event of button :
protected void btnScheduleAppointments_Click(object sender, EventArgs e)
{
if (!Permissions.checkPermissions(Session["employeeloggedin"].ToString(), "PHMSGVMD"))
{
ScriptManager.RegisterStartupScript(this, Page.GetType(), "OnLoad", "alert('You must have the Phone Messages: View and Modify permission to schedule appointments!')", true);
}
else
{
string script = String.Format("openNewWin('" + "phonescheduler.aspx" + "')");
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "openNewWin", script, true);
}
}
Script :
function openNewWin(url)
{
alert(url);
var open_link = window.open('', '_blank');
open_link.location = url;
}
Any clarification needed. Please comment.
It took me some time to simulate your problem. Have you tried to modify the LinkButton href or atributes after the Page1.aspx has been loaded?
I am assuming your Page1.aspx is your PhoneMessage.aspx, so when you click lnkAppointMent from the master page, it goes to "PhoneMessage.aspx" and execute the method, then if you click it again it will only post back instead of do what it did before, basically, you you need to redirect to "PhoneMessage.aspx" again.
So why dont you modify the part like this:
else if (Session["PhoneCenter"].ToString() == "Appointment")
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "NoExec", String.Format("document.getElementById('lnkAppointMent').href=window.location.href;"), true);
btnScheduleAppointments_Click(btnScheduleAppointments, null);
}
That would make the link redirect to the Page1.aspx again, and do the same without postback. It is not the most elegant solution, but it does the job in your case. Just make sure to have Session["PhoneCenter"] = "Appointment";
So it is not the answer to your question but I think is a solution to your problem.
It worked for me, I hope It helps! Let me know what you think.
I have set dropdown enable set to false in one button click and i will set enable="true" is not working in page load
here is my aspx
<asp:DropDownList ID="ddlJournal" runat="server" OnSelectedIndexChanged="ddlJournal_SelectionChanged" AutoPostBack="true" CssClass="drop" />
Here is my click event:
protected void btnTemplate_click(object sender, EventArgs e)
{
check.Value = "1";
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "Load_functions()", true);
//txtAddJournal.Attributes.Add("Style", "display:block");
//btnUpload.Attributes.Add("Style", "display:block");
//if (fileuploader.HasFile)
//{
try
{
string Filename = Path.GetFileName(fileuploader.FileName);
//fileuploader.SaveAs(Server.MapPath("~/") + Filename);
// fileuploader.SaveAs(Server.MapPath("D:\\Req Sep16\\") + Filename);
OleDbConnection myconnectionini = default(OleDbConnection);
OleDbDataAdapter mycommandini = default(OleDbDataAdapter);
//if (fileuploader.PostedFile.FileName.EndsWith(".xls") == false & fileuploader.PostedFile.FileName.EndsWith(".xlsx") == false)
//{
// // lbl_Error.Text = "Upload only excel format";
// Response.Write(#"<script language='javascript'>alert('Upload only excel format');</script>");
// return;
//}
//else
//{
gvDetails.DataSource = null;
string pathToSave = HttpContext.Current.Server.MapPath("~/UploadFiles/") + "Copy of Database_HBM";
//fileuploader.PostedFile.SaveAs(pathToSave);
//strFilePath = "D:\\Files\\" + fileuploader.FileName;
string constrini = "provider=Microsoft.Jet.OLEDB.4.0;data source=" + pathToSave + ";Extended Properties=Excel 8.0;";
DataSet ds = new DataSet();
// DataTable dt = new DataTable();
myconnectionini = new OleDbConnection(constrini);
mycommandini = new OleDbDataAdapter("select * from [Sheet1$]", myconnectionini);
ds = new DataSet();
mycommandini.Fill(ds);
gvDetails.DataSource = ds.Tables[0];
gvDetails.DataBind();
ddlJournal.SelectedIndex = -1;
ddlJournal.Enabled = false;
//ddlJournal.Attributes.Add("disabled", "disabled");
//}
}
catch (Exception ex)
{
string msg = ex.Message;
}
//}
}
And my page load event is
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Grid", "headerLock();", true);
// ScriptManager.RegisterStartupScript(Page, this.GetType(), "Key", "<script>headerLock();</script>", true );
if (!IsPostBack)
{
Bindddl();
BindGrid(null);
ddlJournal.Enabled = true;
}
else
{
ddlJournal.Enabled = true;
}
}
button :
<asp:Button ID="btnUpload" runat="server" Text="Template 1" OnClientClick="return Validate();"
OnClick="btnTemplate_click" CssClass="btn" />
but still my dropdown list is disable.
suggest me get a solution
thanks in advance
You can set dropdown list Enabled false from its control only like this
<asp:DropDownList ID="ddlJournal" runat="server" OnSelectedIndexChanged="ddlJournal_SelectionChanged" AutoPostBack="true" CssClass="drop" Enabled="false"/>
And the rest code should work fine.
Please mark it helps
Understand that your if-else condition in Page_Load() method is the main culprit. You're always setting ddlJournal.Enabled = true, no matter what. Seems like you didn't properly understand the concept of IsPostBack. ddlJournal is supposed to be disabled when IsPostBack is true, because that's what you want. Otherwise, it's supposed be enabled.
This is a very concise explanation about what IsPostBack is:
Postback in an event that is triggered when a action is performed by a contol on a asp.net page. for eg. when you click on a button the data on the page is posted back to the server for processing.IsPostback is normally used on page _load event to detect if the page is getting generated due to postback requested by a control on the page or if the page is getting loaded for the first time.
[a comment from http://forums.asp.net/t/1115866.aspx?What+is+IsPostBack ]
So based on that, you should change your code like the following:
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Grid", "headerLock();", true);
if (!IsPostBack)
{
//When IsPostBack is false, ddlJournal should be enabled
Bindddl();
BindGrid(null);
ddlJournal.Enabled = true;
}
else
{
//Else, IsPostBack is true, so, ddlJournal should be disabled
ddlJournal.Enabled = false;
}
}
Also, you don't need this in your btnTemplate_click() method since you're doing this on page load:
ddlJournal.Enabled = false;