Help!Controlling writing html into the response output stream. I'm getting close but need some guidance!
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngdatagridrepeaterdatalist' list.


Bill Bassler
Here's what I have so far ...
I've got an event wired from the Page object to the DataGrid object embedded
in the page via:

private void InitializeComponent()
{
...
this.DataGrid1.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.Item_Bound);

and the event handler (below) appears to fire for every row (Item?) (when
the grid is rendered ... I hope? It happens after the DataBind method is
called)
I can also get a reference to the control index via e.Item.ItemIndex.

e.g.
private void Item_Bound(object sender, DataGridItemEventArgs e)
{
Label2.Text = Label2.Text + " " + e.Item.ItemIndex;
}

So far so good.

Question1: What's the best way to inject something like the client-side
script string for each row rendered in a grid. What I want to do is write a
small chunk of client-side scripting connected to a hyperlink for each row
that will
run a function to show an hide a group of rows. So for each row, there will
be an id associated with the click of the link that, in turn fires the
client script
to show hide rows.

// this can be rendered via Page.RegisterClientScriptBlock. No problem
// client side script to show or hide rows
function toggleT(_w,_h) {
// hide or show document elements: _w : divID _h : (h)ide or (s)how
if (document.all) { // is IE
if (_h=='s') {
eval("document.all." + _w + "minus" + ".style.display='inline';");
eval("document.all." + _w + "plus" + ".style.display='none';");
eval("document.all." + _w + ".style.display='inline';");
}

if (_h=='h') {
eval("document.all." + _w + "minus" + ".style.display='none';");
eval("document.all." + _w + "plus" + ".style.display='inline';");
eval("document.all." + _w + ".style.display='none';");
}
}
}

// Here's an example of the row link to call the function above to show the
rows between a div tag
<A href="javascript:toggleT('divt1','s')"><IMG src='images/sign_plus.gif'
name=plus1 alt='Click to Expand' width=9 height=9 border=0></A>

Unfortunetely, the Page client-side script methods don't seem to handle this
per row case. Also, I'm having trouble getting an instance of the
HtmlTextWriter.

Question2: Would I be better off creating my own implementation of the
DataGrid and adding an event handler internally to render the additional
tags and html?

Question 3: Do I need to go the route of a rendered control (custom control)
to get this kind of functionality? i.e. write all the html for the grid
(table) out myself because inheriting
from the DataGrid will do me no good. I won't have control over when I can
inject the custom html. i.e. Overriding the DataGrid Render method won't do
it.

Reply to this message...
 
    
Alex Lowe
I was able to inject text and tie it to the ID of a tag using something
like this:

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then

e.Item.Attributes.Add("ID", "td" & e.Item.ItemIndex)
e.Item.Cells(0).Controls.AddAt(0, New LiteralControl("<A
href=""javascript:toggleT('td" & e.Item.ItemIndex & "','h')""><IMG
src='images/sign_plus.gif' name=plus1 alt='Click to Expand' width=9
height=9 border=0></A>" & e.Item.Cells(0).Text))

End If

It's roughly the same in C#. This code basically sets the ID for the Row
and then also sets the ID to be passed in the Javascript function we
output. Notice that I have grabbed the text from the databound cell so
that I can reattach it to the HTML we are adding to the cell. If I
didn't it would be overwritten by the LiteralControl text we are adding.
This doesn't do exactly what you want but it should give you some idea
how you could shoot stuff out to the Row and Cell.

Hth,

Alex - AspFriends.com Moderation Team
Microsoft MVP - ASP.NET

***********************************************************
Translate C# code to VB.NET code at
http://aspalliance.com/aldotnet/examples/translate.aspx
***********************************************************

[Original message clipped]

| = JOIN/QUIT

Reply to this message...
 
 
System.Web.UI.HtmlTextWriter
System.Web.UI.LiteralControl
System.Web.UI.Page
System.Web.UI.WebControls.DataGrid
System.Web.UI.WebControls.DataGridItemEventArgs
System.Web.UI.WebControls.DataGridItemEventHandler
System.Web.UI.WebControls.ListItemType
System.Windows.Forms.DataGrid




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com