Solved

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

Posted on 2009-05-04
5
547 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 100 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 150 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

690 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