Create Image and PlaceHolder from code behind - dynamic - asp.net

I want read images from my database but they have to be inside of unorderd list.
I have an Handler do get the source of imgUrl
s+= "< ul>";
while (reader.Read())
{
id = reader.GetInt32(0);
s += "< li>< div>";
s += "< a href='#'>< img src='~/Handlers/ShowImage.ashx?id=" + id + "'/>< /a>";
s += "</ div></ li>";
}
s+="< /ul>";
after that I use a Literal who is declared in aspx to pass this block of html code.
myLiteral.text = s;
The problem is: my handler never is executed, so i never get the img source.
To solve that I start to use an PlaceHolder instead write the tag "< img ... />".
So i have:
while (reader.Read())
{
id = reader.GetInt32(0);
PlaceHolder holder = new PlaceHolder();
Image img = new Image();
img.ImageUrl = "~/Handlers/ShowImage.ashx?id=" + id;
holder.Controls.Add(img);
s += " li div";
s += " a href='" + reader.GetString(1) + "'" + holder + " /a";
s += " /div /li";
}
this way the handler executes but my holder just pass the .toString() :S
Someone help-me? And, is this the best way for what I want?
Thanks.

Please try....
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
holder.RenderControl(htw);
string s = sw.ToString();
"s" is what you want.

Can you please clarify your question a bit?
When you say you have a handler... is that an HttpHandler?
Where is your code running? Is it on the codebehind of your page? What method is it?
Also, if you want to render the HTML strings yourself, it's recommended that you use the StringBuilder class instead of just "s+="
Thanks!

Related

Put the html tags in the title property of the image tag (Text formatting in the image tag)

using (SqlConnection conn = DataAccess.GetConnected())
{
SqlCommand cmd = new SqlCommand("GetImageForSlider", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader sqr = cmd.ExecuteReader();
while (sqr.Read())
{
string image_path = sqr.GetString(0);
string description = sqr.GetString(1);
string action = sqr.GetString(2);
HtmlImage += "<img src=\"" + image_path + "\" title=\"" + "<h3>" + description + action + "\".</h3><p><br></p><ahref=http://www.giftotravels.com >Thisislink</a>" + "/>";
}
}
i am trying to as above but this is not working properly
any one can help???
You cannot insert html tags inside image tag; it becomes invalid format. You want to render html like this.
HtmlImage += string.Format("<img src=\"{0}\" title=\"/><h3>{1}{2}.</h3>" +
"<p><br></p><a href='http://www.giftotravels.com'>Thisislink</a>",
image_path, description, action);
You can't. The title attribute gets rendered as plain text. If you want to do text markup, create an element yourself and let that float above the picture on mouseover.

send rendered GridView in an email

I have a GridView whose HTML I need to put into an email.
How can this be done?
use an HTMLtext writer to render the gridview control.You will get an HTML output as string.use this as your message body.
public string RenderControl(Control ctrl)
{
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
return sb.ToString();
}
Use a stringbuilder to create a HTML table to resample your gridview like so:
var output = new StringBuilder("<table cellpadding='5' style='border: solid 1px black;'>");
foreach (GridViewRow row in MyGridView.Rows)
{
output.Append("<tr>");
output.Append("<td>" + Title + "</td>");
output.Append("<td>" + Price + "</td>");
output.Append("<td>" + Quantity + "</td>");
output.Append("</tr>");
}
output.Append("</table>");
Ideally you should not be concerned about the GridView control. Take the data that is being displayed in the control, format it in what ever manner you want and pass it as your message body.
I would treat the logic to display Grid in my application and generating grid for message body as two separate functionality. What will be common in between the two, is the source of data. For formatting of data(generating the Grid), you can use the HTMLTextWriter class(as mentioned by others) and format the data in what ever way you want.
Two thing you can do for this
1) take screen shot of the current page by using any utility and send in mail
2) you can export grid as excel and than sent it in mail.
Export grid to Excel
http://geekswithblogs.net/azamsharp/archive/2005/12/21/63843.aspx

Loop through the labels of a page to assign text

I have some 20 labels on my aspx page for which the IDs are lbl1,lbl2....lbl20 and text is driven by SqlServer table. Is there any easy way to loop through all the labels on the page and assing the text from reader.
I did some thing like but it doesn't work.
SqlDataReader Reader = new SqlDataReader();
int i = 0;
while(Reader.read())
{
label lbl = new label();
lbl.ID = "label" + i;
lbl.text = Reader["ColumnName"].ToString();
}
Is there any other method through which I can loop through all the labels and assign text for it?
If you have them in a container, i think you can do something like this below:
For Each lbl As Control In Grid1.Children
If TypeOf lbl Is Label Then
'your logic
End If
Next
I have only tried this in silverlight however, so i'm not sure it works, or if putting them all in a container is practical in your case.
One way to do this is use the findcontrol method. This would work well because all your labels are named with a "lbl0", "lbl1"... convention.
**start looping:
int index = 0;
string currentLabel = "lbl" + index.ToString();
index++;
Control myControl1 = FindControl(currentLabel);
// cast control to type: (label)
// apply text from reader**
Give that a shot. Hope it works out
I have used this in the past and I just tested it out.
You can do this because every page has a form
HtmlForm form1 = (HtmlForm)Page.FindControl("ContentPlaceHolder1");
for (int i = 1; i <= 3; i++) {
((TextBox)form1.FindControl("label" + i)).Text = "This is label number " + i;
}
If you have a master page change the first line to this
ContentPlaceHolder ph = (ContentPlaceHolder)Page.FindControl("ContentPlaceHolder1");

ASP.Net add image to radiobuttonlist items on load

I have a RadioButtonList that is bound to a datatable. Within the datatable, a column exist with urls of images and another column with the description.
The following is what I am doing right now (not working though):
foreach ( acryliccolor scurrent in ssmacryliccolor )
{
DataRow dr = dt.NewRow();
dr["TEXT"] = "<img src=\"\\colorswatches\\" + scurrent.SwatchURL + "\" alt=\"\" border=\"0\" /><span style=\"margin-right:21px;\"></span>" + scurrent.Color;
dr["VALUE"] = "ID|SC_" + scurrent.ID.ToString() + ";CSID|" + current.ID.ToString() + ";JS|radiosimple(this)";
dt.Rows.Add(dr);
}
this.rblAcrylicColors.DataSource = dt;
this.rblAcrylicColors.DataTextField = "TEXT";
this.rblAcrylicColors.DataValueField = "VALUE";
this.rblAcrylicColors.DataBind();
How do I add the image next to the description for each radiobutton item?
Have you tried dynamically adding items to the radiobuttonlist.Items collection?
this.rblAcrylicColors.Items.Add(String.Format("<img src={0} />", "url"));
Also have you checked the html output from what you are currently trying? Is the img tag there?

ASP.NET adding controls inside a loop

Pardon me, this really is a noob question but please understand that I do not have much ASP.NET experience. All I need to do is:
1) Open up the following SQL query:
SELECT myid FROM mytable
2) For each record, generate this fragment of HTML:
<img src="http://someurl/__myid comes here as well__/small.png" />
Its easy for me to use the classical ASP <% do until recordset.eof ... loop %> style loops but I need it in ASP.NET style, probably perform the operation in Page_Load event and use intrinsic ASP.NET controls.
Use a repeater control. In the ItemTemplate, you can put whatever you would like to be generated for each record returned from your query.
http://articles.sitepoint.com/article/asp-net-repeater-control
add this in the aspx page source
<td id="urLink" runat="server">
</td>
add this in the Page_Load event
SqlConnection con = new SqlConnection();
con.connectionstring = "your connection database";
SqlDataReader reader;
SqlCommand command = new SqlCommand(con);
command.Commandtext = "SELECT myid FROM mytable";
command.CommandType = CommandType.Text;
reader = command.ExecuteReader();
while(reader.Read())
{
urLink.innerHTML += "<a href="#mynameanchor" onClick="myfunction( " + reader["myid"].tostring() + " );">
<img src="http://someurl/" + reader["myid"].tostring() + "/small.png" /></a>";
}
Looks like the onclick is a javascript event which means you can write out the HTML markup to a string and then append it all into a literal control. Remember to use a stringbuilder :)
here's a very basic example:
// get data from database and into a reader before
StringBuilder links = New StringBuilder();
while(reader.read())
{
links.appendFormat("<a HREF='id_{0}'>{0}</a>", reader["ID"]);
}
ltlLinks.Text = links.ToString();

Resources