Value not being able to pass from one page to other - c#

This is how i pass value to the page called in iframe
<script type="text/javascript">
function refreshConversatio() {
document.getElementById('ifrmConversation').src = 'Default2.aspx?id=' + document.getElementById('<%=HiddenField1.ClientID %>').value;
}
</script>
This is How i recieve value in othe page which is being loaded in iframe
<script type="text/javascript">
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
function myLoad() {
document.getElementById('<%=hdn.ClientID%>').Value = getParameterByName("id");
}
</script>
<asp:HiddenField ID="hdn" runat="server" />
<script type="text/javascript">
myLoad();
</script>
I think that there is problem somewhere (might be in myLoad() ) because i am not able to recieve passed value. What am i doing wrong here?

Please refer this SO answer to know how to call a function in iframe from parent window:
Calling javascript function in iframe

In order to get the value of hidden field try the following:
var abc = document.getElementById('hdn');
or
var abc = document.getElementById('hdn').value;

Try this sample way
code
<asp:HiddenField ID="hf_myhiddenfield" runat="server" Value="hidden value"/>
You can use a Javascript function to insert the value into your onclick attribute
onclick
onclick="window.open('../New/FeedbackV4.aspx'+GetHFValue(),'FeedbackWindow','width=960,height=640,scrollbars=yes,resizable=yes,status=yes')"
Javascript
<script type="text/javascript">
function GetHFValue() {
var hf_value = '?' + document.getElementById("<%= hf_myhiddenfield.ClientID %>").value;
return hf_value;
}
</script>
The above code sample is here
and Call this below code for get query string value
/*
* <summary>
* Get the querystring value
* </summary>
* <param name="key">A string contains the querystring key</param>
* <param name="defaultVal">Object which get returns when there is not key</param>
**/
function getQuerystring(key, defaultVal) {
if (defaultVal == null) {
defaultVal = "";
}
key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
var qs = regex.exec(window.location.href);
if (qs == null) {
return defaultVal;
}
else {
return qs[1];
}
}

Related

Why Jquery is not working for asp.net textbox's CssClass Property?

I am tying to validate the textboxes for achieving 'alphabets only' property in asp.net page with Jquery.
Here is the code
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
.............codes.............
<script type="text/javascript">
$('.alph').keypress(function (e) {
var regex = new RegExp("^[a-zA-Z\s]+$");
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
return true;
}
else {
e.preventDefault();
alert('Alphabets only');
return false;
}
});
</script>
.............codes.............
<asp:TextBox ID="txt_name" CssClass="alph" BorderColor="Gray" Font-Size="Large" Height="25" Width="250" runat="server"></asp:TextBox>
This code didn't work and I am sure my computer is connected to the internet to reach code.jquery.com. Help me please.
try this script code after Document.ready block
$(document).ready(function(){
$('.alph').keypress(function (e) {
var regex = new RegExp("^[a-zA-Z\s]+$");
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
return true;
}
else {
e.preventDefault();
alert('Alphabets only');
return false;
}
});
});
Before you can safely use jQuery you need to ensure that the page is in a state where it's ready to be manipulated. With jQuery, we accomplish this by putting our code in a function, and then passing that function to $(document).ready(). The function we pass can just be an anonymous function.
My mistake! Since I am new to Jquery I didn't know about 'doc.ready' I corrected the code as
$(document).ready(function () {
$('#<%=txt_name.ClientID %>').keypress(function (e) {
var regex = new RegExp("^[a-zA-Z\s]+$");
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
return true;
}
else {
e.preventDefault();
alert('Alphabets only');
return false;
}
});
});
Code works perfect!

How to call both javascript function and C# function on a single button click in MVC

I want to call a javascript and a function written in Model Class using a single button click. I used the following code:
<script language="javascript" type="text/javascript">
function RunEXE() {
var txtfile = document.getElementById("txtFileName");
//var txtProgram = document.getElementById("txtProgram");
//if ((!String.IsNullOrEmpty(txtfile)) && (!String.IsNullOrWhiteSpace(txtProgram))) {
if (txtfile.value != "") {
var oShell = new ActiveXObject("WScript.Shell");
//var prog = "c:\\Pgms\\sample0.exe";
var prog = "\\\\Test-PC\\Programms\\" + txtfile.value + ".exe";
oShell.Run('"' + prog + '"', 1);
} else {
alert('The file name must be entered in file name textbox');
}
}
</script>
<input type="submit" name="button" value="Run" onclick="RunEXE()" />
The below code is Model function:
public ActionResult Run(UserProgram userProgram)
{
SaveAndCompile(userProgram);
return null;
}
But its working with Run() alone and not running RunEXE()
[HttpPost]
public ActionResult RunAction(string option1)
{
//if needed, you can use the "option1" value to determine the UserProgram to pass
UserProgram userProgram = new UserProgram();
Run(userProgram);
//you can return a JSON reuslt that you can evaluate back at the client
return Json(new { #Success = true, #MyString = "a string" });
}
$.post('#Url.Action("RunAction", "MyController")',
{
option1: "some optional value"
},
function (data) {
alert("success!");
//here you have access to your JSON result via data, for example:
//data.Success = true
//data.MyString = "a string"
}
);
In your case, you can submit your form by JQuery submit function.
I assume your code will like below:
<form id="form" action="/Run">
// your some inputs
<input type="submit" name="button" value="Run" />
</form>
And the javascript for submitting will be:
$(function() {
$('#form').submit(function() {
// to do something before the form is submitted
RunEXE();
return true; // return false to cancel form action
});
});
Cheers.

Url.Action Not figuring out the actual url

I am trying to use Url.Action to generate the correct HTTP URL based on a controller action like this :
$.post('#Html.Raw(Url.Action("Delete", new { id = "1" }))')
However, it is not working as expected . The actual url fired (got this from dev tools) is
http://localhost:60223/CordBlood/#Html.Raw(Url.Action(%22Delete%22,%20new%20%7B%20id%20=%20%224%22%20%7D))
Whereas I want something like this :
http://localhost:60223/CordBlood/Delete/1
What am I doing wrong here?
I think you are trying to achieve something similar to this
<script src="#Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#dropdown").change(function () {
var id = $("#dropdown").val();
if (id == "")
{ id = 0; }
var dataToSend = {
Id: id
};
RedirectToPage(id);
});
function RedirectToPage(id) {
var url = '#Url.Action("Delete", "yourController", new { Id = "__id__" })';
window.location.href = url.replace('__id__', id);
}
});
</script>
Hope this will give you some idea

limit characters in dynamic text box in data list asp.net 3.5

In the JavaScript below I have a problem in that the JavaScript applies only to one text box not to all the text area's because the ID generated in the html is different for all text area's. Any help regarding to this
<font>Maximum Number of characters for this text box is 255.<br>
<textarea runat="server" id="txtAnswerMain" onkeypress="return taLimit(this)" onkeyup="return taCount(this,'myCounter')"
name="Description" rows="7" wrap="physical" cols="40">
</textarea>
this is the java script i am using it works for single text area but when i apply to dynamically created text area it does not work
<script language="Javascript">
maxL = 100;
var bName = navigator.appName;
function taLimit(taObj) {
if (taObj.value.length == maxL) return false;
return true;
}
function taCount(taObj, Cnt) {
objCnt = createObject(Cnt);
objVal = taObj.value;
if (objVal.length > maxL) objVal = objVal.substring(0, maxL);
if (objCnt) {
if (bName == "Netscape") {
objCnt.textContent = maxL - objVal.length;
}
else { objCnt.innerText = maxL - objVal.length; }
}
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>
If I dynamically add a textarea using JQuery it works fine.
<script type="text/JavaScript">
$(document).ready(function(){
$('#divToAddTo').append('<textarea id="txtAnswerMain2" onkeypress="return taLimit(this)" onkeyup="return taCount(this,'myCounter')" name="Description" rows="7" wrap="physical" cols="40">');
$('#divToAddTo').append('</textarea>');
});
</script>

String is undefined

I've got the following property in the code-behind of my .aspx:
protected string CurrentProductView
{
get
{
string viewName = string.Empty;
viewName = Enum.GetName(typeof(ProductView), currentProdView);
return viewName;
}
}
In my .aspx I've got some Javascript that tries to reference this string:
$(document).ready(function ()
{
var action = <%=CurrentProductView %>
$("#recommendations").load("recommendationsHandler.ashx?action=" + action + "item&csid=" + csid + "&productID=" + productID, function()
{
$("#recommendationsView").show();
});
});
but for some reason I get "Item is not defined".
When I debug this, I'm definitely seeing a string come back for viewName. So why would it complain if a string is coming back?!?!
Change this:
var action = <%=CurrentProductView %>
to this:
var action = "<%=CurrentProductView %>"
Since you are printing out a string value to the page into a variable, you need quotes around it, because the value is viewed as a literal value to the JavaScript on the page. You don't need the quotes around ints, because in JavaScript this is legal:
var my_number = 4;
where this is not
var my_string = this is a string;
and it needs to be this:
var my_string = "this is a string";

Categories

Resources