Casting to a custom control

fizzled

Regular
Joined
Aug 9, 2004
Messages
52
Location
Atlanta, GA, USA
I am working on a small forum project for the sake of learning. At the moment, I'm using a Repeater to list Forums, and a custom control named ForumLink to actually create a link to each forum. Within ForumLink is simply a LinkButton with some additional formatting. Within the Repeater's OnItemDataBound method I am trying to pass the current DataItem as a DataRowView to a public property of ForumLink, ForumDataRowView, so that I can customize the display of the LinkButton. To set properties of standard controls, I'd simply use the FindControl() method, but I'm having trouble casting to my custom control.

Code:
/* Produces the error: CS0246: The type or namespace name
      'ForumLink' could not be found (are you missing a using
      directive or an assembly reference?) */
((ForumLink) e.Item.FindControl("lnkForum")).ForumDataRowView =
    (DataRowView) e.Item.DataItem;

Anyone know where I'm going wrong?
 
It appears your missing the reference to your custom control or need to add an 'using' statement to the top of your class in your web project. Can you access it via the fully qualified namespace?
 
That doesn't mean that it's referenced in your code behind. Again can you access it using the fully qualified namespace?
 
What I mean is that if the control is a Label, you would fully reference if by going System.Web.UI.WebControls.Label - if for instance this ForumLink is in a 3rd party control called SomebodyCustomControls.WebControls.ForumLink, and you didn't have

C#:
using SomebodyCustomControls.WebControls;

declared at the top of your class then this would give you the compile errors you are getting
 
bri189a said:
What I mean is that if the control is a Label, you would fully reference if by going System.Web.UI.WebControls.Label - if for instance this ForumLink is in a 3rd party control called SomebodyCustomControls.WebControls.ForumLink, and you didn't have

C#:
using SomebodyCustomControls.WebControls;

declared at the top of your class then this would give you the compile errors you are getting

The two files I'm using are:

ViewBoard.ascx:
C#:
<%@ Control Language="C#" Debug="True" AutoEventWireup="True" %>
<%@ Register TagPrefix="FBB" TagName="ForumLink" src="ForumLink.ascx" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<script runat="server">

public String DBConnString = "";

private void Page_Load (Object Sender, EventArgs e) {
  if (!Page.IsPostBack) {
    LoadForumList();
  }
}

private void LoadForumList () {
  String qryForumGroups = "SELECT ForumGroupID, Title, Description FROM ForumGroups
    ORDER BY ListPosition ASC, Title ASC;";

  // try {
    OleDbConnection fbbConn = new OleDbConnection(DBConnString);
    OleDbCommand fbbCmd = new OleDbCommand(qryForumGroups, fbbConn);
    OleDbDataReader rdrData;

    if ((DBConnString == null) || (DBConnString.Length == 0)) {
      throw new Exception("No Connection String specified.");
    }

    fbbConn.Open();
    rdrData = fbbCmd.ExecuteReader();
    if (rdrData.HasRows) {
      rptrForumList.Visible = true;
      rptrForumList.DataSource = rdrData;
      rptrForumList.DataBind();
    } else {
      litDebug.Visible = true;
      litDebug.Text = "No Forum Groups.";
    }

    rdrData.Close();
    rdrData = null;
    fbbCmd = null;
    fbbConn.Close();
    fbbConn = null;
  /* } catch (OleDbException dbEx) {
    litDebug.Visible = true;
    litDebug.Text = dbEx.Message;
  } catch (Exception ex) {
    litDebug.Visible = true;
    litDebug.Text = ex.Message;
  } */
}

private void rptrForumList_ItemDataBound (Object Sender, RepeaterItemEventArgs e) {
  switch (e.Item.ItemType) {
    case ListItemType.Item:
    case ListItemType.AlternatingItem:
      ((ForumLink) e.Item.FindControl("lnkForum")).ForumDataRowView = (DataRowView) e.Item.DataItem;
      break;
  }
}	// End lnkViewForum_Command

</script>

<div class="divPageContent">
  <div class="divMenuBar">
  </div>
  <ASP:Literal ID="litDebug"
    Visible="false"
    RunAt="server" />
  <ASP:Repeater ID="rptrForumList"
    Visible="false"
    OnItemDataBound="rptrForumList_ItemDataBound"
    RunAt="server">
    <HeaderTemplate>
      <div class="divForum">
    </HeaderTemplate>
    <ItemTemplate>
      <FBB:ForumLink ID="lnkForum"
        RunAt="server" />
    </ItemTemplate>
    <FooterTemplate>
      </div>
    </FooterTemplate>
  </ASP:Repeater>
</div>

ForumLink.ascx:
C#:
<%@ Control Language="C#" Debug="True" %>
<%@ Import Namespace="System.Data" %>

<script runat="server">

public DataRowView ForumDataRowView;

private void Page_Load (Object Sender, EventArgs e) {
  lnkViewForum.Text = ForumDataRowView["Title"].ToString();
  lnkViewForum.CommandArgument = ForumDataRowView["ForumGroupID"].ToString();
}

private void lnkViewForum_Command (Object Sender, CommandEventArgs e) {
  Session["CurrentForum"] = e.CommandArgument.ToString();
  Response.Redirect("ViewForum.aspx");
}	// End lnkViewForum_Command

</script>

    <div class="divForum" id="Forum<%# ((DataRowView) ForumDataRowView)["ForumGroupID"].ToString() %>">
      <div class="divForumName"
        id="Forum<%# ((DataRowView) ForumDataRowView)["ForumGroupID"].ToString() %>_Name">
        <ASP:LinkButton ID="lnkViewForum"
          CommandName="ViewForum"
          OnCommand="lnkViewForum_Command"
          RunAt="server" />
      </div>
      <div class="divForumDescription"
        id="Forum<%# ((DataRowView) ForumDataRowView)["ForumGroupID"].ToString() %>_Desc">
        <%# ((DataRowView) ForumDataRowView)["Description"].ToString() %>
      </div>
  </div>
 
Last edited:
I'd say since you're not using code behinds this is the cause.

If you put your ProjectName.ForumLink, it finds the reference but then it will come back with an error that says it doesn't posses a property named ForumDataRowView. When using the same page as the code behind it seems anything you declare public there is actually not.
 
Back
Top