get hidden value on server side using master page

virafh
virafh used Ask the Experts™
on
Dear Friends,

i am setting the value of hidden fields on client side
and when i am not using master page, i can easily get the value of hidden fields on server side

but when i am using master page, the hidden value coming blank on server side

please let me know the solution.


thanx
on client side

document.getElementById("hdValue").value = filename;

on server side

string val = hdValue.Value;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
you will need to use the find control method.  Please see example below


HiddenField MyHiddenField = (HiddenField)this.Master.FindControl("MainContent").FindControl("MyHiddenField");

Open in new window

Commented:
if you need more of an explanation please let me know

Author

Commented:
its giving me error:

An exception of type 'System.NullReferenceException' occurred in App_Web of addcategory.aspx.75226392.6fyrob-n.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Commented:
can you post the code for the aspx page, what is the name of the content place holder.  Is the hidden field inside another control?

Author

Commented:
Dear Ross,

please check the attached Code snippets,

if you want any more information, please let  me know.

thanx
Aspx Page

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.master"
    CodeFile="~/AddCategory.aspx.cs" Inherits="AddCategory" %>

<asp:Content ID="Content2" ContentPlaceHolderID="cphTitle" runat="Server">
    Add Category
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<input type="hidden" runat="server" id="hdValue" />
</asp:Content>

Open in new window

Commented:
cphTitle is the place holder ID that you where missing try the following now


HiddenField MyHiddenField = (HiddenField)this.Master.FindControl("cphTitle").FindControl("MyHiddenField");

Open in new window

Author

Commented:
its giving the below error
An exception of type 'System.InvalidCastException' occurred in addcategory.aspx.75226392.rbed_u-5.dll but was not handled in user code

Additional information: Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputHidden' to type 'System.Web.UI.WebControls.HiddenField'.

Open in new window

Commented:
We are nearly there, change the hidden field on the aspx page to the following:

<asp:HiddenField ID="hdValue" runat="server" />

You must have inserted the hidden field from the html section of the toolbar instead of the web controls.

Author

Commented:
buddy now its not giving any error but the value is NULL

please check the javascript, where i set the value

please remember its working fine when the page not contain the master page.


thanx

.cs Page:
HiddenField MyHiddenField = (HiddenField)this.Master.FindControl("ContentPlaceHolder1").FindControl("hdValue");

Javascript:
<script type="text/javascript">
        $(document).ready(function() {
        new Ajax_upload('#aDefaultImage', {
                action: '../FileHandler.ashx?type=image',
                onSubmit: function(file, ext) {
                    if (!(ext && /^(jpg|png|jpeg|gif)$/.test(ext))) {
                        // extension is not allowed
                        alert('Error: invalid file extension');
                        // cancel upload
                        return false;
                    }
                    this.disable();
                    $('#loading').css('visibility', 'visible');
                },
                onComplete: function(file, response) {
                    var filename = response.replace('<pre>', '').replace('</pre>', '')

                    $('#loading').css('visibility', 'hidden');
                    //$('#result').append('<img src="../images/' + filename + '" style="padding-right:5px;" />');
                   document.getElementById('imgDefault').src = '../images/' + filename;

                   this.enable();


                   document.getElementById("hdValue").value = filename;



                    $('#dialog').dialog('close');

                }
            });

        });
    </script>

Open in new window

Author

Commented:
i am using hidden value just to store the file name which i uploaded on the server and get this value on the server side to store in db

if you have any other solution to get the file name value on server side, please let me know.

in short i just want client side value on server side.

thanx

Commented:
Your very nearly there.  ASP.net asigns client side control id's on the server.  Where you set the value with the js get element by id the client side id will be different.  If you view the source of the page in your web browser and see what id the hidden field has.
Commented:
All you need to do now is change the javascript from

 document.getElementById("hdValue").value = filename;

to something like the following

 document.getElementById("ctl00_MainContent_ctl00_hdValue").value = filename;

when the page is in a web browser view the source then find the ID

Author

Commented:


Thank You Very Much :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial