本文介绍了asp.net 2005中的转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好先生,

Hello Sir,

if (e.CommandName == "insertrecord")
        {
            string firstname = ((TextBox)(GridView1.FooterRow.FindControl("txtfirstname")).Text??string.Empty);
            string lastname = ((TextBox)(GridView1.FooterRow.FindControl("txtlastname")).Text??string.Empty);
            string nickname = ((TextBox)(GridView1.FooterRow.FindControl("txtnickname")).Text??string.Empty);
            string salary = ((TextBox)(GridView1.FooterRow.FindControl("salary")).Text??string.Empty);



先生我面临相同的错误和先生错误,例如:-



SIR I FACE SAME ERROR AND SIR ERROR LIKE THIS :-

Error   1   'System.Web.UI.Control' does not contain a definition for 'Text'    C:\Documents and Settings\Harry\My Documents\Visual Studio 2005\example6\Default.aspx.cs    45  92  C:\...\example6\



先生PLS我无法执行此代码PLS向我发送了很好的解决方案,我可以很好地执行该操作.

谢谢
SIR



SIR PLS I CANT EXECUTE THIS CODE PLS SEND ME GOOD SOLUTION FOR THIS..WHICH I CAN EXECUTE VERY WELL

THANK YOU
SIR

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
   {
       if (e.CommandName == "insertrecord")
       {
           string firstname = (GridView1.FooterRow.FindControl("txtfirstname")).ToString() as TextBox;
           string lastname = (GridView1.FooterRow.FindControl("txtlastname")).ToString() as TextBox;
           string nickname = (GridView1.FooterRow.FindControl("txtnickname")).ToString() as TextBox;
           string salary = (GridView1.FooterRow.FindControl("salary")).ToString() as TextBox;
 
           SqlCommand comm = new SqlCommand();
           comm.CommandText = "insert into tbluser (firstname,lastname,nickname,salary) values(@firstname,@lastname,@nickname,@salary)";
           comm.Connection = conn;
 
           comm.Parameters.AddWithValue("@firstname", firstname.ToString());
           comm.Parameters.AddWithValue("@lastname", lastname.ToString());
           comm.Parameters.AddWithValue("@nickname", nickname.ToString());
           comm.Parameters.AddWithValue("@salary", salary.ToString());
 
           conn.Open();
           comm.ExecuteNonQuery();
           conn.Close();
 
           LoadGridView();
 
       }

执行代码时出现错误:
收合|复制代码
1无法通过内置转换C:\ Documents and Settings \ Harry \ My Documents \ Visual Studio 2005 \ example6 \ Default.aspx将类型``string''转换为``System.Web.UI.WebControls.TextBox'' .cs 45 32 C:\ ... \ example6 \
请帮我解决问题.

谢谢.

When I execute the code I get the error:
Collapse | Copy Code
1Cannot convert type ''string'' to ''System.Web.UI.WebControls.TextBox'' via a built-in conversionC:\Documents and Settings\Harry\My Documents\Visual Studio 2005\example6\Default.aspx.cs4532C:\...\example6\
Please help me solve the problem.

Thank you.

推荐答案

string value = (GridView1.FooterRow.FindControl("txtValue") as TextBox).Text;


另外,如果对象已经是字符串,则无需使用ToString .

祝你好运!


Also, you don''t need to use ToString if the object already is a string.

Good luck!


这篇关于asp.net 2005中的转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 12:23