Solved

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

Posted on 2009-05-04
5
546 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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