I am using the a hidden controls in my page and assign value to the hidden control using javascript. Now i want to get the hidden control value in code behind. It always return the null value. Please help me to solve it.
My partial code is here.
<asp:HiddenField ID="Hfproductid" runat="server" />
var hfproductid = document.getElementById('<%= Hfproductid.ClientID %>');
prod_id=100;
hfproductid .innerHTML = prod_id;
In My Code Behind
------------------
string invid = HfInvoices.Value; //invid =""; always
You must set the value of hidden field, not innerHTML.
hfproductid.value = prod_id;
You can directly set value by
if (!Page.IsPostBack)
{
document.getElementById('<%= Hfproductid.ClientID %>').value = "100";
}
Then show the value by
alert(document.getElementById('<%= Hfproductid.ClientID %>').value)
It is beacause your page is posting back.
Thats why on clientside you may get its value through javascript but on serverside, page is posting back.
Try to use IsPostback Property.
if(!Page.IsPostback)
Refer:
http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx
var hdnvalue= document.getElementById('<%= HiddenValue1.ClientID %>');
myvalue=5;
hdnvalue.val(myvalue);alert(hdnvalue.val());
try the following code:
var hfproductid = document.getElementById('<%= Hfproductid.ClientID %>');
prod_id=100;
hfproductid .val(prod_id);
alert(hfproductid .val());
Hope this will work for you
Updated:
Then on post back your page is being refreshed:
Add
if(!IsPostback){
String test = Hdftest.value;
}
Try this
what juanreyesv mentioned earlier is absolutly correct,If you change the value with javascript you have to post back the information to the server, if not you are not going to see the changes in the "code behind"
For that you have to force tha postback event in clientside itself by using
__doPostBack();
So your code will be,
var hfproductid = document.getElementById('<%= Hfproductid.ClientID %>');
var prod_id = 100;
hfproductid.value = prod_id;
__doPostBack();
And then in Page ispostback you will get the desired value
if(IsPostback){
string invid = Hfproductid.Value;
}
The above code will work fine but since it is a forced postback without any condition it will get fired continuously so to avoid that you have to take 1 hidden field and use it as a flag to check if there is any value then dont fire the
_doPostBack()
So your final code should look like
if (document.getElementById('<%= hdncheck.ClientID %>').value == "Y") {
var hfproductid = document.getElementById('<%= Hfproductid.ClientID %>');
var prod_id = 100;
hfproductid.value = prod_id;
document.getElementById('<%= hdncheck.ClientID %>').value = "N";
__doPostBack();
}
In server side
if (!IsPostBack)
{
hdncheck.Value = "Y";
}
if (IsPostBack)
{
string invid = Hfproductid.Value;
}
Related
I am setting the hidden filed value through JavaScript as below
<script lang="JavaScript" type="text/javascript">
function ChangeVal()
{
var elem = document.getElementById("btnDownloadStream");
if (elem.value == "Start")
{
elem.value = "Stop";
document.getElementById('myHiddenInput').value = "1";
}
else
{
document.getElementById('myHiddenInput').value = "0";
elem.value = "Start";
}
}
I am trying to get hidden field value in code behind. My code is
HiddenField myHiddenInput = (HiddenField)Page.FindControl("myHiddenInput");
var val = myHiddenInput.Value;
Before this line I am calling one function which creates and generates the GetResponseStream(). While doing this I am not able to get the value from server controls. Why?
Becuase Changing the value in javascript will not affect the server side value.
if you want to change a server side value from javascript: You can try the following
// Javascript
var myHidden = document.getElementById("<%:myHiddenId.ClientId%>");
myHidden.value = myJSVariable;
Make sure the myHidden is a server control.
Set Runat="server" attribute to your hidden field as shown below :
<input type="hidden" value="" id="myHiddenInput" runat="server" />
Then update your javascript function as shown below :
function ChangeVal() {
var elem = document.getElementById("btnDownloadStream");
if (elem.value == "Start") {
elem.value = "Stop";
document.getElementById('<%=myHiddenInput.ClientID%>').value = "1";
}
else {
document.getElementById('<%=myHiddenInput.ClientID%>').value = "0";
elem.value = "Start";
}
}
now you can directly access your hidden field value in your code behind without using Page.FindControl as mentioned :
var val = this.myHiddenInput.Value;
Update:
One thing I have noticed that your button is server side button and in your javascript you call
var elem = document.getElementById("btnDownloadStream");
I think it should be
var elem = document.getElementById('<%=btnDownloadStream.ClientId%>')
otherwise you will always get the value of else part
Make sure this is not the case.
well #Vishweshwar Kapse is answered your question.
Place your hidden field in update panel and don't forget to add click event of button in trigger.
This also happens in case you use the UpdatePannel in your page. if this is the case then place the HiddenField inside the UpdatePannel and try again.
You forget about ViewState. If you change data in the hidden field using java script code the ViewState do not get this changes and that's why you cannot get the correct value in code behind.
Make sure your hidden field have runat="server" attribute..
So I have a text box, where I add an onchange event of markAsException.
My javascript is -
function markAsException(recordID) {
//alert("Exception");
//mark exception column
document.getElementById("ctl00_cpMain_lblScrollException_" + recordID).innerText = "Exception";
document.getElementById("ctl00_cpMain_lblScrollException_" + recordID).style.color = "#FF0000";
document.getElementById("ctl00_cpMain_tdScrollException_" + recordID).style.backgroundColor = "#99CCFF";
//enable comments ddl and remove blank (first item)
document.getElementById("ctl00_cpMain_ddlCommentId_" + recordID).disabled = false;
document.getElementById("ctl00_cpMain_ddlCommentId_" + recordID).focus();
document.getElementById("ctl00_cpMain_ddlCommentId_" + recordID).options[0] = null;
}
What I want to do is, when a user changes the value in a textbox, to mark a column as "Exception", and then focus a drop down list where they have to chose the reason for the exception.
This is what happens.. If I am on that text box and change it, then tab, it tabs to the drop down list.
However, if I change the value and then simply click in another text box on the form, I don't focus the drop down list.
How would I accomplish that?
I would suggest using JQuery's change() function.
The advantage is that it will be more stable across different browsers.
Something like:
$('#<%= TextBox1.ClientID %>').change(function(){
// extract the recordID from the textbox - perhaps with an attribute?
markAsException(recordID);
});
My comment was getting longer so I'm expanding:
Take a look at the JQuery documentation for setting this up as the page finishes loading. If tb is the ID of your textbox your selector would be
$('#<%= tb.ClientID %>')
I would suggest you replace your code and use
tb.Attributes.Add("recordID", recordId.ToString());
This will add the ID you need onto the textbox tag. Once you're in the function I outlined above you can use the following selector to get the recordID in javascript
var recordID = $('#<%= TextBox1.ClientID %>').attr('recordID');
All together
$(document.ready(function(){
$('#<%= tb.ClientID %>').change(function(){
var recordID = $('#<%= tb.ClientID %>').attr('recordID');
if(recordID){
markAsException(recordID);
}
});
});
Can I access server control values in aspx design page.
For now being its just my curiosity but may be in need on tomorrow or so.
For example I have a label in aspx page and I want to assign a textbox value to it.
I know I can use aspx.cs file instead but any desired suggestion will be highly appreciable.
Thanks in advance...... :)
There are many ways.
1) var txtbox = Document.getElementbyId("<%= texboxId.ClientID%>"); txtbox.value = "new";
2) var txtbox = $("#" + "<%= texboxId.ClientID%>"); txtbox.value = "new";
3) var txtbox = $get("texboxId"); txtbox.value = "new";
If you are using Asp.Net 4.0 then the easy way is
<asp:TextBox runat="server" ID="txtboxId" ClientIDMode="Static" />
and access it directly without using ClientID.
1) var txtbox = Document.getElementbyId("txtboxId"); txtbox.value = "new";
2) var txtbox = $("#txtboxId"); txtbox.value = "new";
For number 2 you need Jquery and for number 3 you need Microsoft AJAX library.
In our project we are deleting something after the user left the page. We are using window.unload event for doing this.
window.onunload = function() {
// delete something
}
We are generally using buttons, linkbuttons..etc in UpdatePanel so we hadn't needed to check Page.IsPostBack property.
Today we realized that we used some buttons out of UpdatePanel and this situation had produced some errors. After that we decided to change our method, defined a global variable (var _isPostBack = false), at the top of the our page and:
window.onunload = function() {
if (_isPostBack) {
_isPostBack = false;
return;
}
// delete something
}
Altought i set the g_isPostBack in Page_Load, g_isPostBack didn't change. I tried "RegisterClientScriptBlock", "RegisterOnSubmitStatement" and "RegisterStartupScript" methods. Register methods were called before the onunload event but _isPostBack was set after onunload event had triggered...
if (IsPostBack)
{
Control c = MyClass.GetPostBackControl(this);
bool inUpdatePanel = ControlParentForUpdatePanel(c);
if (!inUpdatePanel)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), Guid.NewGuid().ToString(), "_isPostBack = true;", true);
}
}
Is there anyone to help me?
that's the trick...
if you add onsubmit attribute to your form tag:
<form id="form1" onsubmit="return yourPostBack()">
and than write your own function:
function yourPostBack()
{
_isPostBack = true;
return true;
}
and finally in the page load:
if (IsPostBack)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), Guid.NewGuid().ToString(), "_isPostBack = false;", true);
}
with this method you can understand that it is postback or not, in window.onunload
I hope i am on the right track here,
As i understand,
the OnUnload() is ClientSide,
and therefore you don't have the server objects
what you can do... is save the value in a hidden field.
As i am used to PHP you can even embed the value in a Javascript variable
Dont know if this applys to ASP.NET:
<script language="javascript">
var MyServerVariable = "<?PHP echo MyServerVariable ?>"
if(MyServerVariable == "Blah...")
{
}
</script>
translates to
<script language="javascript">
var MyServerVariable = "VALUE"
if(MyServerVariable == "Blah...")
{
}
</script>
But same thing can be done with <asp:Label /> , i am sure...
I need to detect a postback in the frontend so I can use it with JQuery to change a class on page load. How can I do this?
You can check the IsPostBack property. Eg:
<script type="text/javascript">
$(function()
{
var isPostBack = <%=Page.IsPostBack.ToString().ToLower()%>;
if (isPostBack)
{
alert("Postback");
}
});
</script>
Stolen from this post:
On the server side have this
if(IsPostBack)
{
// NOTE: the following uses an overload of RegisterClientScriptBlock()
// that will surround our string with the needed script tags
ClientScript.RegisterClientScriptBlock(GetType(), "IsPostBack", "var isPostBack = true;", true);
}
On client side this
if(isPostBack) {
// do your thing
}
I put this variable inside the header tag of my asp.net web forms page.
<script type="text/javascript">
var isPostBack = ("true"==="<%= Page.IsPostBack ? "true" : "false" %>");
</script>
The var contains a Boolean. The comparison can probably be shortened.
Simple:
if you're using jquery it has to go after(jquery goes nuts otherwise):
$(document).ready(function(){
});
var isPostBack = <%=Convert.ToString(Page.IsPostBack).ToLower()%>;
Then
function whatever(){
if (isPostBack){
//Whatever you want to do
}else{
//Whatever else you want to do
}
}
I'm actually using it with jquery to show a web service status box then force a postback to refresh a ListView, so when it posts back it doesn't invoke the web service or show the status box just the updated ListView data.
$("a[href^='javascript:__doPostBack']").click(function () {
// do something
});