Why IE cannot close tab on "Cancel" button click c# - javascript

I have this cancel button:
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Cancel" />
I want click that button it will close the current window, so in codebehind:
protected void Button1_Click(object sender, EventArgs e)
{
string jScript = "<script>window.close();</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "keyClientBlock", jScript);
// Response.Write("<script>parent.close_window();</script>");
}
But I do not see that window tab close in my IE browser. Is there anything can be corrected here?

Why are you doing this server side when you can just attach to the buttons click event in js/jQuery?
This probably wont work due to a browser security setting unless the window itself was spawned via the window.open() method, so you'll want to use a workaround if this is absolutely necessary.
<script type="text/javascript">
$('#button').click(function(e){
e.preventDefault();
window.open('','_self').close();
});
</script>
<button id="button" value="close" />

function CloseWindow() {
window.open('','_self').close();
}
Code Behind
protected void Button1_Click(object sender, EventArgs e)
{
string jScript = "<script>CloseWindow();</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "keyClientBlock", jScript);
}
I would also suggest to create a function in javascript and then just call it with your code, and if your not going to do anything in the .cs side us DGibs answer.

Related

Onclick event on html button

I have a simple button
<input type="submit" value="Global" onclick="Global_Click" />
that when clicked should search into a db
and here is the event that it should call:
protected void Global_Click(object sender, EventArgs e)
{
FillUsers("global");
}
FillUsers is the actual method that searches id DB.
When I click the button a JavaScript error is displayed: Global_Click is not defined!
Why is that if I am not using JavaScript? And how can I call the Global_Click from the button?
You miss the runat="server" attribute.
In your code,in that way,it is called javascript handler.

Unable to make value fields editable after clicking on a link button

I've created a link button and made a event handler for it. But still unable to make the values editable.
This is the link button:
<FooterTemplate>
<asp:LinkButton ID="lbEdit" runat="server" OnClick="lbEdit_Click">Edit</asp:LinkButton>
</FooterTemplate>
Here is the code behind:
protected void lbEdit_Click(object sender, EventArgs e)
{
dvEAEdit.DefaultMode = DetailsViewMode.Edit;
}
Any help is appreciated!

Call JavaScript function (in aspx) on aspx.cs using a button

I have this aspx:
<body>
<div>
<script type="text/javascript">
function NewPage() {
document.location.href = "http://www.nextservice.pt/"
}
</script>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Btn2" runat="server" Text="OK" onclick="Button2_Click" />
CODE1: <asp:Label ID="Label1" runat="server" Text="Label" ForeColor="#CC0000" />
</form>
</div>
</body>
and I'm working with web forms, and I wont call this button on aspx.cs
public partial class SITE_TESTER : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click (object sender, EventArgs e)
{
string code = TextBox1.Text.ToString();
if (!verifyCode(code)) // comparing users from table
{
Label1.Text = "Not Exists"; //for invalid code
}
else
{
Label1.Text = "Exist"; //for sucsseful code
/*
I Wont call my JavaScript Function here!!!!
*/
}
}
}
you can call a javascript method from server side in asp.net by following ways:
protected void button_Click(object sender , EventArgs e)
{
string jsMethodName= = "NewPage()";
ScriptManager.RegisterClientScriptBlock(this, typeof(string), "uniqueKey", jsMethodName, true);
//or
//ScriptManager.RegisterStartupScript(this, GetType(), "NewPage()", false);
}
you can use either ScriptManager.RegisterStartupScript or ScriptManager.RegisterClientScriptBlock
so difference between the two is explained below:
Let's say we have a .aspx page with the following form tag : (Line
nos. are for reference)
1. <form id="Form1" runat="server">
2. ..
3. ..
4. ..
5. </form>
Now let's look at key differences for each method :
A.
Page.RegisterClientScriptBlock() will insert the block of script
before Line 2.
Page.RegisterStartupScript() will insert the script after Line 4.
B.
Page.RegisterClientScriptBlock() should usually be used for scripts
encapsulated in functions. (hence the word "block")
Page.RegisterStartupScript() can be used for any script, even if it's
not in a function.
C.
Page.RegisterClientScriptBlock() should be used for functions that
don't need to run on Page load.
Page.RegisterStartupScript() should be used for scripts that must run
on Page Load.
D.
Page.RegisterClientScriptBlock() should be used for a script that does
not require the form elements to have been created.
Page.RegisterStartupScript() should be used for scripts that require
the form elements to have been created and uses references to them.
Notice that all the 4 differences are essentially related to each
other (they build upon the prev. one). The difference put in one line
can sometimes be too subtle.
you can know more about these from here and here
You can add a script which will be executed when page is loaded to browser:
Page.RegisterStartupScript("unique_key", "<script type=\"text/javascript\">NewPage()</script>"); // but this is deprecated function
or like this:
ClientScript.RegisterClientScriptBlock(this.GetType(), "unique_key", "NewPage()", true);
But if you simply want to do a redirect (as I can see from your NewPage function), you can do:
Response.Redirect("http://www.example.com");

click() Event in javascript with a PARAM?

I'm using the event click() to call a method in Code Behind, like this:
HTML
<asp:button bordercolor="White" id="btnAddGS" onclick="AddGSBandeira" runat="server">
JAVASCRIPT
$("#ctl00_ContentPlaceHolder1_btnAddGS").click();
C#
public void AddGSBandeira(object sender, EventArgs e)
{
}
Its work normally, but I need to pass a param in the javascript call, like this:
$("#ctl00_ContentPlaceHolder1_btnAddGS").click("param");
But I do not know how this works ...can anybody help?
The best thing to do is create a hidden control and populate it's value with JavasScript on the click event. Your code behind will be able to access that value on your postback (AJAX or otherwise).
Markup
<asp:HiddenField ID="myHiddenField" runat="server" />
<asp:button bordercolor="White" id="btnAddGS"
onclick="AddGSBandeira"
onclientclick="SetHiddenValue()" runat="server">
JavaScript
function SetHiddenValue()
{
document.getElementById("<%=myHiddenField.ClientID%>").value = "[Your value here]";
}
C#
public void AddGSBandeira(object sender, EventArgs e){}
{
var jsVal = myHiddenField.Value;
}
You can do this with trigger.
http://api.jquery.com/trigger/
$("#ctl00_ContentPlaceHolder1_btnAddGS").trigger("click",["param"]);
I believe the 2nd parameter to trigger should be an array of arguments to pass to the function.

Why can't I hit the event handler in the code behind doing a modal popup postback

I am basically trying to do what this article says.
link text I am able to postback, but my handler is not getting hit. Any ideas?
Code Behind
protected void Page_Init(object sender, EventArgs e)
{
WireEvents();
}
private void WireEvents()
{
btnAuthOk.Click += new EventHandler(btnAuthOk_Click);
btnAuthOk.OnClientClick = string.Format("fnClickOK('{0}','{1}')", btnAuthOk.ClientID, string.Empty);
}
private void btnAuthOk_Click(object sender, EventArgs e)
{
DoSomeCodeHere();
}
Javascript & HTML
function fnClickOK(sender, e) {
__doPostBack(sender, e);
}
<p>To allow this payment to be processed, enter an authorized User ID and Password</p>
<p>User ID: <asp:TextBox runat="server" ID="txtAuthUser" CssClass="underlinedTextBox" Columns="8" />
<asp:Literal runat="server" ID="spauth" Text=" " />
Password : <asp:TextBox runat="server" ID="txtAuthPass" TextMode="Password" CssClass="underlinedTextBox" Columns="10" />
</p>
<asp:Button runat="server" ID="btnAuthOk" Text="Submit" CssClass="popupAuthButton" UseSubmitBehavior="false" />
</asp:Panel>
<cc1:ModalPopupExtender ID="authPE" runat="server" PopupControlID="popupAuth"
OkControlID="btnAuthOk" TargetControlID="hdnPopupTarget" BackgroundCssClass="modalBackground" />
Thanks for any help here...
Cheers,
~ck
PS Stack is mucking my html a bit as I don't know how to properly post HTML. Is someone can edit and fix, I appreciate it. :)
I found that setting UseSubmitBehavior="false" actually allows me to hit my code.
If you just want your OK button to do a postback, simply remove the OkControlID attribute from your ModalPopupExtender markup.
I know, I know - that sounds ridiculous. But when you specify OK and Cancel button ID's to the MPE, it actually disables them from posting back on the client side.

Categories