Solved

Repeater Control inside custom asp.net web control

Posted on 2003-12-11
2
1,478 Views
Last Modified: 2007-12-19
I have programmatically created a Repeater control inside an asp.net web control. I need a bit of help setting up the structure correctly. After hours of research, i basically came up with the following:

// code block below is all inside my CreateChildControls() method
==========================================
Repeater1.ID="Repeater1";
Repeater1.DataSource = dt;
Repeater1.DataBind();
                  
// set up TemplateBuilder to dynamically build ItemTemplate
TemplateBuilder rptrItem = new TemplateBuilder();
                  
// build content for ItemTemplate
string BuildHTML1 = "";
BuildHTML1 += "<tr>";
BuildHTML1 += "<td valign='top'>";
BuildHTML1 += "<table cellspacing='0' cellpadding='0' align='Center' border='1' BGCOLOR='#E9E9EE'";
BuildHTML1 += "<tr>";
BuildHTML1 += "<td width='100' align='center' valign='middle' nowrap>";
                  
// register BuildHTML1
rptrItem.AppendLiteralString(BuildHTML1);
                  
// construct ThumbButton (an ImageButton)
//ThumbButton.ID = "ThumbButton";
ThumbButton.ImageUrl = Repeater1.Items[3].ToString();
ThumbButton.AlternateText = Repeater1.Items[4].ToString();
ThumbButton.BorderWidth = 0;
ThumbButton.Command += new CommandEventHandler(BuildBrava);
ThumbButton.CommandName = Repeater1.Items[0].ToString();
ThumbButton.CommandArgument = Repeater1.Items[2].ToString();
                  
// register ThumbButton
Repeater1.ItemTemplate.InstantiateIn(ThumbButton);
                  
// add ThumbButton
Repeater1.Controls.Add(ThumbButton);

// register final HTML
rptrItem.AppendLiteralString("</td></tr></table></td></tr>");
Repeater1.ItemTemplate = rptrItem;
                  
// add Repeater1 as a control
Controls.Add(Repeater1);
==========================================

The 1st problem, right off the bat, is that all the "Repeater1.Items[3].ToString();" code is wrong. I can't figure out how to get the value of a chosen column of the "current" row of my DataTable (dt), so I can set properties of my ImageButton (ThumbButton).

so. . .I really need a bit of help with:
1- how do I grab the data values of the Repeater?
2- is my structure correct? i'm especially wondering whether the following 2 lines of code are right, and in the right place:

      Repeater1.ItemTemplate.InstantiateIn(ThumbButton);

and. . .

     Repeater1.Controls.Add(ThumbButton);

please help.
0
Comment
Question by:SweatCoder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
edwinugomez earned 500 total points
ID: 9930603
Hi.
Instead of creating the table and button in the CreateChildControls method, create them in the ItemCreated event of the repeater. On CreateChildControls  just create the repeater, add a handler to the itemcreated event and add it to the controls collection.

protected override void CreateChildControls() {
   Repeater Repeater1 = new Repeater();
   Repeater1.ID="Repeater1";
   Repeater1.DataSource = dt;
   Repeater1.ItemCreated += new RepeaterItemEventHandler(onItemCreated);
   Repeater1.DataBind();
   Controls.Add(Repeater1);
}

And the evend handler should be something like:
void onItemCreated(Object sender, RepeaterItemEventArgs e) {
   ImageButton ThumbButton  = new ImageButton();
   RepeaterItem MyItem  = e.Item;
   DataRowView CurrentDataRow  = (DataRowView)MyItem.DataItem;
   
   ThumbButton.ImageUrl = (String)CurrentDataRow.Row.ItemArray[1];
   ThumbButton.ID = "MyImageButton";
   ThumbButton.AlternateText =  (String)CurrentDataRow.Row.ItemArray[0];
   ThumbButton.BorderWidth = 0;
   ThumbButton.ImageUrl = "New Image";
   ThumbButton.Command += new CommandEventHandler(BuildBrava);
   
   Table MyTable = new Table();
   MyTable.CellPadding = 0;
   MyTable.CellSpacing = 0;
   MyTable.Rows.Add(new TableRow());
   MyTable.Rows[0].Cells.Add(new TableCell());
   MyTable.Rows[0].Cells[0].Controls.Add(ThumbButton);
   e.Item.Controls.Add(MyTable);
}

Hope it helps!
Edwin
0
 
LVL 11

Author Comment

by:SweatCoder
ID: 9931714
there is still a problem. everything compiles, but the Repeater is not rendering. no errors, just doesn't render.

since my class must derive from WebPart (it's a MS Sharepoint thing) rather than WebControl or Control, things work a bit differently. One difference is that NOTHING will render/ouput unless it is defined inside the mandatory override method "RenderWebPart".
 
after getting your code, i tried to render ThumbButton separately in the RenderWebPart method, but that just created a broken image link above the FileInput control. when i leave it out, i get nothing. The RenderWebPart is supposed to be equivalent to the "Render" method of the Control class. In a standard environment in which Render is used, you don't *have* to put anything in Render, you can make the controls render from within CreateChildControls, but that's not the case in this environment.
 
I have a FileInput control and a button control, both of which render just fine (see attached image). It's when I get to the Repeater part that i have the trouble.
 
here's my code at this point:

void onItemCreated(Object sender, RepeaterItemEventArgs e) {
      ImageButton ThumbButton  = new ImageButton();
      RepeaterItem MyItem  = e.Item;
      DataRowView CurrentDataRow  = (DataRowView)MyItem.DataItem;
                  
      ThumbButton.ImageUrl = (String)CurrentDataRow.Row.ItemArray[3];
      ThumbButton.ID = "ThumbButton1";
      ThumbButton.AlternateText =  (String)CurrentDataRow.Row.ItemArray[4];
      ThumbButton.BorderWidth = 0;
      ThumbButton.Command += new CommandEventHandler(BuildBrava);

      Table MyTable = new Table();
      MyTable.CellPadding = 0;
      MyTable.CellSpacing = 0;
      MyTable.Rows.Add(new TableRow());
      MyTable.Rows[0].Cells.Add(new TableCell());
      MyTable.Rows[0].Cells[0].Controls.Add(ThumbButton);
      e.Item.Controls.Add(MyTable);
}

protected override void CreateChildControls(){
                  
      BuildFileList("CreateChildControls"); // build recordset
      Controls.Clear(); // clear controls
                  
      // declaration of UpLoader (INPUT-SUBMIT)
      UpLoader.ID = "UpLoader";
      UpLoader.EnableViewState = false;
      Controls.Add(UpLoader);

      // declaration of InputSubmit (SUBMIT BUTTON)
      InputSubmit.ID = "InputSubmit";
      InputSubmit.Value="Upload";
      InputSubmit.EnableViewState = false;
      InputSubmit.ServerClick += new System.EventHandler(Upload_Click);
      Controls.Add(InputSubmit);

      // declaration of Repeater1            
      Repeater Repeater1 = new Repeater();
      Repeater1.ID="Repeater1";
      Repeater1.DataSource = dt;
      Repeater1.ItemCreated += new RepeaterItemEventHandler(onItemCreated);
      Repeater1.DataBind();
      Controls.Add(Repeater1);
}

protected override void RenderWebPart(HtmlTextWriter output){
      EnsureChildControls(); // must call this first

      // stylesheet definitions
      output.Write("<STYLE>");
      output.Write("BODY,TD {FONT-FAMILY:" + IGCFont + ";COLOR:" + IGCFontColor + ";FONT-SIZE:" + IGCFontSize + "pt;}");
      output.Write("</STYLE>");

      // begin table defs
      output.Write("<table BORDER='0'>");
      output.Write("<tr>");
      output.Write("<td valign='top' nowrap>");
      output.Write("<table cellspacing='0' cellpadding='0' border='0' width='100%' bordercolor='#A09DB3'>");
      output.Write("<tr>");
      output.Write("<td nowrap>");
            
      // render UpLoader
      UpLoader.RenderControl(output);
                  
      // render InputSubmit
      InputSubmit.RenderControl(output);
      
      // end html for uploader controls
      output.Write("</td>");
      output.Write("</tr>");      

               //===================================
      // render Repeater1
      Repeater1.RenderControl(output);
      //=====================================
                  
      //ThumbButton.RenderControl(output);
                  
      output.Write("</table>");
      output.Write("</td>");
      // end outer table def for thumbnails
            
      // begin cell for Brava DIV
      output.Write("<td valign='top'>");

      //===================================
      // render BravaDIV
      BravaDIV.RenderControl(output);
      //=====================================
                // end cell for Brava DIV
      output.Write("</td>");
      output.Write("</tr>");
      output.Write("</table>");
      // end outermost table
}

. . .the input/submit stuff renders and works, but the Repeater (and controls inside repeater) is getting lost in deep space somehow. no rendering, nothing visible.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net figure out error 3 47
Get sourcecode path 14 64
c#, case, if 4 39
Convert a string to date 4 40
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question