无法将类型为“System.Web.UI.WebControls.TextBox”的对象强制转换为“System.Web.UI.WebControls.Label”。 [英] Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.Web.UI.WebControls.Label'.

查看:93
本文介绍了无法将类型为“System.Web.UI.WebControls.TextBox”的对象强制转换为“System.Web.UI.WebControls.Label”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有将boundField加入标签...

我使用此代码将boundFied行放入文本框中,我也希望将其添加到标签中但是我收到了一个错误。

这是我的代码:



< asp:GridView ID =   GridView1 runat =  < span class =code-string> server AutoGenerateColumns =   false DataKeyNames =   CustomerID OnPageIndexChanging =   GridView1_PageIndexChanging OnRowCancelingEdit =   GridView1_RowCancelingEdit OnRowDeleting = < span class =code-string>  GridView1_RowDeleting OnRowEditing =   GridView1_RowEditing OnRowUpdating =   GridView1_RowUpdating  >  
< Columns>
< asp:BoundField DataField = CompanyName HeaderText = 公司名称 />
< asp:BoundField DataField = 地址 HeaderText = Sddress />
< asp:BoundField DataField = City HeaderText = 城市 />
< asp:BoundField DataField = PostalCode HeaderText = PostalCode />
< asp:BoundField DataField = Country HeaderText = 国家 />
< asp:CommandField ShowEditButton = true />
< asp:CommandField ShowDeleteButton = true />
< / >
< / asp:GridView >





这是c#代码:



  protected   void  GridView1_RowUpdating( object  sender,GridViewUpdateEventArgs e)
{
int Customerid = Convert.ToInt32(GridView1.DataKeys [e.RowIndex]。 Value.ToString());
GridViewRow row =(GridViewRow)GridView1.Rows [e.RowIndex];
标签lblID =(标签)row.FindControl( lblID);
TextBox textCompanyName =(TextBox)row.Cells [ 0 ]。控件[ 0 ];
TextBox textAddress =(TextBox)row.Cells [ 1 ]。控件[ 0 ];
TextBox textCity =(TextBox)row.Cells [ 2 ]。控件[ 0 ];
TextBox textPostalCode =(TextBox)row.Cells [ 2 ]。控件[ 0 ];
// string country = row.Cells [4] .Text;
标签lblCountry =(标签)row.Cells [ 4 ]。控件[ 0 ];
...
}



我在这一行收到错误:

标签lblCountry =(标签)行。细胞[4] .Controls [0]; 
任何帮助

解决方案

错误清楚地告诉你,你不能将类型A的对象转换为类型的对象B.

TextBox和Label是两个不同的控件。您将TextBox类型转换为Label,在这种情况下错误是显而易见的。

如果您想获取文本框的值并将其应用于标签,那么以下代码将有所帮助。 />

 var textBoxObj =(TextBox)row.Cells [4] .Controls [0]; 
标签lblCountry = new Label(){Text = textBoxObj.Text};


因为更新时boundfields被转换为文本框。首先,您必须将其作为文本框投射,之后您可以将其值分配给任何标签。



尝试更换

标签lblCountry =(标签)row.Cells [ 4 ]。控件[ 0 ] ; 



with

 TextBox lblCountry =(TextBox)row.Cells [ 4 ]。控制[ 0 ]; 



然后你可以得到像:

标签myLabel = 标签(); 
myLabel.Text = lblCountry.Text.Trim();


Hi,Is there any to get a boundField into a label...
I am using this code to get a boundFied row into a textbox and I want also to get the same into a Label but I am getting an error.
This is my code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="CustomerID" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                <Columns>
                    <asp:BoundField DataField="CompanyName" HeaderText="Company Name" />
                    <asp:BoundField DataField="Address" HeaderText="Sddress" />
                    <asp:BoundField DataField="City" HeaderText="City" />
                    <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" />
                    <asp:BoundField DataField="Country" HeaderText="Country" />
                    <asp:CommandField ShowEditButton="true" />
                    <asp:CommandField ShowDeleteButton="true" />
                </Columns>
            </asp:GridView>



and this the c# code:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int Customerid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            Label lblID = (Label)row.FindControl("lblID");
            TextBox textCompanyName = (TextBox)row.Cells[0].Controls[0];
            TextBox textAddress = (TextBox)row.Cells[1].Controls[0];
            TextBox textCity = (TextBox)row.Cells[2].Controls[0];
            TextBox textPostalCode = (TextBox)row.Cells[2].Controls[0];
            //string country = row.Cells[4].Text;
            Label lblCountry = (Label)row.Cells[4].Controls[0];
...
}


I am getting an error ont this line:

Label lblCountry = (Label)row.Cells[4].Controls[0];
Any help

解决方案

The error is clearly telling you that you cannot convert an object of type A to an object of type B.
TextBox and Label are two different controls. You are type casting TextBox as a Label, in which case the error is obvious.
If you want to get the value of textbox and apply it to a label, then the following code would help.

var textBoxObj = (TextBox)row.Cells[4].Controls[0];
Label lblCountry = new Label(){ Text = textBoxObj.Text };


Because on updating boundfields are converted into textboxes. First you will have to cast it as a textbox after that you can assign its value to any label.

Try replacing

Label lblCountry = (Label)row.Cells[4].Controls[0];


with

TextBox lblCountry = (TextBox )row.Cells[4].Controls[0];


Then you can have something like:

Label myLabel = new Label();
myLabel.Text = lblCountry.Text.Trim(); 


这篇关于无法将类型为“System.Web.UI.WebControls.TextBox”的对象强制转换为“System.Web.UI.WebControls.Label”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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