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



    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, May 25, 2005
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.