Setting a value to a HiddenField in ASP.NET 4.5 - c#

I'm having some issues setting a value to a HiddenField in ASP.NET 4.5.
From what I've seen I've tried the following without any luck:
In ASPX:
<asp:HiddenField ID="HiddenField" runat="server" value="" />
<script type="text/javascript">
function SetHiddenField() {
var vv = "HELLO WORLD";
document.getElementById('<%=HiddenField.ClientID%>').value = vv;
}
</script>
In code-behind:
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "SetHiddenField", "SetHiddenField();", true);
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('" + HiddenField.ClientID + "');", true);
This alerts garbage in the ClientID.
The other solution I've tried is the following.
In .ASPX:
<asp:HiddenField ID="HiddenField" runat="server" value="" />
<script type="text/javascript">
function SetHiddenField() {
var vv = "HELLO WORLD";
document.getElementById('HiddenField').value = vv;
}
</script>
One issue here is that .value does not exist in the IntelliSense, only .ValueOf.
In code-behind:
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "SetHiddenField", "SetHiddenField();", true);
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('" + HiddenField.Value + "');", true);
Nothing happens, probably an error in the JavaScript, since no alert is shown.
Can anyone point me to the right direction, please?

Your first markup is good:
<asp:HiddenField ID="HiddenField" runat="server" value="" />
<script type="text/javascript">
function SetHiddenField() {
var vv = "HELLO WORLD";
document.getElementById('<%=HiddenField.ClientID%>').value = vv;
}
</script>
Change the code to this (check the second line):
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "SetHiddenField", "SetHiddenField();", true);
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert(document.getElementById('" + HiddenField.ClientID + "').value);", true);
And the output should be like this:
EDIT : In your scenario, you can run javascript to get a value and force a postback to use the value in your code. I would change my markup to this:
<script type="text/javascript">
function SetHiddenField() {
var vv = "HELLO WORLD";
document.getElementById('<%=HiddenField.ClientID%>').value = vv;
__doPostBack('<%=HiddenField.ClientID%>', '')
}
</script>
And in code my Page_Load is like below:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Register JavaScript which will collect the value and assign to HiddenField and trigger a postback
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "SetHiddenField", "SetHiddenField();", true);
}
else
{
//Also, I would add other checking to make sure that this is posted back by our script
string ControlID = string.Empty;
if (!String.IsNullOrEmpty(Request.Form["__EVENTTARGET"]))
{
ControlID = Request.Form["__EVENTTARGET"];
}
if (ControlID == HiddenField.ClientID)
{
//On postback do our operation
string myVal = HiddenField.Value;
//etc...
}
}
}

In the hidden field tag add clientid static like this -
<asp:HiddenField ID="HiddenField" runat="server" value="" ClientIDMode="Static" />
This way ASP.Net will not replace it with dynamic ID and always have the id that you provided, so it will now have ID HiddenField. Then your second attempt should work.
More can be found here -
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode(v=vs.110).aspx

Related

Get value from html side controls and pass it to server side controls

I have a question about server side and html side controls.
This is my code
ClientScript.RegisterStartupScript(this.GetType(),
"Enter Id",
" prompt('Enter your Id ....');",
true);
I want to get value that user entered?
What should I do?
Try this way
<form id="theform" runat="server">
<input type="hidden" id="hidValue" runat="server" />
</form>
Script for get value from hidden field
<script type="text/javascript">
function storeinput(id) {
document.getElementById("<%=hidValue.ClientID%>").value = id;
}
</script>
ClientScript.RegisterStartupScript(this.GetType(), "prompt", "var id = prompt('Enter your Id .'); storeinput(id);", true);
Please try below:
Please define hidden variable in ASPX page:
<input type="hidden" id="hidValue" runat="server" />
Please write below code at CodeBehind:
ClientScript.RegisterStartupScript(this.GetType(), "prompt", "document.getElementById('" + hidValue.ClientID + "').value = prompt('Enter your Id .'); alert(document.getElementById('" + hidValue.ClientID + "').value); ", true);

jQuery(function ($) does not work after partial update in updatepanel

<script type="text/javascript">
//http://digitalbush.com/projects/masked-input-plugin/
jQuery(function ($) {
//phone numbers
alert("test");
var txtInvestContactPhone = $("#<%=txtInvestContactPhone.ClientID%>");
$(txtInvestContactPhone).mask("(999) 999-9999");
});
</script>
i tried adding the pagerequestmanager to the content of the page, but i think i'm doing it wrong:
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add("jQuery(function ($)");
</script>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="true">
</asp:ToolkitScriptManager>
i've also tried to registerStartUp Script in my page_load event like so:
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), "jQuery(function ($);", true);
if (!Page.IsPostBack)
{
but this just breaks my accordion causing the panels to all be unclickable.
i think i'm on the right path here, just need some help with syntax.
jQuery(function ($); isn't a function that can be called, that's like... half a function.
Put your init code in it's own function definition, then call it from your DOM ready handler, and from your startup script.
function init() {
//phone numbers
alert("test");
var txtInvestContactPhone = jQuery("#<%=txtInvestContactPhone.ClientID%>");
jQuery(txtInvestContactPhone).mask("(999) 999-9999");
});
jQuery(function () { init(); });
...
ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), "init();", true);

Need to access variable from code behind. What i found shows how to go about it but i but the value keep displaying null

How to access and use javascript variables from code behind (C#). Upon research, i implemented the proccess in accessing the variables but i still do not get the values of the variables. Where am i going wrong?
//Javascript
<script type="text/javascript">
function OnSucceeded() {
var status = 65;
var state = "pass value"
document.getElementById("getValue").value = status;
document.getElementById("getvalues").value = state;
}
</script>
//
<asp:HiddenField ID="getValue" runat="server" Value="" />
<asp:HiddenField ID="getvalues" runat="server" Value="" />
<asp:Button ID="Button1" runat="server" OnClientClick="OnSucceeded()" Text="Button"
OnClick="Button1_Click" />
//C# code behind
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + getvalues.Value + "');", true);
Use ClientID to render correct IDs of server controls
<script type="text/javascript">
function OnSucceeded() {
var status = 65;
var state = "pass value"
document.getElementById("<%= getValue.ClientID %>").value = status;
document.getElementById("<%= getvalues.ClientID %>").value = state;
}
</script>

How to call javascript on user control

I have webapplication in c# asp.net 4.0
I have User Control in that I have written javascript :-
<script>
function SetValue() {
alert(document.getElementById('offSetClient').value);
}
</script>
<asp:HiddenField ID="clientDateTime" runat="server" />
<asp:HiddenField ID="offSetClient" runat="server" Value="Test"/>
and this User Control added into a web form.
Please suggest me how can I call this javascript on User Control page load.
You can use Page.ClientScript.RegisterStartupScript to attain this.
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(GetType(), "ShowAlert", "SetValue('" + offSetClient.ClientID + "');", true);
}
Please add an input parameter for the function to get the object of hidden field and pass the same from function.
<script type="text/javascript">
function SetValue(objHdn) {
alert(document.getElementById(objHdn).value);
}
</script>

Retrieve selected tags from the Jquery Tag it! plugin on PostBack

I've found this nice jquery plugin tag it! http://levycarneiro.com/2010/03/tag-it-tag-suggestions-editor-and-autocomplete-in-a-jquery-ui-plugin/ and want to implement it into an ASP.Net application.
Upon inspecting the source code, I found out, that the plugin adds additional li items (with remove links and so on) into an ul.
How can I retrieve the selected tags upon a PostBack?
#citronas, i used this jQuery tag plugin: jQuery Tagit
I modified it as follows to both load the plugin with tags from the server side and retrieve selected tags on the server side.
...<script>
$(function () {
var availableTags = $("#<%= hdnDBTags.ClientID %>").val().split(',');
$('#demo1').tagit({ tagSource: availableTags, select: true });
$("#<%= btnGetTags.ClientID %>").click(function () {
getTagsString($('#demo1').tagit('tags'))
});
function getTagsString(tags) {
var string = "";
for (var i in tags) {
string += tags[i] + ",";
}
$("#<%= hdnSelectedTags.ClientID %>").val(string);
}
});
</script>
<asp:HiddenField ID="hdnDBTags" runat="server" />
<asp:HiddenField ID="hdnSelectedTags" runat="server" />
<h1>
Your Profile</h1>
<p>
<ul id="demo1" name="nameOfSelect">
</ul>
<asp:Button ID="btnGetTags" runat="server" Text="Get Tags" OnClick="btnGetTags_Click" />
</p>
And in the code behind:
protected void Page_Load(object sender, EventArgs e)
{
hdnDBTags.Value = "real_estate,mortgage_lending";
}
protected void btnGetTags_Click(object sender, EventArgs e)
{
string test = hdnSelectedTags.Value;
IList<string> array = test.Split(',').ToList();
array.Remove("");
}
Hope this helps...
D

Categories