Getting the input of a confim box to C# variable in VS2005 - c#

How to get input from user using a confirm box in asp.net and store the input in a C# variable.
as whether it is a ok or cancel button. pls help me forward.
For Example i am using like this,
Response.Write("confirm('Testing FiileAlready existing Replace it?')");
instead i need the output of the confirm box in c# variable...

Try this:
Your javascript function:
<script type="text/javascript" language="javascript">
function ConfirmOnReplace() {
if (confirm("Are you sure?") == true)
return true;
else
return false;
}
</script>
The ASP.NET button:
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return ConfirmOnReplace();" />
And the code-behind(YourPage.aspx.cs) file:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button1.Click += new EventHandler(Button1_Click);
}
void Button1_Click(object sender, EventArgs e)
{
// If you are here, your javascript method returned true
// TODO: Replace the file, or do what you want
}
}

Related

set the asp:textbox val() in jQuery and get the Text of the same on the server side code

I have successfully set the text of asp:textbox using jQuery val() function, now I want the same value of the textbox on click of asp:button on the server side code.
$("#textboxId").val('some text');
protected void button_Click(object sender, EventArgs e)
{
// getTheText is blank
string getTheText = textboxId.Text.Trim();
}
<script type="text/javascript">
$(document).ready(function () {
$('#<%= TextBox1.ClientID %>').val("my value");
});
</script>
and in code behind on button click use
protected void Button1_Click(object sender, EventArgs e)
{
var value = TextBox1.Text;
}
this will work. it work for me i test it.
I also had the same problem and finally found a solution.
string getheText =Page.Request.Form["textboxId"].ToString().Trim();
But be careful if you use "Content" in master page the id must be like that
string gettheText = Page.Request.Form["ctl00$ContentPlaceHolder1$textboxId"].ToString().Trim();
if your textbox is an aspx server control then you can directly set Text by using
textboxId.Text = "Some Value";

code behind confirm with custom validation

i understand that we able to achieve this by using "onClientClick", but i want to check the validation first BEFORE the confirmation box.
javascript
function showConfirm() {
var result = window.confirm('Are you sure?');
if (result == true)
alert("ok");
}
html
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
C#
protected void Button1_Click(object sender, EventArgs e)
{
if(checkValidation() == true)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "scr", "javascript:showConfirm();", true);
//if(result == true) //how to get the result value?
//{
////run some code
//insert data into sql
//}
}
}
is there anyway i can get the confirmation result at code behind? without the if-else-statement, the data will insert into sql before user choose their decision.
i create a button with display:none
.hideButton{
display:none;
}
create code behind
protected void btnConfirm_Click(object sender, EventArgs e)
{
Response.Redirect("page2.aspx");
}
trigger the btnConfirm click if user click Yes on confirmation box
$('#ContentPlaceHolder1_btnConfirm').trigger('click');
If you are doing a custom validation in Web forms that you much enable EnableClientScript="true" and write the JS validate function name in ClientValidationFunction="JSValidateFunctionName" like this you would be able to validate on client side before even going to server side.

How to close rad window in c# on button Click using JS

i have tried to close window(radWindow) using asp.net C#, but error on java script function is that 'object required' ,How can i solve this?
My java script and asp.net code is
<script type="text/JavaScript">
function Close() {
GetRadWindow().Close();
}
</script>
Asp.net code is
<asp:Button ID="btnSubmit" runat="server" Text="Submit Request" Height="27px" OnClick= "btnSubmit_Click" OnClientClick="Close();return false;" />
Optional try using C# for same operation as
C# code for button is
protected void btnSubmit_Click(object sender, EventArgs e)
{
//Server code
ScriptManager.RegisterStartupScript(this,GetType(), "close", "Close();", true);
}
You call GetWindow() but you don't have this function .
Just add this code ,
function GetWindow()
{
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow;
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
return oWindow;
}
This post may help you lot
http://www.telerik.com/community/forums/aspnet-ajax/window/getradwindow-close-cause-unspecified-error.aspx
i had same issues in the past but manged by the logic from the above post

Opening URL using an ImageButton

using asp.net | C#
I want my ImageButton to open a URL when I click it. I finally have my Image loading and it clicks but when I click it nothing happens. Here is the code I have so far:
aspx page
<asp:ImageButton ID="Button1" runat="server" ImageUrl="~/images/button.gif"
onclick="Open_Click"></asp:ImageButton>
aspx.cs page
protected void Open_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
try
{
System.Diagnostics.Process.Start("http://www.website.com");
}
catch { }
}
You want to do a redirect, not start a process. Try this:
protected void Open_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
try
{
Response.Redirect("http://www.website.com");
}
catch { }
}
Additionally, you could just set the PostBackUrl attribute on the control and have no need for a server side event.
You can do it on the client-side:
This will open in another window:
<asp:ImageButton OnClientClick="window.open('/xxx/xxx.aspx');
OR this will open in same window, javascript needs to return false so server code won't run:
<script>
function ReDirect() {
location.href = '/xxx/xxx.aspx';
return false;
}
</script>
asp:ImageButton OnClientClick="javascript:return(ReDirect());" />

Browser not handling exception from AJAX panel, ASP.NET c#

i am having trouble catching errors in an AJAX panel. Even when i throw an exception in the c# code behind the front end completely ignores it.
Here is the code i have setup, can anyone see why?
I ideally want to show a js alert window on error.
Code Behind:
protected void btnX_Click(object sender, EventArgs e)
{
throw new ApplicationException("test");
}
protected void ScriptManager_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
ScriptManager.AsyncPostBackErrorMessage = e.Exception.Message;
}
Markup:
<script type="text/javascript" language="javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, e)
{
window.alert(e.get_error().name);
}
</script>
<asp:ScriptManager ID="ScriptManager" runat="server" AllowCustomErrorsRedirect="true" OnAsyncPostBackError="ScriptManager_AsyncPostBackError" />
I think you should be displaying the 'message' error property and not 'name'
see http://msdn.microsoft.com/en-us/library/bb383810.aspx
function EndRequestHandler(sender, e)
{
if (e.get_error() != undefined)
{
var errorMessage = e.get_error().message;
e.set_errorHandled(true);
alert(errorMessage)
}
}

Categories

Resources