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

已答覆 set a Hidden control in client side using Jquery and retrieve it in code behind

  • samedi 14 avril 2012 01:09
     
     

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

    Thanks,

    Pallavi


    Pallavi Sharma

Toutes les réponses

  • samedi 14 avril 2012 13:23
     
     Réponse proposée

    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)

    • Proposé comme réponse Ishaan Puniani samedi 14 avril 2012 13:24
    •  
  • samedi 14 avril 2012 14:07
     
     Réponse proposée

    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/ 

    • Proposé comme réponse raghupavan samedi 14 avril 2012 15:47
    • Non proposé comme réponse raghupavan samedi 14 avril 2012 15:47
    • Proposé comme réponse raghupavan samedi 14 avril 2012 15:54
    •  
  • lundi 16 avril 2012 05:09
     
      A du code

    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;

    }


  • lundi 16 avril 2012 05:49
     
     

    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>



    • Modifié Ishaan Puniani lundi 16 avril 2012 17:28 mismatch in the function name provided by you
    •  
  • lundi 16 avril 2012 13:03
     
     

    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

  • lundi 16 avril 2012 15:01
     
     Réponse proposée

    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.

    • Proposé comme réponse shishek jeudi 26 avril 2012 15:50
    •  
  • lundi 16 avril 2012 19:56
     
     

    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

  • lundi 16 avril 2012 21:30
     
     Traitée

    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

    • Marqué comme réponse Pengyu Zhao dimanche 29 avril 2012 06:39
    •  
  • vendredi 20 avril 2012 07:13
     
     

    hey pallavi 

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