Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

861 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