Using a Resource File on buttons OnClientClick - c#

I would like to use a resource file to display the dialog box which is displayed on client click, i have tried the below however I am getting the result in the screen shot:
<asp:Button ID="editview" runat="server" Text="<%$ Resources:btnEditViewText%>" CssClass="buttonlink"
OnClientClick="return confirm('<%$ Resources:btnEditViewText%>');"
CommandName="editview"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Id")%>'/>
I have seen a few people suggest setting this in the code behind however this control is wrapped in a repeater and I can only get a handle on it once the ItemCommand event is triggered which requires the confirmation dialogue to have been confirmed.
This feels like a bit of a catch 22 and im out of ideas, any suggestions guys?

Thanks Ben, this worked
protected void rptVessels_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Button button = (Button)e.Item.FindControl("editview");
button.OnClientClick = String.Format("return confirm('{0}')", GetLocalResourceObject("alertEditVesselText").ToString());
}

This is my code and it works! But I also prefer code behind solution especially for single controls.
In my project, i use this in nested repeaters so I do not need to use OnItemDataBound method. Its so easy...
OnClientClick='<%#String.Concat("if ( !confirm(\u0027", Resources.Resource.AreYouSureToDelete, "\u0027)) return false;") %>'

I found the best solution
<script type="text/javascript">
function DeleteItem() {
if (confirm(document.getElementById('<%=hdelete.ClientID%>').value)) {
return true;
}
return false;
}
</script>
<asp:HiddenField runat="server" ID="hdelete" Value="<%$Resources:Resources, Confiemdelete %>" />
<asp:Button ID="btnmergewithname" runat="server" Text="<%$Resources:Resources, Delete %>" OnClientClick="return DeleteItem()"/>

Related

Full postback on select & remove within update panel

I'm having issues with the asp.net textbox within an update panel. It works perfectly fine when adding or removing each individual character but if I highlight all of the text within the textbox, and then remove it a full postback occurs, not a partial postback which is expected.
Why is this happening? I haven't found anything related to this particular problem so it's likely I'm doing something wrong.
Example aspx:
<asp:UpdatePanel ID="updExample" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:Repeater ID="rptExample" runat="server" .... >
<ItemTemplate>
<asp:TextBox ID="txtExample" runat="server" ClientIDMode="static" Text='<%# Eval("Example") %>' OnTextChanged="txtExample_TextChanged" AutoPostBack="true"></asp:TextBox>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
Example TextChanged Event:
protected void txtExample_TextChanged(object sender, EventArgs e)
{
updExample.Update();
}
Additional Notes:
Switching UpdateMode to 'Always' doesn't work.
Karthikeyan Nagaraj pointed out in the comments to try adding triggers alongside what I already had. I did in fact already have this, however, I was assigning the trigger in the ItemDataBound event which I realized was incorrect after reinvestigating. The ItemCreated event was much more suited.
I had no issues finding the control in the ItemCreated event, however adding a new async postback trigger to the update panel gave me grief and said the control couldn't be found when changing the text. To resolve this, I used the script managers RegisterAsyncPostBackControl(); method as shown below.
protected void rptExample_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var input = e.item.FindControl("txtExample");
if (input != null) {
ScriptManager sm = ScriptManager.GetCurrent(this);
sm.RegisterAsyncPostBackControl(input);
}
}

How to implement a click event on textbox in ASP.NET?

In my web application I need a functionality so that when users click on textbox to input values, it should make the button and the other fields visible?
I am using the code provided below but, could not get it working.
C#:
protected void TextBox1_Click(object sender, EventArgs e)
{
ButtonSearch.Visible = true;
}
ASP.NET:
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" OnClick="TextBox1_Click"></asp:TextBox>
<asp:Button ID="ButtonSearch" runat="server" OnClick="ButtonSearch_Click" Text="Search" Visible="False" />
How to accomplish this?
Set AutoPostback="True". This way the event will be fired server-side, and the button will become visible.
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" OnClick="TextBox1_Click" AutoPostBack="true"></asp:TextBox>
However, if you only want to toogle visility of a button, you really should considerate javascript. This will save a trip back to the server.
<asp:TextBox onclick="txtBox1_ClientClicked()" ID="TextBox1" runat="server" OnClick="TextBox1_Click"></asp:TextBox>
<asp:Button ID="ButtonSearch" runat="server" OnClick="ButtonSearch_Click" Text="Search" style="display:none;" />
<script type="text/javascript">
function txtBox1_ClientClicked(){
var theButton = document.getElementById('<%=ButtonSearch.ClientID%>');
theButton.style.display = 'block';
}
</script>
You do not need to post back to the server to accomplish your job. You can use client side onFocus event and javascript/jquery, for example.
I know I used an input of type text, and you are using an ASP Control which posts on server, but here is a JSFiddle to get you on the right track: http://jsfiddle.net/Mmjtz/1/
$("<%= ButtonSearch.ClientID %>").click(function(){
$("#TextBox1").show():
});
In this code you need to pass fields ID which you want to visible on the click of button.
Put the textbox inside a div and use the div's onClick event from codebehind. It's not what you asked but it works for me without any errors. Here is a javascript function to implement requested event:
function toggleVisibility()
{
document.getElementById('TextBox1').disabled = true;
/*
...some other code...
*/
}
And of course, you have to define your onclick event at the div definition after implementing this JS function.
<div id="TBdiv" onClick="toggleVisibility()">
<asp:TextBox ID="TextBox1"..../>
</div>
IMPORTANT: Since you now disabled your TextBox from codebehind, you have to enable it in somewhere before you want to use it again. Otherwise you will not see it while the page is running.
jQuery is the perfect solution for your problem. The code would be something like this:
$("#TextBox1").on("click",function(){$("#ButtonSearch").css("visibility", "visible");})
You include the script by adding <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> to the page and then you can add the piece of code above to within <script></script> tags.

OnClientClick does not works on asp.net LinkButton

I have an asp.net linkbutton, which contains the OnClientClick property, however the function within the OnClientClick never gets called, it directly jumps to OnClick function.
Below are the 2 ways I am using LinkButton as:
<asp:LinkButton ID="btn" CssClass="button" Text="Browse Thumbnail" runat="server"
OnClientClick="return confirm('Are you sure you want to delete?');">
</asp:LinkButton>
and:
<asp:LinkButton ID="lnkDelete" runat="server"
OnClientClick="return confirm('Are you sure you want to delete this slide?');"
CommandName="DeleteThumbnail" CommandArgument='<%# Container.DataItemIndex %>'>
<asp:Image ImageUrl="~/images/delete.gif" ID="imgDelete" runat="server"></asp:Image>
</asp:LinkButton>
Both of the approaches does not works.
Can anyone please provide some solution for the same.
OnClientClick="javascript:return confirmAction();"
function confirmAction() {
if(confirm('Are you sure you want to delete?')) {
// you clicked the OK button.
// you can allow the form to post the data.
return true;
}
else {
return false;
}
}
implement the Onclick on the server side
protected void lnkdelete_Click(object sender, EventArgs e)
{
}
and if you dnt want to call server method use this
OnClientClick="javascript:confirmAction(); return false;"
There is most probably some other page element that is preventing this event from being fired.
Do you have any other page elements that might interfere? Have you tried removing all other page elements but this one? Do you have some AJAX calls that might interfere? Have you tried this with a simple html element (not asp.net)?
You are most probably doing everything fine in your link button but it seems like problem is elsewhere.
Place it in single quotes like below,
<asp:LinkButton ID="btn" CssClass="button" Text="Browse Thumbnail" runat="server" OnClientClick="return confirm('Are you sure you want to delete?');"></asp:LinkButton>
Use like this
function Navigate()
{
javascript: window.open("microsoft.com");
return false;
}
and on clientclick as follows
OnClientClick="return javascript:Navigate()"
or even
function Navigate()
{
window.open("microsoft.com");
return false;
}
OnClientClick="return Navigate()"
Seems like you have Disabled the javascript in IE. Just enable it & you are good to go. You can follow this post to enable/disable the javascript in IE:
http://browsers.about.com/od/internetexplorertutorials/ss/disable-javascript-ie9.htm
There is no problem with your OnClientClick method it should prompt confirm window. but you haven't specify the onclick event of the link button. So you will not able to get the event from code behind.
You may need to enable java scripts for your browser
How to enable JavaScript in a web browser?
Are you using Ajax toolkit? Update panel? then you need to register script by using script manager
To inject a confirm script from a AJAX postback,
ScriptManager.RegisterOnSubmitStatement(btn, Page.GetType(), "confirm", "return confirm('Are you sure');");

Redirecting new tab on button click.(Response.Redirect) in asp.net C#

I'm trying to open a page in new tab/window on button click.I tried in the google got this code but its not working.
Can anybody help me with this?
<asp:Button ID="btn" runat="Server" Text="SUBMIT"
OnClick="btnNewEntry_Click" OnClientClick="aspnetForm.target ='_blank';"/>
protected void btnNewEntry_Click(object sender, EventArgs e)
{
Response.Redirect("CMS_1.aspx");
}
When I use this I'm getting error saying
Microsoft JScript runtime error: 'aspnetForm' is undefined.
You can do something like this :
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" OnClientClick="document.forms[0].target = '_blank';" />
Wouldn't you be better off with
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="CMS_1.aspx"
Target="_blank">
Click here
</asp:HyperLink>
Because, to replicate your desired behavior on an asp:Button, you have to call window.open on the OnClientClick event of the button which looks a lot less cleaner than the above solution. Plus asp:HyperLink is there to handle scenarios like this.
If you want to replicate this using an asp:Button, do this.
<asp:Button ID="btn" runat="Server"
Text="SUBMIT"
OnClientClick="javascript:return openRequestedPopup();"/>
JavaScript function.
var windowObjectReference;
function openRequestedPopup() {
windowObjectReference = window.open("CMS_1.aspx",
"DescriptiveWindowName",
"menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
}
I think your code should work just remove one thing from here but it'll do redirection from current page within existing window
<asp:Button ID="btn" runat="Server" Text="SUBMIT"
OnClick="btnNewEntry_Click"/>
protected void btnNewEntry_Click(object sender, EventArgs e)
{
Response.Redirect("CMS_1.aspx");
}
And if u wanna do the this via client side scripting
Use this way
<asp:Button ID="BTN" runat="server" Text="Submit" OnClientClick="window.open('Default2.aspx')" />
According to me you should prefer the Client Side Scripting because just to open a new window server side will take a post back and that will be useless..
This is what I ended up using. Temporarily sets target to _blank, then sets it back.
OnClientClick="var originalTarget = document.forms[0].target; document.forms[0].target = '_blank'; setTimeout(function () { document.forms[0].target = originalTarget; }, 3000);"
You have to add following in header:
<script type="text/javascript">
function fixform() {
if (opener.document.getElementById("aspnetForm").target != "_blank") return;
opener.document.getElementById("aspnetForm").target = "";
opener.document.getElementById("aspnetForm").action = opener.location.href;
}
</script>
Then call fixform() in load your page.
You can use Rout redirecting.
protected void btnNewEntry_Click(object sender, EventArgs e)
{
Response.RedirectToRoute("CMS_1");
}
which requires to define your routing logic in Global.asax file that could be like that:
routes.MapPageRoute("CMS_1", "CMS_1", "~/CMS_1.aspx");
where any request by CMS_1 pattern in application scope will be redirecting to CMS_1.aspx, but in URL shows like www.yoursite.com/CMS_1

ScriptManager.RegisterClientScript in a UserControl within a FormView inside an Async Panel

I'm having an annoying problem registering a javascript event from inside a user control within a formview in an Async panel. I go to my formview, and press a button to switch into insert mode. This doesn't do a full page postback. Within insert mode, my user control's page_load event should then register a javascript event using ScriptManager.RegisterStartupScript:
ScriptManager.RegisterStartupScript(base.Page, this.GetType(), ("dialogJavascript" + this.ID), "alert(\"Registered\");", true);
However when I look at my HTML source, the event isn't there. Hence the alert box is never shown. This is the setup of my actual aspx file:
<igmisc:WebAsyncRefreshPanel ID="WebAsyncRefreshPanel1" runat="server">
<asp:FormView ID="FormView1" runat="server" DataSourceID="odsCurrentIncident">
<EditItemTemplate>
<uc1:SearchSEDUsers ID="SearchSEDUsers1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
Hello
<asp:Button ID="Button1" runat="server" CommandName="Edit" Text="Button" />
</ItemTemplate>
</asp:FormView>
</igmisc:WebAsyncRefreshPanel>
Does anyone have any idea what I might be missing here?
Have you tried using RegisterClientSideScript? You can always check the key for the script with IsClientSideScriptRegistered to ensure you don't register it multiple times.
I'm assuming the async panel is doing a partial page past back which doesn't trigger the mechansim to regenerate the startup scripts. Perhaps someone with a better understanding of the ASP.Net Page Life Cycle and the CLR can fill in those blanks.
For me that works fine. resizeChartMid() is a function name.
ScriptManager.RegisterStartupScript(this, typeof(string), "getchart48", "resizeChartMid();", true);
Try this, i got the same issue
ScriptManager.RegisterClientScriptBlock(MyBase.Page, Me.[GetType](),
("dialogJavascript" + this.ID), "alert(\"Registered\");", True)
This worked for me!

Categories