Customized Control in the Datalist doesnt render

Discussion in 'ASP .Net' started by Victor, Jul 4, 2007.

  1. Victor

    Victor Guest

    Hi. all
    I have a customize web control. I have three simple properties and
    customized render class. then I add this control into my datalist like
    <asp:DataList ID="datalist" runat="server" RepeatColumns="3">
    <ItemTemplate>
    <uc:mycontrol id="test" runat="server" />
    </ItemTemplate>
    </asp:DataList>
    In the code-behind I have something like
    List<mycontrol > lst = new List<mycontrol >;
    lst.add(new mycontrol ());
    lst.add(new mycontrol ());
    lst.add(new mycontrol ());
    datalist.datasource= lst;
    datalist.databound();

    But i found the control can not be rendered properly. Instead of displaying
    four images, no images has been shown. What did I do wrong here? Can someone
    give me some reference on how to add your customized control to datalist
    then bind the value and render it properly.

    Cheers
    Thanks a lot
    Victor
     
    Victor, Jul 4, 2007
    #1
    1. Advertising

  2. Victor

    Masudur Guest

    On Jul 4, 11:25 am, "Victor" <> wrote:
    > Hi. all
    > I have a customize web control. I have three simple properties and
    > customized render class. then I add this control into my datalist like
    > <asp:DataList ID="datalist" runat="server" RepeatColumns="3">
    > <ItemTemplate>
    > <uc:mycontrol id="test" runat="server" />
    > </ItemTemplate>
    > </asp:DataList>
    > In the code-behind I have something like
    > List<mycontrol > lst = new List<mycontrol >;
    > lst.add(new mycontrol ());
    > lst.add(new mycontrol ());
    > lst.add(new mycontrol ());
    > datalist.datasource= lst;
    > datalist.databound();
    >
    > But i found the control can not be rendered properly. Instead of displaying
    > four images, no images has been shown. What did I do wrong here? Can someone
    > give me some reference on how to add your customized control to datalist
    > then bind the value and render it properly.
    >
    > Cheers
    > Thanks a lot
    > Victor


    Hi victor ...

    not getting the actual picture what is going on... any way...
    http://msdn2.microsoft.com/en-us/library/ms366538.aspx
    http://msdn2.microsoft.com/en-us/library/ms366539.aspx
    here is two link which demonstrated how to build a custom databound
    control...

    Thanks
    Munna
    www.kaz.com.bd
    http://munnacs.110mb.com
     
    Masudur, Jul 4, 2007
    #2
    1. Advertising

  3. Hi Victor,

    Based on your description, what you want is display a custom control in a
    DataList control's template(in each row after databind), correct?

    For the "MyControl" you mentioned, is it a custom webserver control or web
    usercontrol(ascx)? As you said that after you perform databindin on the
    DataList, the images are not displayed as expected, do you mean your custom
    webcontrol(MyControl) will display an Image if working correctly? Also, I
    saw you bind the DataList to a List<> of MyControl type, is this just a
    test datasource? Generally, you should bind DataList or other template
    databound control to a DataSource object (such as DataReader, DataSet or
    other custom data object Array/List). If convenient, you can also provide
    the complete code snippet (or simplified one of your custom control) so
    that we can get a clear view on it.

    If there is anything I missed, please feel free to post here.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Jul 4, 2007
    #3
  4. Victor

    Victor Guest

    Hi Steve:
    Sorry about my poor description. I post simplified version of code here.
    I have a customized control MyControl like

    public class MyControl : WebControl
    {
    private int nMyControlID;
    private string strImagePath;
    public int MyControlID
    {
    get
    {
    return nMyControlID;
    }
    set
    {
    nMyControlID = value;
    }
    }
    public string ImagePath
    {
    get
    {
    return strImagePath;
    }
    set
    {
    strImagePath = value;
    }
    }

    protected override void Render(HtmlTextWriter writer)
    {
    writer.RenderBeginTag(HtmlTextWriterTag.A);
    writer.RenderBeginTag(HtmlTextWriterTag.Div);

    writer.AddAttribute("src", ResolveUrl(ImagePath);
    writer.RenderBeginTag(HtmlTextWriterTag.Img);
    writer.RenderEndTag(); //img

    writer.RenderEndTag(); //div
    writer.RenderEndTag(); //a
    }
    }

    And In my code I have

    List<MyControl> lstResult = new List<MyControl>();

    Database db = DatabaseFactory.CreateDatabase("MyDB");

    string sqlCommand = "dbo.usp_GetAllImageList";
    DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);

    using (DataSet ds = db.ExecuteDataSet(dbCommand))
    {
    foreach (DataRow dr in ds.Tables[0].Rows)
    {
    DataRowRecord drMyControl = new DataRowRecord(dr);

    MyControl obj = new MyControl();
    obj.MyControlID = DataUtil.GetIntValue(drMyControl,
    "MyControlID");
    obj.ImagePath = DataUtil.GetStringValue(drMyControl, "ImagePath");

    lstResult.Add(obj);
    }
    }

    then I databind my list with the datalist like

    datalist.datasource = lstResult;
    datalist.databind():

    the problem I have now is the datalist do create four customized controls.
    but images do not display at all. I think the value is not bound to the
    control.How can I solve this problem. Do I need to create another customized
    databound list control for mycontrol?

    Thanks a lot
    Regards
    Victor


    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Hi Victor,
    >
    > Based on your description, what you want is display a custom control in a
    > DataList control's template(in each row after databind), correct?
    >
    > For the "MyControl" you mentioned, is it a custom webserver control or web
    > usercontrol(ascx)? As you said that after you perform databindin on the
    > DataList, the images are not displayed as expected, do you mean your
    > custom
    > webcontrol(MyControl) will display an Image if working correctly? Also, I
    > saw you bind the DataList to a List<> of MyControl type, is this just a
    > test datasource? Generally, you should bind DataList or other template
    > databound control to a DataSource object (such as DataReader, DataSet or
    > other custom data object Array/List). If convenient, you can also provide
    > the complete code snippet (or simplified one of your custom control) so
    > that we can get a clear view on it.
    >
    > If there is anything I missed, please feel free to post here.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    >
     
    Victor, Jul 4, 2007
    #4
  5. Thanks for your reply Victor,

    Well, I've performed some local test through the custom control you
    provided. It seems I can correctly make the "MyControl" display image
    (through databinding) well in DataList. I think there might be something
    incorrect with your aspx template of the DataList, would you also provide
    it so that we can have a check? Anyway, below is my test page's aspx
    template and code behind for your reference:


    ===========aspx================
    <asp:DataList ID="DataList1" runat="server">
    <ItemTemplate>
    MYControl:
    <cc1:MyControl ID="MyControl1" runat="server"
    ImagePath='<%# Eval("ImagePath") %>' />
    </ItemTemplate>
    </asp:DataList>

    ========code behind============
    protected void Page_Load(object sender, EventArgs e)
    {
    BindList();
    }

    protected void BindList()
    {
    MyControl[] mcs = new MyControl[5];

    for (int i = 0; i < mcs.Length; i++)
    {
    mcs = new MyControl();
    mcs.ID = "mc_" + i;
    mcs.MyControlID = i;
    mcs.ImagePath =
    "http://static.asp.net/asp.net/images/MicrosoftASPNET.gif";

    }

    DataList1.DataSource = mcs;
    DataList1.DataBind();

    }
    =============================

    the "MyControl"'s code is identical to yours. If you have any questions on
    this, please feel free to post here.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Jul 5, 2007
    #5
  6. Victor

    Victor Guest

    Hi Steven:
    thanks so much for your help. The problem is solved. The problem is I did
    not bind the properties properly..
    :) Thanks again for the help


    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Thanks for your reply Victor,
    >
    > Well, I've performed some local test through the custom control you
    > provided. It seems I can correctly make the "MyControl" display image
    > (through databinding) well in DataList. I think there might be something
    > incorrect with your aspx template of the DataList, would you also provide
    > it so that we can have a check? Anyway, below is my test page's aspx
    > template and code behind for your reference:
    >
    >
    > ===========aspx================
    > <asp:DataList ID="DataList1" runat="server">
    > <ItemTemplate>
    > MYControl:
    > <cc1:MyControl ID="MyControl1" runat="server"
    > ImagePath='<%# Eval("ImagePath") %>' />
    > </ItemTemplate>
    > </asp:DataList>
    >
    > ========code behind============
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > BindList();
    > }
    >
    > protected void BindList()
    > {
    > MyControl[] mcs = new MyControl[5];
    >
    > for (int i = 0; i < mcs.Length; i++)
    > {
    > mcs = new MyControl();
    > mcs.ID = "mc_" + i;
    > mcs.MyControlID = i;
    > mcs.ImagePath =
    > "http://static.asp.net/asp.net/images/MicrosoftASPNET.gif";
    >
    > }
    >
    > DataList1.DataSource = mcs;
    > DataList1.DataBind();
    >
    > }
    > =============================
    >
    > the "MyControl"'s code is identical to yours. If you have any questions
    > on
    > this, please feel free to post here.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    >
    >
    >
    >
     
    Victor, Jul 5, 2007
    #6
  7. You're welcome :)


    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Jul 6, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lau Lei Cheong

    Page.Render do not render complete page

    Lau Lei Cheong, May 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,075
    Lau Lei Cheong
    May 15, 2004
  2. Nevyn Twyll
    Replies:
    8
    Views:
    1,881
  3. =?Utf-8?B?QWxleCBGaW1pbmU=?=

    IE6 doesnt render the page properly whereas IE7 does

    =?Utf-8?B?QWxleCBGaW1pbmU=?=, Aug 31, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    404
    Patrice
    Aug 31, 2007
  4. Replies:
    3
    Views:
    436
    alex23
    May 27, 2008
  5. Glenn Gillen
    Replies:
    0
    Views:
    280
    Glenn Gillen
    Nov 17, 2006
Loading...

Share This Page