Get Global Resource File from separate javascript file - javascript

I would like to set button text with different languages based on Culture. I have all my Javascript code in separate file. This file is imported in ToolkitScriptManager in Master Page. I have created JSString.resx global resource file. I have written code:
function showSessionTime() {
$(".sessionTime").val('<%= GetGlobalResourceObject("JSString", "SessionTime").ToString() %>' + " " + convertTime(minutes) + ":" + convertTime(seconds)); seconds = seconds - 1;
}
The function is then called in $(document).ready(function ()...
The button in Master Page:
<asp:Button ID="Button2" runat="server" class="ui-state-default ui-corner-all sessionTime"
UseSubmitBehavior="False" OnClientClick="javascript:return false;"
meta:resourcekey="Button2Resource1"></asp:Button>
I know that <%= GetGlobalResourceObject("JSString", "SessionTime").ToString() %> doesn't work in separate js files. How to workaround this as simple and as quick as possible?

I check value like this:
if ($("#ctl00_ddlLanguage").val() == 'pl-PL') {
expiredMessage = 'Twoja sesja wygasła. Zostałeś wylogowany.';
}
else {
expiredMessage = 'You session has ended. You have been logged out.';
}
Is there any risk of using #ctl00_ddlLanguage??

Related

RegisterStartupScript only runs the first time. ASP.NET C# JavaScript

I have an .aspx page with the following code
<asp:Panel runat="server" ID="ImagePanel">
<asp:UpdatePanel runat="server" ID="ImageUpdatePanel" UpdateMode="Conditional"><ContentTemplate>
<img id="photo" src="/Icons/Factory Layout.png" style="display: none;"/>
<script type="text/javascript">
function ResetImage(typeOfImage) {
var factoryImage = $("#photo");
if (typeOfImage === 1) {
factoryImage.attr("src",document.getElementById('<%= FactoryImageFileNameHF.ClientID %>').value);
}
else if (typeOfImage === 2) {
factoryImage.attr("src",document.getElementById('<%= IncidentFactoryImageFileNameHF.ClientID %>').value);
}
It is the ResetImage javascript function that I am trying to run with the following code in the code behind.
if (typeOfMap == 1)
{
FactoryImageFileNameHF.Value = fileNameOfFactoryImage.FileFullPath();
ClientScript.RegisterStartupScript(Page.GetType(), "test" + ScriptKeyHF.Value, "ResetImage(1);", true);
ScriptKeyHF.Value = (ScriptKeyHF.Value.ToInt() + 1).ToString();
}
else if (typeOfMap == 2)
{
IncidentFactoryImageFileNameHF.Value = fileNameOfFactoryImage.FileFullPath();
ClientScript.RegisterStartupScript(Page.GetType(), "test" + ScriptKeyHF.Value, "ResetImage(2);", true);
ScriptKeyHF.Value = (ScriptKeyHF.Value.ToInt() + 1).ToString();
}
The problem is that the ResetImage() method that I am calling in the RegisterStartUpScript only runs the first time on the browser. It doesn't run the second and third time of postbacks, etc.
I have tried the RegisterClientScriptBlock but it runs before the javascript code is there. Does anyone have any idea why the code only runs the first time.
The solution is that I was using a ClientScriptManager (ClientScript) instead of the a ScriptManager so I changed the line of code to
System.Web.UI.ScriptManager.RegisterStartupScript(this.Page,Page.GetType(), "test" + ScriptKeyHF.Value, "ResetImage(1);", true);
and it works.
I have the ScriptManager on a Master page and my code was on a child page

Asp.Net Control Value return undefined in Jquery

In my project i need change visible of dynamic asp control when click label based on textbox values. So i first tried to get textbox value when click label but its return undefined. I am search and get two methods i tried that also it return same.
My Try :
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$(document).on("click", "#lblShow", function() {
alert($('#<%=txtTotalVersion.ClientID%>').val());
alert($('input[id$=txtTotalVersion]').val());
var xTotlal =$('#<%=txtTotalVersion.ClientID%>').val()
var i = 0;
for (i = 0; i < xTotlal; i++) {
$('#createDiv' + i).style.display = "blcok";
$('#createDiv1' + i).style.display = "block";
$('#createDiv2' + i).style.display = "block";
$('#createDiv3' + i).style.display = "block";
}
});
});
</script>
HTML
<div id="DivCompName">
<asp:TextBox runat=server ID="txtTotalVersion" Visible="false"></asp:TextBox>
<asp:TextBox runat=server ID="txtCurrentDisplay" Visible="false"></asp:TextBox>
</div>
First two alert return undefined.
Visible="false" is asp.net attribute, in this case your control will not be rendered at the client side. So your client script won't find the control as it doesn't exists!
If you want to store some value at client side and don't want to display it then you can use HiddenFields or you can make the same control hidden by using css style display:none;. (Don't use Visible="false" for this)
you can add ClientIDMode=Static and call it from your jquery
<asp:TextBox runat=server ID="txtTotalVersion" Visible="false" ClientIDMode="Static"></asp:TextBox>
<script>
$(document).ready(function () {
alert("#txtTotalVersion").val();
})
</script>
reason is, the client id for your control might not be as it is assigned with ID="xxx", if the control is inside of another asp.net server control, after adding the ClientIDMode, you are telling your server to treat this control with a static ID
to learn more: msdn

Can you get parameter values of reportviewer at runtime?

I have a very specific question about the reporting services report viewer control.
I appreciate any help. I am a bit of a novice with Javascript on the client side, but I have to use this in my project.
THE REQUIREMENT
I need to retrieve the current value of a single parameter on a report viewer control embedded in a webpart at runtime. I need to access the value using Javascript on the client side.
Can this even be done? The reportviewer doesn't appear to get rendered.
THE HTML CODE FOR THE REPORT VIEWER
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnUnload="UpdatePanel_Unload">
<ContentTemplate>
<rsweb:ReportViewer ID="ReportViewer1" runat="server"
Font-Names="Verdana" Font-Size="8pt" Height="383px"
InteractiveDeviceInfos="(Collection)" ProcessingMode="Remote"
WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Width="757px"
SizeToReportContent="True">
</rsweb:ReportViewer>
</ContentTemplate>
</asp:UpdatePanel>
UPDATE- WORKING CODE BELOW
The key was creating a custom data attribute as #Fil indicated in this link (http://html5doctor.com/html5-custom-data-attributes/) and passing from the code behind and then accessing the $.cache. And passing the clientID of the reportviewer into the javascript function to get to the current instance of the webpart child controls.
<input type="hidden" id="<%= ASP_SSRS.ClientID %>_myDataState"
onchange="compareUnitValues(this.id, this.parentNode.id, '<%= ReportViewer1.ClientID %>', '<%= ASP_SSRS.ClientID %>', '<%= btnSendHiddenField.ClientID %>');" />
<script type ="text/javascript">
function compareUnitValues(elemt, parent, reportviewerID, value1, value2) {
var myDataUnit = $("#" + elemt),
parentObject = $("#" + parent),
reportviewerObject = $("#" + reportviewerID),
ssrs = $("#" + value1),
btnSend = $("#" + value2);
var myDataUnitValue = myDataUnit.val();
var myDataUnitJSON = jQuery.parseJSON(myDataUnitValue);
var currentmyDataUnit = myDataUnitJSON.currentUnit.objectId;
var sessioncurrentObjectId = document.getElementById('<%= hiddenCurrentObjectId.ClientID %>').value;
ssrs.val(myDataUnitValue);
var currentReportViewerParam = $("#" + reportviewerID).attr("data-report-param");
if (currentmyDataUnit != currentReportViewerParam) {
btnSend.trigger("click");
}
}
FROM CODE BEHIND CREATE THE CUSTOM DATA ATTRIBUTE
ReportViewer1.Attributes.Add("data-report-param", parsedObjectId)

Javascript method call from code behind

I visited Calling java script from codebehind and other questions marked as duplicate by some users. But specific to my problem none of them are helpful.
I have an already built CMS and I need to change one of the module which was built using a user control. I cannot hereby add runat="server"attribute to the form and head tags.
I am having a
<asp:GridView ID="gdvResxKeyValue" runat="server" Width="100%"AutoGenerateColumns="False">`</asp:GridView>`
and
<asp:TreeView ID="tvList" ShowLines="True" runat="server" ImageSet="Msdn" OnSelectedNodeChanged="tvList_SelectedNodeChanged">
<SelectedNodeStyle CssClass="sfSelectednode" />
</asp:TreeView>
gdvResxKeyValue is bind while selection of nodes is made in treeview ie.
protected void tvList_SelectedNodeChanged(object sender, EventArgs e)
{
gdvResxKeyValue.DataSource = lstResDef;
gdvResxKeyValue.DataBind();
this.Page.ClientScript.RegisterStartupScript(this.GetType(),LocalizationGlobalVariable5", string.Format("edition();"), true);
}
At last column of gdvResxKeyValue I have an image as
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="imgEditResxValue" CssClass="sfEdit" runat="server" ImageUrl="~/Administrator/Templates/Default/images/imgedit.png" />
</ItemTemplate>
</asp:TemplateField>
and I need a image click handler using javascript using minified version of jquery-1.9.1.js. so have written code as..
<script type="text/javascript">
//<![CDATA[
$.Localization = {
TextAreaID: 0,
FilePath: "",
ID: 0,
GridID: '<%=gdvResxKeyValue.ClientID%>'
};
function edition() {
$('#'+ $.Localization.GridID).on('click', 'img[class="sfEdit"]', function () {
var index = $(this).attr("alt");
$.Localization.ID = index;
var data = $('#' + $.Localization.GridID + ' textarea[title="' + index + '"]').val();
$('#txtResxValueEditor').val(data);
ShowPopUp("editorDiv");
});
}
</script>
But it is not working.
Try to put your grid to div wrapper:
<div id="myDiv">
....<asp:GridView ID="gdvResxKeyValue"....
</div>
and JavaScript:
$('#myDiv .sfEdit').click(function(){
alert('called');
});
EDIT: just noticed you call the startup script registration on every postback (the Change event of your treeview). It's not needed. Simply include your JavaScript file, something like:
$(document).ready(function(){
$('#myDiv .sfEdit').click(function(){
var imgId = $(this).attr('id');
alert('called ' + imgId);
});
});
and keep the gridview in div wrapper as described above.
Rather calling javascript method on clicking the image. On the ready of document I have written following code and finally got my problem solved.
$(document).on('click', "#" + $.Localization.GridID + ' img.sfEdit', function (e) {
var index = $(this).attr("alt");
$.Localization.ID = index;
var data = $('#' + $.Localization.GridID + ' textarea[title="' + index + '"]').val();
$('#txtResxValueEditor').val(data);
ShowPopUp("editorDiv");
e.preventDefault();
});

How can I write javascript functions in content page html and how I call that function

I write two javascript functions in content page. Then I take a html textbox and on the onkeypress event I try to call those two functions, but I am running that application and I didn't find any output to help me.
Here I am trying to count number of characters in textbox on keypress event. If I write a javascript function in simple page then it runs successfully but it doesn't run in content page help me.
Here is the code
<%# Page Language="C#" AutoEventWireup="true" MasterPageFile ="~/Site1.Master" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<asp:Content ID="Content1" runat="server"
contentplaceholderid="ContentPlaceHolder1">
<script language ="javascript" type = "text/javascript">
maxL=0;
var bName = navigator.appName;
function taCount(taObj,Cnt,totmsg)
{
objCnt=createObject(Cnt);
objtotmsg = createObject(totmsg);
objVal=taObj.value;
if (objCnt)
{
if(bName == "Netscape")
{
//objCnt.textContent = maxL-objVal.length;}
var totalchar = parseInt((objVal.length - 1) / 160);
objCnt.textContent = maxL + objVal.length;
objtotmsg.textContent = totalchar + 1;
}
//else{objCnt.innerText= maxL -objVal.length;}
else
{
var totalchar = parseInt((objVal.length - 1) / 160);
objCnt.innerText= maxL + objVal.length;
objtotmsg.innerText = totalchar + 1;
}
}
return true;
}
function createObject(objId)
{
if (document.getElementById) return document.getElementById(objId);
else if (document.layers) return eval("document." + objId);
else if (document.all) return eval("document.all." + objId);
else return eval("document." + objId);
}
</script>
<textarea id="TextArea1" onkeyup="return taCount(this,'charcount','totalmsg')" cols="20" rows="10"></textarea>
<asp:Label ID="charcount" runat="server" Text="0"></asp:Label>/<asp:Label ID="totalmsg" runat="server" Text="0"></asp:Label>
</asp:Content>
This is because the IDs get lengthened (their naming containers are pre-pended) when inside a master page, so instead of this:
taCount(this,'charcount','totalmsg')
You'll need this, which gets their actual rendered IDs:
taCount(this,'<%=charcount.ClientID %>','<%=totalmsg.ClientID %>')
If you view source in the browser and search for charcount or totalmsg when rendered in that master page, you'll see what I mean about the IDs, they'll probably look something like this: _ctl00_Content1_charcount.

Categories