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

    Frage

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

    Thanks,

    Pallavi


    Pallavi Sharma

    Samstag, 14. April 2012 01:09

Antworten

  • 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

    • Als Antwort markiert Pengyu Zhao Sonntag, 29. April 2012 06:39
    Montag, 16. April 2012 21:30

Alle Antworten

  • 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)

    • Als Antwort vorgeschlagen Ishaan Puniani Samstag, 14. April 2012 13:24
    Samstag, 14. April 2012 13:23
  • 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/ 

    • Als Antwort vorgeschlagen raghupavan Samstag, 14. April 2012 15:47
    • Nicht als Antwort vorgeschlagen raghupavan Samstag, 14. April 2012 15:47
    • Als Antwort vorgeschlagen raghupavan Samstag, 14. April 2012 15:54
    Samstag, 14. April 2012 14:07
  • 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;

    }


    Montag, 16. April 2012 05:09
  • 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>



    • Bearbeitet Ishaan Puniani Montag, 16. April 2012 17:28 mismatch in the function name provided by you
    Montag, 16. April 2012 05:49
  • 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

    Montag, 16. April 2012 13:03
  • 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.

    • Als Antwort vorgeschlagen shishek Donnerstag, 26. April 2012 15:50
    Montag, 16. April 2012 15:01
  • 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

    Montag, 16. April 2012 19:56
  • 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

    • Als Antwort markiert Pengyu Zhao Sonntag, 29. April 2012 06:39
    Montag, 16. April 2012 21:30
  • hey pallavi 

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

    Freitag, 20. April 2012 07:13