Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Public void GetData(){                
HtmlInputHidden hdnfld = new HtmlInputHidden();
                StringBuilder sb = new StringBuilder();
                sb.Append(@"<script type='text/javascript'>");
                sb.Append(@"var r = confirm('do you want to Checked-Out?');");
                sb.Append(@"if(r == true)");
                sb.Append(@"{");               
                hdnfld.value="1";
                sb.Append(hdnfld);
                sb.Append(@"}");
                //For cancel
                sb.Append(@"else");
                sb.Append(@"{");
                sb.Append(@"alert('before failure');");
                hdnfld.value="0";
                sb.Append(hdnfld);
                sb.Append(@"}");
                sb.Append(@"</script>");
}


What I have tried:

Public void GetData(){                
HtmlInputHidden hdnfld = new HtmlInputHidden();
                StringBuilder sb = new StringBuilder();
                sb.Append(@"<script type='text/javascript'>");
                sb.Append(@"var r = confirm('do you want to Checked-Out?');");
                sb.Append(@"if(r == true)");
                sb.Append(@"{");               
                hdnfld.value="1";
                sb.Append(hdnfld);
                sb.Append(@"}");
                //For cancel
                sb.Append(@"else");
                sb.Append(@"{");
                sb.Append(@"alert('before failure');");
                hdnfld.value="0";
                sb.Append(hdnfld);
                sb.Append(@"}");
                sb.Append(@"</script>");
}
Posted
Updated 27-Apr-16 19:59pm
v2

why you using this : sb.Append(hdnfld);
if you want value than use : sb.Append(hdnfld.value);
if you want to accept hidden field value from Javascript than use : sb.Append("document.getElementById('hdnfld')
.value");
 
Share this answer
 
v2
try this

C#
HtmlInputHidden hdnfld = new HtmlInputHidden();
           StringBuilder sb = new StringBuilder();
           sb.Append(@"<script type='text/javascript'>");
           sb.Append(@"var r = confirm('do you want to Checked-Out?');");
           sb.Append(@"if(r == true)");
           sb.Append(@"{");
           sb.Append(@"document.getElementById('hdnfld').value='1'"); 
           sb.Append(@"}");
           //For cancel
           sb.Append(@"else");
           sb.Append(@"{");
           sb.Append(@"alert('before failure');");
           sb.Append(@"document.getElementById('hdnfld').value='0'"); 
           sb.Append(@"}");
           sb.Append(@"</script>");
 
Share this answer
 
Comments
Member 12477201 28-Apr-16 3:13am    
Hi karthik, Thank you once again, after click 'ok' need to pass hidden field value to .cs file these everything should happen in form load only, is it possible?. if it is. could you send me the code.
Karthik_Mahalingam 28-Apr-16 3:19am    
you will have to define the hidden field on the aspx file. so that you can access the hidden field control easily..

<asp:HiddenField runat="server" ID="hdnfld" />
Member 12477201 28-Apr-16 3:21am    
Am explaining scenario. need to show confirmation box, if user clicks yes/ok then access x(), no/cancel then acess y() from code behind in pageload() only.
Karthik_Mahalingam 28-Apr-16 3:24am    
when to show the confirm box?
accessx() ? what is that js method or cs method?
could you please explain, i need more info
Member 12477201 28-Apr-16 3:26am    
page load only show the confirm box. after click ok in that box acess x(), these thing should happen in .cs file only.
Um.
Look at what your code is doing!
There are a number of obvious problems here: the first is that GetData doesn't do anything. You append lots of items to a StringBuilder, but as it's local to the method it's all thrown away when the method exits - so effectively your method does nothing at all!
Second, hdnfld is also local to the method, so setting it's value to anything doesn't do anything useful either, as it is also discarded when the method ends.
Thirdly, even if you send the StringBuilder content to your website at the end of the method, it's not going to do anything useful, because the hidden field you set will not be part of the page!
So have a look at the documentation: HtmlInputHidden Class (System.Web.UI.HtmlControls)[^] and see what their example does!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900