set a Hidden control in client side using Jquery and retrieve it in code behind
-
2012年4月14日 1:09
Is it possible to set a Hidden control in client side using JQuery and retrieve it in code behind?
Thanks,
Pallavi
Pallavi Sharma
全部回复
-
2012年4月14日 13:23
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)
- 已建议为答案 Ishaan Puniani 2012年4月14日 13:24
-
2012年4月14日 14:07
Hi Pallavi,
if you are using the SharePoint 2010, can explore client object model, refer the below link
or
you can use the codeplex solution to hide or to make read only, refer below
- 已建议为答案 raghupavan 2012年4月14日 15:47
- 取消建议作为答案 raghupavan 2012年4月14日 15:47
- 已建议为答案 raghupavan 2012年4月14日 15:54
-
2012年4月16日 5:09
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;
}
- 已编辑 Pallavi Sharma 2012年4月16日 5:09
-
2012年4月16日 5: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>
- 已编辑 Ishaan Puniani 2012年4月16日 17:28 mismatch in the function name provided by you
-
2012年4月16日 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
-
2012年4月16日 15:01
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.
- 已建议为答案 shishek 2012年4月26日 15:50
-
2012年4月16日 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
-
2012年4月16日 21:30
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
- 已标记为答案 Pengyu Zhao 2012年4月29日 6:39
-
2012年4月20日 7:13
hey pallavi
dd you find any issue or any other alternative..??

