Solved

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

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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 …
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now