?
Solved

why am I unable to see these hidden field values in PostBack ?

Posted on 2009-05-04
5
Medium Priority
?
548 Views
Last Modified: 2012-05-06
I need to call a codebehind method and pass some parameters.
below is my code, the hidden field values are null eventhough I am setting them using javascript. what am I doing wrong ?

I get an exception - object reference not set to null  at  - Request.Params.Get("hidID").ToString()

why is it ?
-- aspx page 
function confirmDelete(id)
       {
       
            if(confirm("Do you want to Delete this Invoice?"))
            {
              
               //document.forms[0].hidAction.Value = "DELETE";
               //document.forms[0].hidID.Value     = id;
               document.getElementById('<%= hidAction.ClientID %>').Value = "DELETE";
               document.getElementById('<%= hidID.ClientID %>').Value = id;
               document.getElementById('<%= btnDelete.ClientID %>').click();
               return true;
               //document.getElementById("hidID").value = id;
 
            }
   
            
         
      }
 
 
codebehind
--------------
 
  if (Page.IsPostBack)
        {
            long InvoiceID = Convert.ToInt64(Request.Params.Get("hidID").ToString());
            if (Request["hidAction"].ToString().ToUpper() == "DELETE")
            {
                this.DeleteInvoice(InvoiceID);
            }
 
        }

Open in new window

0
Comment
Question by:tech_question
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:Kalpana_Natarajan
ID: 24299860
In the aspx page, you are retrieving the value of the hidId.ClientID control, but you are not adding this to the query string of the same page.
You need to call the page from javascript as http://<PageAddress>?hidID=<Value retreived in the javascript method> when the form is submitted.

The null reference is thrown because the name value pair of hidID is missing in the Request String, not because hidden field value didn't work.

Please let me know if anything is unclear.

Regards,
Kalpana
0
 

Author Comment

by:tech_question
ID: 24300392
I do not want the users to see the value - I am not passing them as querystring but as form hidden values. Does that make sense ?
0
 
LVL 7

Assisted Solution

by:ajolly
ajolly earned 400 total points
ID: 24300777
I believe that you are adding the hidden field ot your ABC.aspx page using 'Hidden Field' control.
Something like this:

<asp:hiddenfield id="hidID" value="" runat="server"/>
<asp:hiddenfield id="hidAction" value="" runat="server"/>

and then you javascript updates the hidden field values.

If you are using the 'Hidden Field' control, you can just do as following in your code behind of the same page.

long InvoiceID = Convert.ToInt64(hidID.value);
if (hidAction.value.ToUpper() == "DELETE")
{
            this.DeleteInvoice(InvoiceID);
}
0
 
LVL 2

Assisted Solution

by:Kalpana_Natarajan
Kalpana_Natarajan earned 600 total points
ID: 24301061
Yes it makes sense.

The Request Params object will be null, unless or otherwise you load it in the client side and pass it to the page on PostBack. The request string doesnt get automatically populated on form submit, whether you have hidden fields or visible fields.

To prevent users from seeing the parameter string, use AJAX XmlHttpRequestObject and XmlHttpResponseObject. A simple example of it is given in this page

http://www.degraeve.com/reference/simple-ajax-example.php

(page courtesy www.degraeve.com - the first hit when search for "Simple Ajax Example" by Google)


Or set EnableViewState property of the page to true, and on PostBack, get the values of the form controls, and direct the values to another page using the Server.Transfer() method  (Server.Transfer("new page address?QueryString"). This method suppresses the new page address and the query string to be shown in the browser address bar, isntead the browser page will still show the old page address, which is your page with the hidden controls.
0
 

Accepted Solution

by:
tech_question earned 0 total points
ID: 24309096
I am using PageMethods to achieve this. Thanks for your help though.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question