Render a control to a DataGrid durng DataBind using custom method

Render a control to a DataGrid durng DataBind using custom method

Post by Grant Harm » Fri, 27 May 2005 06:10:06

OK. I have a web page with a DataGrid that I would like to dynamically place
LinkButton controls in the rows.
I have written a method to generate the LinkButton control, but when i
render it, the href="__doPostBack()" is missing from the LinkButton HTML
output. Anyone tried this before?

CS Code:
protected string IsEnabled ( string userName )


// Local Variables

UserAccount u = null;

LinkButton l = null;

HtmlTextWriter h = null;

StringBuilder sb = null;

StringWriter sw = null;

// Begin

l = new LinkButton();

u = new UserAccount(userName);


// Set the LinkButton base stuff


l.CausesValidation = false;

l.CommandArgument = u.UserName;

l.Command += new CommandEventHandler(this.ToggleEnabled_Command);

l.Font.Bold = true;

if ( u.Enabled )


l.CommandName = "Disable";

l.Text = "Yes";

l.ForeColor = System.Drawing.Color.Green;

}// end if



l.CommandName = "Enable";

l.Text = "No";

l.ForeColor = System.Drawing.Color.Red;

}// end else


// Render the Control


sb = new StringBuilder();

sw = new StringWriter(sb);

h = new HtmlTextWriter(sw);


return h.InnerWriter.ToString();

}// end IsEnabled

I then put this in a DataGrid TemplateColumn:

<%# this.IsEnabled(DataBinder.Eval(Container.DataItem,
"UserName").ToString()) %>

The HTML output in the DataGrid does almost everything it's supposed to. The
text is displayed properly (color and phrase), but the <A> tag has no HREF
attribute for the PostBack JavaScript:

<a style="color:Green;font-weight:bold;">Yes</a>

I've never tried to return a WebControl from a method that is invoked by
simple DataBinding before, so if anyone has any suggestions that would be
great. I have a feeling there is a much easier way to do this.

BTW for this app, the control should be created in the CS code and added at
runtime. Any Ideas?

Grant Harmeyer

1. How could I ask Thread B to call B().Method() from inside Thread A's run?

2. How could I ask Thread B to call B().Method() from inside Thread A's run?

I agree with jmj's solution, you would want to send a signal of some
sort to Thread B from A when some event occurs in A. A queue is one
way to do it, but keep in mind that there are numerous ways to
communicate between threads/processes (queue, pipe, exit status,
TCP/UDP message, etc.). I believe one of the first couple chapters in
O'Reilly's "Programming Python" book discusses most of these methods.
Choose the simplest one that meets your needs.

3. Printing report b after report a prints using report a's variables

4. using "if" but omitting the #n/a's

5. Why does Form-A's instance fire Form-B's event??

6. Copy all data from a:Class A's fields to the namesake fields of b:Class B.

7. Capture image of App#A's window from App#B

8. equation that will filter out #N/A's

9. lookup only finds #n/a's

10. Quick A's to easy(?) Q's appreciated...

11. how to conver #n/a's to 0

12. #n/a's

13. "Matching" column A's values in column B

14. How do I only get valid "answers" in a VLOOKUP function (no #N/A's

15. best way to sum a column with #N/a's?