文本框包含空值 [英] Textbox contain null value

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

问题描述

你好,



我有一个首先禁用的文本框,两个按钮一个是编辑,另一个是完成,它在开始时是不可见的



在文本框中,当我单击编辑按钮然后文本框启用和完成按钮可见时,我已经有了一些文本。所以我更改了文本框的文本并单击完成按钮



其中包含以下代码



string suggtxt = textbox.Text;



但它包含null。为什么?在某些事件发生后,是否有变化没有反映或变化会反映出来?如果是的话点击完成按钮也是一个事件。



我该怎么办才能获得文本框编辑的价值



我的代码是:



Hello,

I have one textbox which is disable first and two buttons one is Edit and other is Done which is invisible at start

In textbox I already have some text when I click on Edit button then Textbox get enable and Done button is visible. So I change the text of Textbox and click on Done button

which contain following code

string suggtxt = textbox.Text;

but it contain null. why? Is there changes are not reflected or chages will reflect after some event? if yes then Click on Done button is also a one event.

what should i do to get textbox edited value

my code is:

protected void btnDone_Command(object sender, CommandEventArgs e)
{
int index = 0;
string suggtxt = "";
if (int.TryParse(e.CommandArgument.ToString(), out index))
{
TextBox txt = (TextBox)GridViewUserScraps.Rows[index].FindControl("suggText");


suggtxt = txt.Text;




}
}

it is a webform application





我尝试过:



我有一个首先禁用的文本框和两个按钮,一个是编辑,另一个是完成,它在开始时是不可见的



在文本框中我已经有了一些文本当我单击编辑按钮,然后文本框启用,完成按钮可见。所以我更改了文本框的文本并单击完成按钮



其中包含以下代码



string suggtxt = textbox.Text;



但它包含null。为什么?

是否有变化没有反映或者某些事件后chages会反映出来?如果是,那么点击完成按钮也是一个事件。



我该怎么办才能获得文本框编辑的值



What I have tried:

I have one textbox which is disable first and two buttons one is Edit and other is Done which is invisible at start

In textbox I already have some text when I click on Edit button then Textbox get enable and Done button is visible. So I change the text of Textbox and click on Done button

which contain following code

string suggtxt = textbox.Text;

but it contain null. why?
Is there changes are not reflected or chages will reflect after some event? if yes then Click on Done button is also a one event.

what should i do to get textbox edited value

推荐答案

其中包含以下代码

"which contain following code
string suggtxt = textbox.Text;



但它包含null。为什么?



嗯......它没有。 Text属性永远不会返回 null - 它可以返回一个空字符串,但不能返回 null

发生的事情是故障在早期的代码中:


but it contain null. why?"

Well...it doesn't. The Text property never returns null - it can return an empty string, but not null
What's happening is that the fault is in the earlier code:

TextBox txt = (TextBox)GridViewUserScraps.Rows[index].FindControl("suggText");

由于某种原因,返回null - 这意味着FindControl无法找到任何用这个名称控制。

这可能是因为拼写错误,索引错误(可能因为CommandArgument字符串错误)或几乎其他任何东西!

和问题是我们无法分辨,因为它需要你的整个代码才能达到这一点。

所以...从调试器开始。在方法的第一行放置一个断点,然后开始查看究竟发生了什么。看看变量是什么。想想他们应该是什么。检查GridViewRow的内容。通过一些仔细的检查,这应该是相当明显的。



但是我们不能为你做任何事情 - 我们无法运行你的整个代码,该片段在孤立中是无用的,因为它依赖于你的其余代码和数据来设置所有内容!

For some reason, that is returning null - which means that FindControl cannot find any control with that name.
This could be because you spelled it wrong, the index is wrong (possibly because the CommandArgument string is wrong) or almost anything else!
And the problem is that we can't tell, because it takes the whole of your code to get to that point.
So...start with the debugger. put a breakpoint on the first line of the method, and start looking at exactly what is going on. Look at what is in variables. Think about what they should be. Examine the content of your GridViewRow. It should be reasonably obvious with a bit of close inspection.

But we can't do any of that for you - we can't run your whole code, and that fragment is useless in isolation because it relies on the rest of your code and data to set everything up!


嘿。我认为您需要提交gridview更改。请尝试使用以下代码



Hey. I think you need to commit your gridview changes. Please try to use below code

void dataGridView1_CurrentCellDirtyStateChanged(object sender,
    EventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}


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

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