将空白文本框转换为int值0 [英] convert blank textbox into int value 0

查看:83
本文介绍了将空白文本框转换为int值0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在提交按钮内有一个文本类型为double的文本框.单击按钮后,我想将空白文本框转换为int值0.
为此,我做了一些事情,但如何在该框中设置0 int值却没有得到.
请在下面发送代码,请检查一下,如果有任何建议,请发送给我.

i am having a textbox with datatype double,inside the submit button.After clicking on the button,i want to convert blank textbox into int value 0.
for that i did something but how to set 0 int value in that box not getting.
below sending the code pls check it out and if any suggestion is there pls send me.

protected void btnsubmit2_Click(object sender, ImageClickEventArgs e)
{
    double stotal_recpt2;  
    if(stotal_recpt2 = "")
    {
        stotal_recpt2.text = "0";
    } 
    else
    {
        Convert.ToDouble(txttotal_recpt2.Text.Trim());
    }
}



文本框为double,我正在发送字符串值,确定存在错误,
请帮助我如何将空白文本框转换为0值.



the textbox is double and i am sending string value ok there is a bug,
pls help me how blank textbox convert to 0 value.

thanks.

推荐答案

您的代码似乎是相当随机的.您创建未初始化的变量double stotal_recpt2;,然后尝试测试它是否包含文本值"".然后,您尝试设置该对象的text属性,该属性不存在.然后在您的else子句中,将一些文本值转换为double并立即将其丢弃.
Your code seems to be rather random. You create the uninitialised variable double stotal_recpt2; and then try to test if it contains the text value "". You then try to set a text property of that object which does not exist. And in your else clause you convert some text value to a double and immediately throw it away.


我有一个文本类型为double的文本框"

不,我怕你不这样做.文本框包含的字符串不为双精度.正如Richard所说,您的代码中有很多错误.但要回答您的问题:

"i am having a textbox with datatype double"

No I am afraid that you do not. Textbox''s contain strings not doubles. As Richard said, there is plenty of errors in your code. But to answer your question:

protected void btnsubmit2_Click(object sender, ImageClickEventArgs e)
{
    double stotal_recpt2;
    if (string.IsNullOrEmpty(txttotal_recpt2.Text)
    {
        stotal_rectp2 = 0;
    }

    // Do something with stotal_recpt2;
    
}


首先让我们假设您有一点错字,并且您有一个名为txtRecpt的文本框.我提出以下(也是最安全的imo)解决方案:

Lets first assume you have a little typo, and that you have a textbox called txtRecpt. I propose the following (and safest, imo) solution:

double stotal_recpt2 = 0;
double.TryParse(txtRecpt.Text, out stotal_recpt2);

//Do what you need to with stotal_recpt2  


做您需要做的事情
希望这会有所帮助,

--Dom



Hope this helps,

--Dom


这篇关于将空白文本框转换为int值0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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