字符串"不包含“文本"的定义 [英] string' does not contain a definition for 'Text'

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

问题描述

当我执行您的代码时,文本属性未配置

照你说的撒谎

 受保护的 无效 Button1_Click(对象发​​件人,EventArgs e)
    {
         foreach (GridViewRow行 in  GridView1.Rows中的行)
        {
            CheckBox chk =((CheckBox)(row.FindControl(" ))));;

            
                如果(已选中)
                {
                    字符串 lblid = row.FindControl("  span>) as 标签;
                    字符串 lblid = lblid.

                    SqlCommand comm =  SqlCommand();
                    comm.CommandText = " ;
                    comm.Connection = conn;

                    comm.Parameters.AddWithValue("  int  .Parse(lblid.ToString()));
                 
// 实际上,我想要lblid.Text,但是thr是要转换的问题
                    conn.Open();

                    comm.ExecuteNonQuery();

                    conn.Close();
                }
            }

            LoadGridView();
        } 




我该怎么办

谢谢

解决方案

string lblid = row.FindControl("lblid") as Label;



标签不能转换为字符串. lblid将为空.

string lblid = lblid.


WTF!

int.Parse(lblid.ToString())



当然,由于lblid为null,因此将无法正常工作.如果该值不是数字,则它也将不起作用.最好使用TryParse




根据您的代码,您正在查找标签,并将其存储为字符串.

字符串没有text属性.

而是将其存储为标签,然后访问它的文本属性,将其存储在字符串中.

标签lblid = row.FindControl(" )作为标签;
                    字符串 strlblid = lblid.Text 



这应该有帮助.

*如果可以解决您的查询,请标记为答案.


再次为字符串分配标签...

用这种方式做

标签lblid =((Label)(row.FindControl("lblid")));

然后使用lblid.Text属性获取字符串值

Text property not configure when i execute your code

lie this as per you say

  protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chk = ((CheckBox)(row.FindControl("chkdelete")));

            
                if(chk.Checked)
                {
                    string lblid = row.FindControl("lblid") as Label;
                    string lblid = lblid.

                    SqlCommand comm =new SqlCommand();
                    comm.CommandText = "delete from tbluser where id=@id";
                    comm.Connection = conn;

                    comm.Parameters.AddWithValue("@id",int.Parse(lblid.ToString()));
                 
//ACTUALY I WANT lblid.Text but thr is problem to convert
                    conn.Open();

                    comm.ExecuteNonQuery();

                    conn.Close();
                }
            }

            LoadGridView();
        }




what should i do

thank you

解决方案

string lblid = row.FindControl("lblid") as Label;



A Label can''t be converted to a string. lblid will be null.

string lblid = lblid.


WTF!

int.Parse(lblid.ToString())



Of course since lblid is null this won''t work. It will also not work if the value is not a number. It would be better to use TryParse


Hi,

As per your code, you''re finding a label, and storing it as a string.

String does not have a text property.

Instead, store it as a label, and then access it''s text property store that in a string.

Label lblid = row.FindControl("lblid") as Label;
                    string strlblid = lblid.Text



This should help.

*Mark as answer if this solves your query.


again you are assigning a label to string...

do it this way

Label lblid = ((Label)(row.FindControl("lblid")));

and then use lblid.Text property to get string value


这篇关于字符串"不包含“文本"的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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