Page view counter

Problem with UserControl

Last post 10-26-2008 12:00 AM by cheng_jiawei. 19 replies.

Sort Posts:

  • Problem with UserControl

    10-23-2008, 10:32 PM
    • Loading...
    • easter
    • Joined on 06-05-2007, 11:08 PM
    • Posts 27
    • Points 21

    Hi everyone,
    I'm having a problem with UserControls. Specifically, they don't show up and I'm getting null reference exceptions during load. 

        protected void Page_Load(object sender, EventArgs e) {
            lblScore.Text = q.Score.ToString();
            lnkQuote.Text = "#" + q.QuoteID;
            lblTime.Text = q.Submitted.ToString( "yyyy-mm-dd" );
            divBody.InnerText = q.Text;
            divComm.InnerText = q.Comment;
        }
    Each of the controls are null, and I don't know why. "q" is defined as it should be. The ASCX page is the following 
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="QuoteControl.ascx.cs" Inherits="QuoteControl" %>
    
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            Quote 
            <asp:LinkButton ID="lnkQuote" runat="server" onclick="lnkQuote_Click">a</asp:LinkButton>
            Score: <asp:Label id="lblScore" runat="server">a</asp:Label> [ 
            <asp:LinkButton ID="lnkUp" runat="server" Text="+" CommandArgument="up" 
                    onclick="lnkUp_Click">a</asp:LinkButton> / 
            <asp:LinkButton ID="lnkDown" runat="server" Text="-" CommandArgument="down" 
                    onclick="lnkDown_Click">a</asp:LinkButton> ]
            - Submitted: <asp:Label ID="lblTime" runat="server">a</asp:Label>
            <asp:Label ID="lblError" runat="server">a</asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>
    <div id="divBody" runat="server">a</div>
    <div id="divComm" runat="server">a</div>
    And the control is dynamically created with the following 
                foreach ( Model.Quote q in quotes ) {
                    QuoteControl uc = new QuoteControl();
                    uc.QuoteInfo = q;
                    uc.Visible = true;
                    this.Controls.Add( uc );
                }

    Am I missing something? I'm using Web Dev 2008.

     If you need any more info, just let me know. Thanks :)

  • Re: Problem with UserControl

    10-24-2008, 1:06 AM
    • Loading...
    • karthi_ga
    • Joined on 09-18-2008, 9:25 AM
    • Posts 101
    • Points 174

     i don't know in 2008, but in 2005

     

    public partial class ColorsPicker : System.Web.UI.UserControl
    {
        public System.Drawing.Color SelectedColor
        {
    //ur code
    }
    }
      
  • Re: Problem with UserControl

    10-24-2008, 3:23 PM
    • Loading...
    • easter
    • Joined on 06-05-2007, 11:08 PM
    • Posts 27
    • Points 21

    Well, here's the full code to the ascx.cs file.  

    using System;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    
    public partial class QuoteControl : System.Web.UI.UserControl
    {
        private Model.Quote q;
    
        protected void Page_Load(object sender, EventArgs e) {
            lblScore.Text = q.Score.ToString();
            lnkQuote.Text = "#" + q.QuoteID;
            lblTime.Text = q.Submitted.ToString( "yyyy-mm-dd" );
            divBody.InnerText = q.Text;
            divComm.InnerText = q.Comment;
        }
    
        public Model.Quote QuoteInfo {
            get { return q; }
            set { q = value; }
        }
    
        private void Vote( string dir ) {
            if ( BLL.Quote.Vote( q.QuoteID, "dir", Request.UserHostAddress ) )
                lblError.Text = "Vote Successful";
            else
                lblError.Text = "Vote Failed";
        }
    
        protected void lnkUp_Click( object sender, EventArgs e ) {
            Vote( "+" );
        }
    
        protected void lnkDown_Click( object sender, EventArgs e ) {
            Vote( "-" );
        }
    
        protected void lnkQuote_Click( object sender, EventArgs e ) {
            Response.Redirect( "quote.aspx?t=view&id=" + q.QuoteID );
        }
    }
    


    I don't see why, in the page load, the controls are null.

  • Re: Problem with UserControl

    10-24-2008, 6:35 PM

    hello,

    you have to register your ascx control in the aspx page, like using the ajax control toolkit...

    <%@ Register tagPrefix="qcc" src="~/QuoteControl.ascx" tagName="qccntrl" %>

    then call your control on the aspx page:

    <qcc:qccntrl ID="QuoteControlID" runat="server" />

    It will then act as if it is part of the aspx page. To use the controls call by QuoteControlID.controlID

     

    Hope this helps,

    jeef
  • Re: Problem with UserControl

    10-24-2008, 7:01 PM
    • Loading...
    • easter
    • Joined on 06-05-2007, 11:08 PM
    • Posts 27
    • Points 21

    I already have 

    <%@ Reference Control="Controls/QuoteControl.ascx" %>
    But I added what you wrote anyways, still doesn't work. I am dynamically creating all the controls in the background, in the .cs file, because everything is comming in from a database.
  • Re: Problem with UserControl

    10-24-2008, 8:25 PM

    Instead of this in your aspx page: 

    <ContentTemplate>
            Quote
            <asp:LinkButton ID="lnkQuote" runat="server" onclick="lnkQuote_Click">a</asp:LinkButton>
            Score: <asp:Label id="lblScore" runat="server">a</asp:Label> [
            <asp:LinkButton ID="lnkUp" runat="server" Text="+" CommandArgument="up"
                    onclick="lnkUp_Click">a</asp:LinkButton> /
            <asp:LinkButton ID="lnkDown" runat="server" Text="-" CommandArgument="down"
                    onclick="lnkDown_Click">a</asp:LinkButton> ]
            - Submitted: <asp:Label ID="lblTime" runat="server">a</asp:Label>
            <asp:Label ID="lblError" runat="server">a</asp:Label>
        </ContentTemplate>

    Only put:

    <ContentTemplate>

        <qcc:qccntrl ID="QuoteControlID" runat="server" />

    </ContentTemplate>

     

    Hope this helps,

    jeef
  • Re oops

    10-24-2008, 8:25 PM

    Embarrassed

    Hope this helps,

    jeef
  • Re: Problem with UserControl

    10-24-2008, 8:25 PM

    Doh, I did not realize you were referencing your Controls code behind from the aspx file. My bad, ... It was my understanding that UserControls are for writing once what is used often, like the items in the AJAX control toolkit. I always put my asp controls on my UserControl.ascx page and put the code behind in my UserControl.ascx.cs page. I Place the <qcc:qccntrl ID="QuoteControlID" runat="server" /> on the aspx page and any other controls the UserControl might interact with, the aspx.cs code behind handles the interaction between the UserControl and the aspx controls

    The aspx page is "hosting" the ascx control...

    Hope this helps,

    jeef
  • Re: Problem with UserControl

    10-24-2008, 8:43 PM
    • Loading...
    • easter
    • Joined on 06-05-2007, 11:08 PM
    • Posts 27
    • Points 21

    EDIT: You edited your posts :P

    Okay, I see what you mean. The code you posted is in my ASCX page, not ASPX. Was a typo in my first post, sorry.

  • Re: Problem with UserControl

    10-24-2008, 9:00 PM

    easter:

    EDIT: You edited your posts :P

    Okay, I see what you mean. The code you posted is in my ASCX page, not ASPX. Was a typo in my first post, sorry.

     

    ah! I'm taking to long to check my references. It seems my advice cannot help :( 

    Hope this helps,

    jeef
  • Re: Problem with UserControl

    10-24-2008, 9:05 PM
    • Loading...
    • easter
    • Joined on 06-05-2007, 11:08 PM
    • Posts 27
    • Points 21

    Normally, like the Ajax controls, I would statically put them in the page, but unfortunately I need to dynamically create the controls, as I won't know how many will be needed.

     Thanks for trying though :)

  • Re: Problem with UserControl

    10-24-2008, 9:19 PM

    Are you dynamically adding userControls to the aspx page itself?

    If so for every userControl added you have to add another <qcc:qccntrl ID="QuoteControlID" runat="server" /> to the aspx page.

     Possible solutions:

    1) on aspx postback use a for loop to:

     append a number to QuoteControlID incrementing the ID, building the "<qcc:qccntrl ID=""QuoteControl""" + i + " runat=""server"" />" string and use an HTMLWriter to add it to the aspx page. When the page Renders it may show the correct amount of controls.

    2) add the maximum number of quoteControls allowed to the aspx page, on Init disable whatever is not needed yet, on postback enable as many userControls as needed.

    Hope this helps,

    jeef
  • Re: Problem with UserControl

    10-24-2008, 9:57 PM
    • Loading...
    • cheng_jiawei
    • Joined on 10-24-2008, 9:36 PM
    • China Shanghai
    • Posts 6
    • Points 22

    I think the problem is that :where and when you created your UserControls?

    Just as you see, everytime  you postback your page,the UserControls that your added are completed lost(So is null).

    Yet,I got that problems before.

    I think it's better to create Client Controls instead of UserControl in the server.

    Or you have to create the UserControls everytime In the Page_Load event :)

    Hope that help~

  • Re: Problem with UserControl

    10-24-2008, 9:59 PM
    • Loading...
    • easter
    • Joined on 06-05-2007, 11:08 PM
    • Posts 27
    • Points 21

    No post back is occuring. The problem occurs on the initial load of the page.

  • Re: Problem with UserControl

    10-25-2008, 10:44 PM
    Answer
    • Loading...
    • cheng_jiawei
    • Joined on 10-24-2008, 9:36 PM
    • China Shanghai
    • Posts 6
    • Points 22

    I think you'd better Add your UserControls in Panel Control(Server),just try.

    I think your coding is ok, but if you want to click the link button in your controls ,it will be post back.

    :)

Page 1 of 2 (20 items) 1 2 Next >