C# Confirm message Handler in CodeBehind - c#

I need to open a dialog when I click a button so I here is what I have done:
Markup:
<ext:Button runat="server" ID="Button1" Text="Save New Information" OnDirectClick="Button1_DirectClick"/>
CodeBehind:
namespace Admin.Modules
{
public partial class UsersGrid : System.Web.UI.UserControl
{
protected void Button1_DirectClick(object sender, DirectEventArgs e)
{
X.Msg.Confirm("Confirm", "Do you want to update this information also?", new MessageBoxButtonsConfig
{
Yes = new MessageBoxButtonConfig
{
Handler = "App.Direct.DoYes()",
Text = "Yes"
},
No = new MessageBoxButtonConfig
{
Handler = "WndwEdit.close()",
Text = "No"
}
}).Show();
}
[DirectMethod]
public void DoYes()
{
X.MessageBox.Info("Error NOT", "Something went lalala", AnchorPoint.LeftTop, UI.Danger).Show();
}
}
}
The message box is displayed correctly but it doesnt call the DoYes function with error:
Uncaught TypeError: Cannot read property 'DoYes' of undefined

You need to use App.direct.DoYes not App.Direct.DoYes.
<%# Page Language="C#" %>
<script runat="server">
protected void Button1_DirectClick(object sender, DirectEventArgs e)
{
X.Msg.Confirm("Confirm", "Do you want to update this information also?", new MessageBoxButtonsConfig
{
Yes = new MessageBoxButtonConfig
{
Handler = "App.direct.DoYes()",
Text = "Yes"
},
No = new MessageBoxButtonConfig
{
Handler = "WndwEdit.close()",
Text = "No"
}
}).Show();
}
[DirectMethod]
public void DoYes()
{
X.MessageBox.Info("Error NOT", "Something went lalala", AnchorPoint.LeftTop, UI.Danger).Show();
}
</script>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Ext.NET Example</title>
<link type="text/css" rel="stylesheet" href="http://speed.ext.net/www/intro/css/main.css" />
</head>
<body>
<ext:ResourceManager runat="server" Theme="Triton" />
<ext:Button runat="server" ID="Button1" Text="Save New Information" OnDirectClick="Button1_DirectClick"/>
</body>
</html>

Related

C# Label doesnt update on timer

I have a function on my web page to which I pass a message. The function changes a label to to the message but after 3 seconds I want the label to hide or have assigned a "" value. In the console, the text value changes but in the web page
it remains the same.
private void notify(string msg)
{
notification.Text = msg;
System.Threading.Timer timer = null;
timer = new System.Threading.Timer((obj) =>
{
bar();
timer.Dispose();
},
null, 3000, System.Threading.Timeout.Infinite);
}
private void bar()
{
notification.Text="";
System.Diagnostics.Debug.WriteLine(notification.Text); //Output ""
}
What about it,
You will have to use setInterval(func,3000) on client side using javascript.In such a sense when Notify will be called then after 3000 seconds on client side hide to the Label.Use following code
Design File(.aspx)
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
function Hide()
{
document.getElementById("notification").hidden = true;
}
setInterval(Hide, 3000);
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="notification" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Code File(.cs) when it's calling notify method on server side.I'm calling over the Page_Load,It will be your own choice.
protected void Page_Load(object sender, EventArgs e)
{
notify("ok");
}
private void notify(string msg)
{
notification.Text = msg;
}

ASP.NET OnTextChanged method

I have ASP.NET website, there is a page when user have to enter text in textbox and image should appear depends the text that is entered. Everything is working but image appears only when you press Enter, is there a way image to appear as you entering the letters not by pressing Enter?
<asp:TextBox ID="initials" runat="server" Width="50px" OnTextChanged="initials_TextChanged" AutoPostBack="true"></asp:TextBox>
Code behind:
protected void initials_TextChanged(object sender, EventArgs e)
{
if(this.initials.Text == "A") { prvwleft.ImageUrl = "~/Images/left/A1.jpg"; }
}
In asp.net, OnTextChanged event fires when you leave the focus.
In your case, you should go for KeyDown event.
Asp.net Textbox doesn't have server side KeyDown event, so we will have to do it using jquery:
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.3.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$('#initials').keypress(function () {
if ($(this).val() == "A") {
$('#prvwleft').ImageUrl = "~/Images/left/A1.jpg";
}
else {
$('#prvwleft').ImageUrl = "~/Images/left/A1.jpg";
}
})
});
</script>
You need to call onkeypress event in javascript like this
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function tSpeedValue(txt)
{
alert("hi");
var at = txt.value;
alert(at);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" onkeypress="tSpeedValue(this)"></asp:TextBox>
</div>
</form>
</body>
</html>
and if you want to call it in server side
<asp:TextBox ID="TextBox2" runat="server" onkeypress="__doPostBack(this.name,'OnKeyPress');" ></asp:TextBox>
and in .cs page the code should be like
protected void Page_Load(object sender, EventArgs e)
{
var ctrlName = Request.Params[Page.postEventSourceID];
var args = Request.Params[Page.postEventArgumentID];
if (ctrlName == TextBox2.UniqueID && args == "OnKeyPress")
{
TextBox2_OnKeyPress(ctrlName, args);
}
}
private void TextBox2_OnKeyPress(string ctrlName, string args)
{
//your code goes here
}

Access the Label value on Page load in c# when value set through jQuery

I am posting this question again, maybe this time more accurate description.
The problem is , I am using jQuery to set the Label's text value and it works fine on browser, but when I want to save it to string, it does not save it. Here is the
front End Html Code.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(window).load(function () {
var myNewName = "Ronaldo";
$('#<%= Label1.ClientID %>').text(myNewName);
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
And here is the Back End C# Code On Page Load
using System;
using System.Web.UI;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
string mynameCheck = Label1.Text;
if (mynameCheck=="Ronaldo")
{
Response.Write("Yes Name is Fine");
}
else
{
Response.Write("Name's not Fine");
}
}
}
The result displayed is
Name's not Fine
Ronaldo
Seems like the string is still Null. Is there any problem of rendering??
label is not input type so you can not get changed values through jquery on server side. You can use hidden field for this purpose.
Your server side code (c#) can not access the form data until your client side code (HTML/Javascript) posts it.
Why do you want to the name already at the PageLoad event?
You could add a asp:Button with an attached onClick event handler to read the value of your asp:Label.
Labels do not maintain viewstate. The server will not post that information back to the server. You can try explicitly enabling the ViewState on your Label, but if that doesn't work, you will have to store that value in a hidden field.
First Call Page Load event and after that call JQuery Window.Load event.
So if you want to set any content in Label then you can do using onClientClick of button.
For ex.
<asp:Button ID="btn" runat="server" Text="Click me" OnClientClick="SetClientValues();" />
<script type="text/javascript">
function SetClientValues() {
var myNewName = "Ronaldo";
$('#<%= Label1.ClientID %>').text(myNewName);
}
</script>
At server side button event you can get Label values that sets at client side.
protected void btn_Click(object sender, EventArgs e)
{
string mynameCheck = Label1.Text;
if (mynameCheck=="Ronaldo")
{
Response.Write("Yes Name is Fine");
}
else
{
Response.Write("Name's not Fine");
}
}
It will print Yes Name is Fine
This should do it:
<script type="text/javascript">
$(window).load(function () {
if($('#<%= Txt1.ClientID %>').val() != "Ronaldo"){
var myNewName = "Ronaldo";
$('#<%= Txt1.ClientID %>').val(myNewName);
$('#<%= Label1.ClientID %>').text(myNewName);
$('#<%= Btn1.ClientID %>').click();
}
});
</script>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="Txt1" runat="server" style="display:none"></asp:Label>
<asp:Button ID="Btn1" runat="server" style="display:none" Click="Btn1_Click"></asp:Label>
</form>
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
Label1.Text=Txt1.Text;
string mynameCheck = Label1.Text;
if (mynameCheck=="Ronaldo")
{
Response.Write("Yes Name is Fine");
}
else
{
Response.Write("Name's not Fine");
}
}
}
protected void Btn1_Click(object sender, EventArgs e)
{ }
Hope it helps :)

Javascript is not working if I add a second textbox

please help me, I'm trying to click a hidden button if the user press the ENTER key inside a text box. It works fine with one text box, but if I add another, java script don't work.
TextBox. ------------------------------------------------------------------------------------------
<pre>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click1" style="visibility: hidden; display: none;" />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<script type="text/javascript">
var myInput = document.getElementById("TextBox1");
if (myInput.addEventListener) {
myInput.addEventListener('keydown', this.keyHandler, false);
} else if (myInput.attachEvent) {
myInput.attachEvent('onkeydown', this.keyHandler); /* damn IE hack */
}
function keyHandler(e) {
var EnterKEY = 13;
if (e.keyCode == EnterKEY) {
if (e.preventDefault) {
document.getElementByID("Button1").click();
e.preventDefault();
}
return false;
}
}
</script>
<br />
<asp:Label ID="Label1" runat="server" Visible="False"></asp:Label>
</form>
</body>
</pre>
------------------------------------------------------------------------------
CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
TextBox2.Text = "It Works";
}
}
Try wrapping your javascript code inside of a document.onload function
window.document.onload = function(e){
var myInput = document.getElementById("TextBox1");
if (myInput.addEventListener) {
myInput.addEventListener('keydown', this.keyHandler, false);
} else if (myInput.attachEvent) {
myInput.attachEvent('onkeydown', this.keyHandler); /* damn IE hack */
}
//adding the second textbox
myInput = document.getElementById("TextBox2");
if (myInput.addEventListener) {
myInput.addEventListener('keydown', this.keyHandler, false);
} else if (myInput.attachEvent) {
myInput.attachEvent('onkeydown', this.keyHandler); /* damn IE hack */
}
function keyHandler(e) {
var EnterKEY = 13;
if (e.keyCode == EnterKEY) {
if (e.preventDefault) {
document.getElementByID("Button1").click();
e.preventDefault();
}
return false;
}
}
}
so your script is executed once the DOM tree is fully loaded
Looking at your code you only bind your keyhandler(e) function to the element TextBox1 but not on TextBox2. Try creating a new binding code also for your TextBox2.
var myInput2 = document.getElementById("TextBox2");
if (myInput2.addEventListener) {
myInput2.addEventListener('keydown', this.keyHandler, false);
} else if (myInput.attachEvent) {
myInput2.attachEvent('onkeydown', this.keyHandler); /* IE hack */
}

Cannot Unregister UpdatePanel exception with Cross Page Posting

I am getting the dreaded exception "Cannot unregister UpdatePanel with ID 'UpdatePanel' since it was not registered with the ScriptManager" when trying to combine a popup window and some Cross-Page Posting. I've been able to reproduce the problem with a small example.
I have seen other questions that handle the Unload event for the UpdatePanel, but that doesn't seem to work with the addition of the Cross Page Posting.
How do I get around this issue?
To duplicate the issue with the code below:
Start the Setup page
Press the View Popup link
Close the popup
Press the Next button to transfer to the Target page
Press the Back button to transfer to the Setup page
Press the View link and close the popup
Press the Next button, and see the failure
Here are the highlights (forgive the length, but it should be cut-and-pastable):
Setup.aspx
<head runat="server">
<title>Setup </title>
<script type="text/javascript">
function openCenteredWindow(url, height, width, name, parms) {
//Snip setting up window location stuff
var win = window.open(url, name, winParms);
return win;
}
</script>
</head>
<body>
<h1>Setup Page</h1>
<form id="aspnetForm" runat="server">
<div>
<asp:TextBox ID="txtData" runat="server" Width="80%" Text="<%# Information %>" />
<br />
<asp:LinkButton ID="lbViewData" runat="server"
OnClientClick="aspnetForm.target='ViewDataPopup';"
Text="View In Popup" /> <br />
<asp:Button ID="btnNext" runat="server" Text="Next" OnClick="btnNext_Click" />
</div>
<div>
<br />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:updatepanel ID="UpdatePanel" runat="server" OnUnload="UpdatePanel_Unload"></asp:updatepanel>
</div>
</form>
</body>
Setup.Aspx.cs
public partial class Setup : System.Web.UI.Page
{
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (this.ViewState["Information"] != null)
_Information = this.ViewState["Information"].ToString();
}
protected override object SaveViewState()
{
this.ViewState["Information"] = _Information;
return base.SaveViewState();
}
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null && PreviousPage is TargetPage)
{
_Information = ((TargetPage)PreviousPage).SetupData;
}
else if (!Page.IsPostBack)
{
_Information = String.Format("This test started at {0}", DateTime.Now);
}
Page.DataBind();
lbViewData.PostBackUrl = "ViewData.aspx?u=0";
lbViewData.Attributes.Add("onclick", "JavaScript:openCenteredWindow(\"ViewData.aspx?u=0\", 400, 300, \"ViewDataPopup\", 'scrollbars=yes');");
}
private string _Information;
public string Information
{
get { return _Information; }
}
protected void btnNext_Click(object sender, EventArgs e)
{
HttpContext.Current.Server.Transfer("~/TargetPage.aspx");
}
}
ViewData.aspx
<%# PreviousPageType VirtualPath="~/Setup.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
<title>View Data</title>
<script type="text/javascript">
function fixForm() {
opener.document.getElementById("aspnetForm").target = "";
opener.document.getElementById("aspnetForm").action = "";
}
</script>
</head>
<body onload="fixForm()">
<form id="aspnetForm" runat="server">
<div>
<h2 >View Data</h2>
</div>
<asp:Label ID="lblData" runat="server" Text="<%# SetupData %>" />
</form>
</body>
ViewData.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
Setup setup = (Setup)PreviousPage;
this.SetupData = setup.Information ?? "Data not set in Setup Page";
}
Page.DataBind();
}
private string _setupData = "Did not get updated data";
protected string SetupData
{
get { return _setupData; }
set { _setupData = value; }
}
TargetPage.aspx
<%# PreviousPageType VirtualPath="~/Setup.aspx" %>
<body style="background-color: #9999BB">
<h1>Target Page</h1>
<form id="aspnetForm" runat="server">
<div>
<asp:Label ID="lblData" runat="server" Text="<%# SetupData %>" /><br />
<asp:Button ID="btnBack" runat="server" Text="Back" OnClick="btnBack_Click" />
</div>
</form>
</body>
TargetPage.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (this.PreviousPage == null)
Response.Redirect("Setup.aspx");
this.SetupData = PreviousPage.Information;
}
Page.DataBind();
}
public string SetupData
{
get { return ViewState["SetupData"].ToString(); }
set { ViewState["SetupData"] = value; }
}
protected void btnBack_Click(object sender, EventArgs e)
{
HttpContext.Current.Server.Transfer("~/Setup.aspx");
}
As you can see, it's enough to have the UpdatePanel just sitting on the page and doing nothing for this error to occur.

Categories

Resources