How to get the value of a varibale,created in a Javascript, into a code-behind page?

I am creating a Javascript Like:

function GetSelectedTopics()

{debugger;

  var selectedIds=[];

  var TopicIds=document.getElementById("hidTopicList").value.split(",");

  for(var i=0;i<TopicIds.length;i++)

  {

   if(document.getElementById(TOPIC_CHECKBOX_PREFIX+TopicIds[i]).checked)

  {

     selectedIds[selectedIds.length]=TopicIds[i];

 
 }

 }

}

return selectedIds;

}

Here I want to get the value of selectedIDs into my code-behind page. I am calling Javascript like:

btnCopyTopics.OnClientClick = "return GetSelectedTopics()";

I am thinking to create an asp hiddenfield variable:

<asp:HiddenField id="hidSelectedTopicIDList" ruant="Server"></asp:HiddenField>

And strore the value of selectedIDs into this hidden variable when I am string value into selectedIDs variable like:

 selectedIds[selectedIds.length]=TopicIds[i];

     document.getElementById("hidSelectedTopicIDList").value = selectedIds.toString();

     document.getElementById("hidSelectedTopicIDList").value = "," ;

I dont know if I am right.

Can please some one help me out?

Thanks

0
Khushbu84
6/30/2008 2:46:05 PM
asp.net.client-side 24353 articles. 1 followers. Follow

10 Replies
545 Views

Similar Articles

[PageSpeed] 41

You're definitely on the right track. Using hidden fields to communicate back to the server is the most popular approach. You could also use JavaScript to do call a web service - if appropriate, or you could store stuff in a cookie etc. But in most scenarios, using the hidden field trick is the easiest approach.

You should be aware though that you might experience that the id of the hidden field gets mangled by ASP.NET, if you are using master pages (or other data containers). You can easily determine if this is the case when you do a view source in the browser and search for your hidden field id. If the id is rendered exactly as you wrote it then you don't need to worry. If not, then you can use the ClientID property (server-side) to get the actual id that is rendered back to the client.

Example:

document.getElementById('<% =hidSelectedTopicIDList.ClientID %>').value = selectedIds.toString();


If this post was useful to you, please mark it as answer. Thank you!
0
johram
6/30/2008 2:54:57 PM

It is not getting value because when I try to access hidSelectedTopicIDList variable

List<int> sourceTopicIdList = new List<int>(); 

if (hidSelectedTopicIDList.Value.Trim().Length > 0)

{

//sourceTopicIdList.Add();

}

, it shows null and throws an error:: Object reference not set to an instance of an object. as sson as it goes on If statemant.

 

I also tried like:

else

{

 document.getElementById("hidSelectedTopicIDList").value = selectedIds.join(",");

 return selectedIds;

}

but it still shows hidSelectedTopicIDList as null and thrwos the same error.

 

0
Khushbu84
6/30/2008 3:32:11 PM

have a check before your id statement

if(hidSelectedTopicIDList!=null && !String.IsNullOrEmpty(hidSelectedTopicIDList.Value)

{

   after this check put your existing logic.

}

 

Please mark as answer if I answered your post.


Please mark as answer if you find my post helpful.
0
CsharpJunkie
6/30/2008 3:51:32 PM

OK It is not getting any error but it is not getting any value even my selectedIDs has values.

I dont know why hidSelectedTopicIDList is null. It should have value if selectedIDs has values because I am storing all values of selectedIDs into hidSelectedTopicIDList

Can you tell me why hidSelectedTopicIDList is null?

0
Khushbu84
6/30/2008 4:05:35 PM

could you please check for me if hidSelectedTopicIDList is null or hidSelectedTopicIDList.value is null.

Because i think control is not null but value of control is null


Please mark as answer if you find my post helpful.
0
CsharpJunkie
6/30/2008 4:11:19 PM
Actually, 

hidSelectedTopicIDList is null. When I move my cursor to the If statement is shows "hidSelectedTopicIDList null"

I also tried to put OR condition just to see then it stii shows "hidSelectedTopicIDList null" and gets an error;" Object reference not set to an instance of an object."

I think it is giving this error because hidSelectedTopicIDList has not any value. i dont know if I am right

 

0
Khushbu84
6/30/2008 4:26:41 PM

Change your JavaScript to something like this.

<script type="text/javascript">
<!--
function GetSelectedTopics()
{
 //debugger;

 var elementRef = document.getElementById('<%= hidSelectedTopicIDList.ClientID %>');
 elementRef.value = '';

 var selectedIds = new Array();
 var topicIdArray = document.getElementById('hidTopicList').value.split(',');

 for (var i=0;i<topicIdArray.length;i++)
 {
  if ( document.getElementById(TOPIC_CHECKBOX_PREFIX + topicIdArray[i]).checked )
  {

   selectedIds[selectedIds.length] = topicIdArray[i];

   if ( elementRef.value.length > 0 )
    elementRef.value += ',';

   elementRef.value += topicIdArray[i];
  }

 }

 return selectedIds;
}
// -->
</script>

NC...

0
NC01
6/30/2008 4:31:20 PM
It is working well If I create HTML input hidden variable.<input type="hidden" id="hdnSelectedTopicIDList" runat="server" /> 

selectedIds[selectedIds.length]=TopicIds[i];

document.getElementById("hdnSelectedTopicIDList").value = selectedIds[i];

 But problem is that it is getting last ID value from selectedIDs variable and I want to store a list if IDs. Can you tell me how to do that?

0
Khushbu84
6/30/2008 5:30:06 PM

well it's doing this because of following statement

selectedIds[selectedIds.length]=TopicIds[i];

you should have something like following

for(var i=0;i<TopicIds.length;i++)

{

       selectedIds[i]=TopicIds[i];

}

 

Hope this helps.


Please mark as answer if you find my post helpful.
0
CsharpJunkie
6/30/2008 5:37:06 PM

Here's a complete working sample from the tidbits of information that I have gathered from this thread.

The aspx file:

<form id="form1" runat="server">
 <asp:CheckBox ID="abc_1" runat="server" />
 <asp:CheckBox ID="abc_2" runat="server" />
 <asp:CheckBox ID="abc_3" runat="server" />
 <input type="button" onclick="GetSelectedTopics();" value="Get Selected Topics" />

 <asp:HiddenField ID="hidTopicList" runat="server"  Value="1,2,3"/>
 <asp:HiddenField ID="hidSelectedTopicIDList" runat="server"  Value=""/>
</form>

<script type="text/javascript">
<!--
var TOPIC_CHECKBOX_PREFIX = 'abc_';

function GetSelectedTopics()
{
 //debugger;

 var elementRef = document.getElementById('<%= hidSelectedTopicIDList.ClientID %>');
 elementRef.value = '';

 var selectedIds = new Array();
 var topicIdArray = document.getElementById('hidTopicList').value.split(',');

 for (var i=0;i<topicIdArray.length;i++)
 {
  if ( document.getElementById(TOPIC_CHECKBOX_PREFIX + topicIdArray[i]).checked )
  {
   selectedIds[selectedIds.length] = topicIdArray[i];

   if ( elementRef.value.length > 0 )
   elementRef.value += ',';

   elementRef.value += topicIdArray[i];
  }
 }

 return selectedIds;
}
// -->
</script>

The aspx.cs file:

protected void Page_Load(object sender, EventArgs e)
{
    if ( this.IsPostBack )
    {
     this.Response.Write("Value: " + hidSelectedTopicIDList.Value + "<br>");
    }
}

NC...

0
NC01
6/30/2008 6:01:23 PM
Reply:

Similar Artilces:

How can i create get values from Opened page in client side to my first page(server page)
i have a little bit problem that my Server page has coding like this : btn.attributes.add("onclick()",getdata()) and one text box is txt1 now when user clicks it Button btn then getdata function  executes ok client page function getdata(){ response.redirect("hello.aspx") } now on hello.aspx has 1 textbox txt2   now what ever the data has been inputted by user in (client side textbox) txt2 i want to assign it to txt1 (server side )conrtol on server pls help me send me code ...  Jay "Enjoy Life" When you do the redirect from Page #2 back to Page...

How to get value into javascript from code behind page.....
Hello friends...               I want data from .cs file into javascript.How to do that.   Please help me...   HI Here i am sending a radibuttonlist selected text to the javascript function. foreach (ListItem li in optSearch.Items) { li.Attributes.Add("OnClick", "javascript:return GetSearchOptValue('" + li.Text.ToString() + "');"); } This way u can pass the value to javascript function. Thank Venk...

getting client side value in code behind
Dear all, i have declared a variable in client side to hold a particular value..i have to access this variable within pageload evrnt.how can i do it???please suggest   mama Hello, You can make use of Hidden variable, which can be accessed in client side as well as in code behind. I hope this will be helpful..S.SRIRAM hello take a hidden field in your page fill it with ur javascript variable then access the value of hidden field in page loadHAPPY CODINGhttp://shobhitrai.blogspot.com/ Hi It is an easy codding man.. just add runat="server" attribu...

Use client side code to get hidden field value from calling page
Hi, I have a web application that is being called from an extranet app. I need to access the value of a hidden form variable on the calling form, I have tried Request.form("Fieldname") & cannot pick up any values. They are not keen on passing the value in the querystring - I would really appreciate some help on this. Thanks If the item is not in the form collection, then they're probably not doing a POST to your page. You can pass information to a page three ways: URL (querystring, fragment, etc), POST (form.submit()), and via a HTTP Header. They'll need to decide...

finding server control, in the code behind, created from client side using javascript
Hi experts   i wanna add server side controls using javascript and access it in code behind but i am not able to access it can u give me the solution example javascript file function AddControl() { var table=document.createElement('table') var tr=document.createElement('tr') var cell1=document.createElement('td') cell1.innerHtml='<input type="text" ID="txtSample" runat="server"/> tr.appendChild(cell1) table.appendChild(tr); }   if i try to get this control in code behind like this i am not able to get ...

How to get the Client side values in server side code.
Hi all, i have a proble i need to get the client side javascript values in C# code at server side. how can i pass those values? i am using version 1.1, i kno thatwe can use callback events mechnisam in 2.0 or ajax ,apart from that how we can pass? My problem is i need to stop the execution of the code based on after reciving values from the client   any ideas please???   Million thanks RamCheersRam MCP Hello Ram, a common approach is from javascript set an hidden field, then issue a postback. On the server-side, handle the post-back. Feel free to go deeper in your que...

how to access/assign values to variables of aspx code-behind page in javascript code?
Hi, Can anyone help me with this? How to access or assign values to variables of aspx.cs code behind page using javascript code in the aspx page. Will be eagerly waiting for a reply. Thankyou. Raj. here is one way..assign hidden fields to any values you want to get at Page.RegisterHiddenField("fieldName", "fieldValue") then I think in Javascript it would be something like document.forms[0].fieldName.value; to get at it. Ask and it will be given to you; seek and you will find; knock and the door will be opened to you. Luke 11:9 hi Michael, ...

Atlas client side control value available to server side C# code behind
I have an existing ASP.NET application. The page contains a datagrid (asp:datagrid). In my current C# code behind, I fetch data from SQL Server 2005 and bind to the datagrid when a button is selected (Click event). There can be many entires in the grid however, so the user typically enters text in a textbox as a filter on the returned results from SQL Server 2005. Now after studying an Atlas sample animalservice http://atlas.asp.net/docs/atlas/doc/data/default.aspx, with an HTML suggestion text box, I would like to use client-side Atlas to give the user a better indication of...

How to create a code behind page when page was created in expressions web
 When an ASPX page is created and designed in Expressions Web (which has no code behind page check box), how do I add a code behind file in VWD2008?  The help says the following:Adding code-behind files in ASPX or ASCX files within a traditional ASP.NET 2.0 environment is very easy thanks to Microsoft Visual Studio. All you have to do is right-click the file in Visual Studio, either in Solution Explorer or in the designer, and select View Code. Visual Studio then creates a new code behind file (if one did not already exist)but doing the above simply displays the ASPX page. I f...

Getting Textbox Value in Page Code Behind -
The client side webpage has textboxes displaying current information of a specific record (first name, last name, company, title,etc). This information is displayed, the issue becomes when that information is changed and the update button is clicked, how to get the value that was changed (if the text box already had a value), how to get the value if the textbox was empty. How to get the newly changed value  on the codebehind so that it can be processed through an update function. The update function is already written , the only thing that is needed is the new value in the textbox. ...

Getting Textbox Value in Page Code Behind
The client side webpage has textboxes displaying current information of a specific record (first name, last name, company, title,etc). This information is displayed, the issue becomes when that information is changed and the update button is clicked, how to get the value that was changed (if the text box already had a value), how to get the value if the textbox was empty. How to get the newly changed value  on the codebehind so that it can be processed through an update function. The update function is already written , the only thing that is needed is the new value in the textb...

executing server side code and client side (javascript) code simultaneously while clicking a button
how to execute server side code and client side (javascript) code simultaneously while clicking a button in asp.net. You can't. When clicking a button: the client-side executes, the page does a PostBack, then the server-side executes.NC... what if i want to update a database and then open a new windows is it possible with Jscript.net?...

Get whole html page with values in Code behind.
Hello,  I want to get all HTML code with values entered by user in code behind as it is, Actully this need has arised when I want to send a mail to a given mail id, the whole HTML page as it is (same UI) with entered values.   For this I hv written following code, but it only gives the HTML and not the values user has entered. ------------------------------------------------------------------------------------------------------------------------------------------------------ private...

how to get the dynamically created textbox value in code behind?
Hi all, In my vb.net page i've a button('add more'). when i click the 'add more' button it will create 4 textboxes and a delete button.on clicking again another 4 textboxes and a delete button will be created and so on. This work well. What my problem is i cannot get the created textbox's value in the server side.. Here is my code..  <%@ Page Language="VB" MasterPageFile="~/SwapnaMaster.master" AutoEventWireup="false" CodeFile="SwapnaTestPage.aspx.vb" Inherits="SwapnaTestPage" title="Untitled Page" ...

Web resources about - How to get the value of a varibale,created in a Javascript, into a code-behind page? - asp.net.client-side

Looking at the code behind our three uses of Apache Hadoop - Facebook
Facebook Engineering hat eine Notiz mit dem Titel Looking at the code behind our three uses of Apache Hadoop geschrieben. Du kannst den vollständigen ...

How Heartbleed Works: The Code Behind the Internet's Security Nightmare
By now you've surely heard of Heartbleed, the hole in the internet's security that exposed countless encrypted transactions to any attacker who ...

Anatomy of an interactive: a look at the code behind our Second Screen
... and Paralympics coming to town, this presented the perfect opportunity to try it on. Being mainly responsible for the client-side code, I'll ...

How Heartbleed Works: The Code Behind the Internet's Security Nightmare
By now you've surely heard of Heartbleed, the hole in the internet's security that exposed countless encrypted transactions to any attacker who ...

Resources last updated: 1/1/2016 11:19:09 AM