Render a control to a DataGrid durng DataBind using custom method

Discussion in 'ASP .Net Web Controls' started by Grant Harmeyer, May 25, 2005.

  1. 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

    else

    {

    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);

    l.RenderControl(h);

    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
    Grant Harmeyer, May 25, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    718
  2. Replies:
    1
    Views:
    374
    Teemu Keiski
    Jul 2, 2003
  3. Stephen Miller
    Replies:
    12
    Views:
    766
    Stephen Miller
    Jan 20, 2004
  4. Michael
    Replies:
    0
    Views:
    221
    Michael
    Dec 29, 2003
  5. Glenn Gillen
    Replies:
    0
    Views:
    274
    Glenn Gillen
    Nov 17, 2006
Loading...

Share This Page