开发者

Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.Web.UI.WebControl

开发者 https://www.devze.com 2023-02-23 08:45 出处:网络
This is what my aspx page looks like: <asp:TemplateField HeaderText=\"Detail\" HeaderStyle-ForeColor=\"DimGray\" >

This is what my aspx page looks like:

<asp:TemplateField HeaderText="Detail" HeaderStyle-ForeColor="DimGray" >
  <ItemTemplate>
    <asp:Label ID="labeldetail" runat="server" Text='Label' ></asp:Label>
  </ItemTemplate>
</asp:TemplateField>

In aspx.cs, adding values to grid:

 public void Addtogrid()
 {
    string ctrlStr = String.Empty;

    foreach (string ctl 开发者_开发知识库in Page.Request.Form)
    {  
       if (ctl.Contains("Longitude"))
       {
          ctrlStr = ctl.ToString();
          Disctrict.School[0].Student[j].Activity[k].Sport = Request.Form[ctrlStr];
       }
    }
 }

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
     Label lbl7 = (Label)e.Row.FindControl("labeldetail");
     if (lbl7 != null)
     {
       lbl7.Text =  Disctrict.School[0].Student[j].Activity[[e.Row.RowIndex].Sport ;
     }
   }
 }

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
 {
    foreach (Control ctrl in ControlsPanel.Controls)
    {
      if (ctrl.ClientID.Contains("Sport"))
      {
        ((TextBox)ctrl).Text = Disctrict.School[0].Student[0].Activity[x].Sport // <--- Getting error here....
      }
    }
 }

I don't understand what am I missing. There are 16 other items that I have in the same GridView and everything works just perfectly fine.


It looks like one of the controls contained in the ControlsPanel that you are iterating over is not of type TextBox. Based on your error it seems to be of type Label.

You can check the type before attempting the cast like so:

if (ctrl is TextBox)
{
   ((TextBox)ctrl).Text = Disctrict.School[0].Student[0].Activity[x].Sport
}
else
{
   // you can do something else, or ignore the control if it is not a text box
}

In general it is good practice to test for type before attempting a cast like that unless you know for sure the object is of the type you are casting to.

A method that I find useful sometimes is to us the as keyword to try to cast the object, then compare for null. Doing it this way will cause the variable to be set to null if the object does not implement the interface, or can not be cast to the type you are trying to cast it to.

TextBox txtBox = ctrl as TextBox;   
// now txtBox will either be set to an instance or will be null
if (txtBox != null)
{
    // this means the cast worked..
}

Be aware though that the as keyword may be slower than an explicit cast so always consider how often the cast will be performed and under what circumstances!


It is exactly as the error says: You are trying to cast a label to a textbox, change it to:

TextBox txt = (ctrl as TextBox);
if ((ctrl as TextBox) != null)
    (ctrl as TextBox).Text = Disctrict.School[0].Student[0].Activity[x].Sport;
else if ((ctrl as Label) != null)
    (ctrl as Label).Text = Disctrict.School[0].Student[0].Activity[x].Sport;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号