单击确定,然后隐藏值应为1,但它显示为0。 [英] Click ok then hidden value should be 1 , but it is display 0.

查看:61
本文介绍了单击确定,然后隐藏值应为1,但它显示为0。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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>");
}

推荐答案

为什么使用这个:sb.Append(hdnfld);

if你想要价值而不是使用:sb.Append(hdnfld.value);

如果你想接受来自Javascript的隐藏字段值而不是使用:sb.Append(" document.getElementById('hdnfld')

.value");
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");


试试这个



try this

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>");


嗯。

看看你的代码在做什么!

有这里有一些明显的问题:第一个是 GetData 什么都不做。你将大量的项目附加到StringBuilder,但由于它是方法的本地项目,当方法退出时它们都会被丢弃 - 所以你的方法实际上什么都不做!

其次, hdnfld 也是方法的本地,所以将它的值设置为任何东西也没有任何用处,因为当方法结束时它也被丢弃。

第三,甚至如果你在方法结束时将StringBuilder内容发送到你的网站,它不会做任何有用的事情,因为你设置的隐藏字段不会是页面的一部分!

所以看看在文档: HtmlInputHidden类(系统。 Web.UI.HtmlControls) [ ^ ]并查看他们的示例!
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!


这篇关于单击确定,然后隐藏值应为1,但它显示为0。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆