none
set a Hidden control in client side using Jquery and retrieve it in code behind

    Question

  • Is it possible to set a Hidden control in client side using JQuery and retrieve it in code behind?

    Thanks,

    Pallavi


    Pallavi Sharma

    Saturday, April 14, 2012 1:09 AM

Answers

  • you can also use asp control instead of html generic control. 

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

    , client id is associated with all the server controls , there must b e some issue in designer file. otherwise methods works perfectly for all server controls

    • Marked as answer by Pengyu Zhao Sunday, April 29, 2012 6:39 AM
    Monday, April 16, 2012 9:30 PM

All replies

  • yes .. its very simple even you can do this by javascript 

    in javascript you can set the value using 

    document.getElementById("<%=hdnTitle.ClientId%>").value="abc";  // text you want to set

    using jquery you can use $("#"+"<%=hdnTitle.ClientId%>").val('abc');

    remember hdnTitle is a runat server hidden control (like hidden field)

    • Proposed as answer by Ishaan Puniani Saturday, April 14, 2012 1:24 PM
    Saturday, April 14, 2012 1:23 PM
  • Hi Pallavi,

    if you are using the SharePoint 2010, can explore client object model, refer the below link 

    http://blogs.technet.com/b/speschka/archive/2009/11/01/using-the-sharepoint-2010-client-object-model-part-4.aspx

    or

    you can use the codeplex solution to hide or to make read only, refer below

    http://sputility.codeplex.com/ 

    • Proposed as answer by raghupavan Saturday, April 14, 2012 3:47 PM
    • Unproposed as answer by raghupavan Saturday, April 14, 2012 3:47 PM
    • Proposed as answer by raghupavan Saturday, April 14, 2012 3:54 PM
    Saturday, April 14, 2012 2:07 PM
  • Hey Ishaan,

    Can you elaborate this with an example

    Say If i have a hidden conmtrol as below defined in ascx file, a lable which updates the text of label based on the Javascript updates of a save button click > updates a hidden field.

    <inputid="hiddeninputctrl"name="hiddeninputctrl"  type="hidden"runat="server" value="123"/>

    <asp:label id="lbl1" runat="server" />

    and a save button on which we call the 2 events

    <asp:ButtonID="btnSave"name="save"type="submit"runat="server"Text="Save"OnClientClick="funcUpdateHidden()"  onclick="btnSave_Click"/>

    <script>

    funcUpdateHidden()

    {$('input[name=hiddeninputctrl]').val() = "1111";

    alert(" updated value is " + $('input[name=hiddeninputctrl]').val() );

    }

    </script>

    in the .cs file in btn save click

    protected void btnSave_Click(object sender, EventArgs e) { lbl1.Text = hiddeninputctrl.Text;

    }


    Monday, April 16, 2012 5:09 AM
  • you can modify javacsript function ( funcUpdateHidden() ) it like 

    <script type="text/javascript">

    function funcUpdateHidden()

    {

    $('#'+'<%=hiddeninputctrl.ClientId %>').val("1111");

    alert(" updated value is " + $('#'+'<%=hiddeninputctrl.ClientId %>').val() );

    return true;

    }

    </script>



    • Edited by Ishaan Puniani Monday, April 16, 2012 5:28 PM mismatch in the function name provided by you
    Monday, April 16, 2012 5:49 AM
  • OK.. but the requirement is to get that hidden field in code behind,  If
    the hidden field gets an runat server, it does not get the updates from
    Javascript in ascx file, and if the hidden control is not runat server then I
    am not able to access it from code behind.


    Pallavi Sharma

    Monday, April 16, 2012 1:03 PM
  • Hey pallavi

    had you tried the above function ..... ??

    <%=hiddeninputctrl.ClientId %> is used to get the server control client id i.e. rendered on the client side and this id is used by javascript / jquery to get element from dom & to set value in it .

    since it is runat server control you can get it directly on server side .

    ok to make it more clear you can use it in this way

    in code behind 

    btnSave.OnClientClick="UpdateHidden('"+hiddeninputctrl.ClientId +"');

    And In ascx 

    <script type="text/javascript">

    function UpdateHidden(hiddenControlClientId)

    {

    $('#hiddenControlClientId').val("1111");

    alert(" updated value is " + $('#hiddenControlClientId').val() );

    return true;

    }

    </script>

    both way of calling js is correct and suggested. & if this ddnt work then there must be come issue in page or may be you have disabled javascript in your browser.

    • Proposed as answer by shishek Thursday, April 26, 2012 3:50 PM
    Monday, April 16, 2012 3:01 PM
  • OK.. looks like there is some issue with the field type

    getting an error

    'System.Web.UI.HtmlControls.HtmlInputHidden' does not contain a definition for 'ClientId'


    Pallavi Sharma

    Monday, April 16, 2012 7:56 PM
  • you can also use asp control instead of html generic control. 

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

    , client id is associated with all the server controls , there must b e some issue in designer file. otherwise methods works perfectly for all server controls

    • Marked as answer by Pengyu Zhao Sunday, April 29, 2012 6:39 AM
    Monday, April 16, 2012 9:30 PM
  • hey pallavi 

    dd you find any issue or any other alternative..??

    Friday, April 20, 2012 7:13 AM