custom controls / dynamic control rendering question


M

matthias s.

Hi there,

I'm building a usercontrol to provide language selection options. The
availabe languages are retrieved dynamically and should be shown as flags
(ImageButtons) in a table.

But I've got a problem with formatting the whole thing. I'm using
Response.Write statements to write the table. Here is a code snippet (the
fat logic is omitted and I hope the variable names are selfexplanatory):

+++
Language lng = new Language();
Response.Write("<table>");

for (int i = 0; i < Language.AvailableLanguages().Length; i++)
{
if (nRowCount == 0)
Response.Write("<tr>");

Response.Write("<td>");

string sImageFileName = "";
LanguageInfo linfo = Language.AvailableLanguages();
if (linfo.ID == lng.ID)
{
// language is selected
sImageFileName = "onflag_" + linfo.ShortCode + ".gif";
}
else
{
// not selected
sImageFileName = "offflag_" + linfo.ShortCode + ".gif";
}

// create an imagebutton control and set the properties
ImageButton btn = new ImageButton();
btn.ID = linfo.ShortCode;
btn.ImageUrl = "~/Lib/Images/" + sImageFileName;
btn.Click += new System.Web.UI.ImageClickEventHandler(btn_Click);
Controls.Add(btn);
Response.Write("</td>");

if (nRowCount == 3)
{
Response.Write("</tr>");
nRowCount = 0;
}
else
nRowCount++;
}

Response.Write("</table>");
+++

In the source of the resulting html page, I can't find any of the html I've
written with response write. The images are there, but none of the html.

How do I do this properly?

P.S.: If it matters, the LanguageSelectionControl (in who's Page_Load event
the above code is placed) is itself nested in a HeaderControl (as well my
own make).

Any hints are greatly appreciated!

/matthias
 
Ad

Advertisements

M

matthias s.

Hi Karl,

thanks for your answer. Would you mind telling my why it doesn't work
anyway? Would be greatly appreciated.

/matthias

Karl Seguin said:
use a repeater and bind to it...makes your code cleaner and likely will make
it work...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
matthias s. said:
Hi there,

I'm building a usercontrol to provide language selection options. The
availabe languages are retrieved dynamically and should be shown as flags
(ImageButtons) in a table.

But I've got a problem with formatting the whole thing. I'm using
Response.Write statements to write the table. Here is a code snippet (the
fat logic is omitted and I hope the variable names are selfexplanatory):

+++
Language lng = new Language();
Response.Write("<table>");

for (int i = 0; i < Language.AvailableLanguages().Length; i++)
{
if (nRowCount == 0)
Response.Write("<tr>");

Response.Write("<td>");

string sImageFileName = "";
LanguageInfo linfo = Language.AvailableLanguages();
if (linfo.ID == lng.ID)
{
// language is selected
sImageFileName = "onflag_" + linfo.ShortCode + ".gif";
}
else
{
// not selected
sImageFileName = "offflag_" + linfo.ShortCode + ".gif";
}

// create an imagebutton control and set the properties
ImageButton btn = new ImageButton();
btn.ID = linfo.ShortCode;
btn.ImageUrl = "~/Lib/Images/" + sImageFileName;
btn.Click += new System.Web.UI.ImageClickEventHandler(btn_Click);
Controls.Add(btn);
Response.Write("</td>");

if (nRowCount == 3)
{
Response.Write("</tr>");
nRowCount = 0;
}
else
nRowCount++;
}

Response.Write("</table>");
+++

In the source of the resulting html page, I can't find any of the html
I've
written with response write. The images are there, but none of the html.

How do I do this properly?

P.S.: If it matters, the LanguageSelectionControl (in who's Page_Load
event
the above code is placed) is itself nested in a HeaderControl (as well my
own make).

Any hints are greatly appreciated!

/matthias

 
Ad

Advertisements

K

Karl Seguin

My guess is that it does work, just not like you expect.

The table is likely getting rendered atop your page, before the <html> or at
the bottom after your </html>. Response.Write doesn't inject the code in
"the right place"...because how can it know what the 'right place' is....


Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
matthias s. said:
Hi Karl,

thanks for your answer. Would you mind telling my why it doesn't work
anyway? Would be greatly appreciated.

/matthias

Karl Seguin said:
use a repeater and bind to it...makes your code cleaner and likely will make
it work...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
matthias s. said:
Hi there,

I'm building a usercontrol to provide language selection options. The
availabe languages are retrieved dynamically and should be shown as flags
(ImageButtons) in a table.

But I've got a problem with formatting the whole thing. I'm using
Response.Write statements to write the table. Here is a code snippet (the
fat logic is omitted and I hope the variable names are
selfexplanatory):

+++
Language lng = new Language();
Response.Write("<table>");

for (int i = 0; i < Language.AvailableLanguages().Length; i++)
{
if (nRowCount == 0)
Response.Write("<tr>");

Response.Write("<td>");

string sImageFileName = "";
LanguageInfo linfo = Language.AvailableLanguages();
if (linfo.ID == lng.ID)
{
// language is selected
sImageFileName = "onflag_" + linfo.ShortCode + ".gif";
}
else
{
// not selected
sImageFileName = "offflag_" + linfo.ShortCode + ".gif";
}

// create an imagebutton control and set the properties
ImageButton btn = new ImageButton();
btn.ID = linfo.ShortCode;
btn.ImageUrl = "~/Lib/Images/" + sImageFileName;
btn.Click += new System.Web.UI.ImageClickEventHandler(btn_Click);
Controls.Add(btn);
Response.Write("</td>");

if (nRowCount == 3)
{
Response.Write("</tr>");
nRowCount = 0;
}
else
nRowCount++;
}

Response.Write("</table>");
+++

In the source of the resulting html page, I can't find any of the html
I've
written with response write. The images are there, but none of the
html.

How do I do this properly?

P.S.: If it matters, the LanguageSelectionControl (in who's Page_Load
event
the above code is placed) is itself nested in a HeaderControl (as well my
own make).

Any hints are greatly appreciated!

/matthias


 

Top