1

New to dynamic controls, but until now I have been creating them successfully in a template field in my gridview, recently switched from a hyperlink to a link button and had to make some changes but still not working

In my page I have the following code (abridged to salient parts)

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        ...
        ...
        TemplateField tf = new TemplateField();
        tf.HeaderText = "Action";
        tf.ItemTemplate = new AssignPage.MyTemplate(..., mylb);
        GridView1.Columns.Add(tf);


protected void Page_PreInit(object sender, EventArgs e)
{
    LinkButton lb = new LinkButton();
    lb.Text = "AssignAll";
    lb.Command += new CommandEventHandler(AssignAll_Click);
    lb.CommandName = "XXX";
    this.mylb = lb;




protected void AssignAll_Click(object sender, CommandEventArgs e)   
{
    string[] arg = new string[2]; // BREAK POINT HERE
    arg = e.CommandArgument.ToString().Split(';');
    ...
    ...
    Response.Redirect("BaseAndRepeats.aspx?id=" + r.Event.ID);

In the template class I have I have

    LinkButton lb;

    public MyTemplate(..., LinkButton _lb)
    {
        ...
        lb = _lb;
        ...



    public void InstantiateIn(System.Web.UI.Control container)
    {
            ...
            ...
            // various conditional statements

            lb.CommandArgument = mylist[rowCount].ReqtID.ToString() + ";" + mylist[rowCount].RotaUser;
            container.Controls.Add(lb);
            ...

The break point in the handler is never reached

I thought I was creating the linkbutton in the right place the linkbutton appears in the grid quite happily when I click on the linkbutton there is a call to Page_PreInit and to Page_Load and as I expected it is a postback. But AssignAll_Click is never called.

In the browser footer it shows "javascript: __dooPsotabck(..." when you hover over the buttonlink

hardya
  • 51
  • 8

1 Answers1

1

I believe the problem is this line:

lb.Command += new CommandEventHandler(AssignAll_Click);

Change it to:

lb.Command += AssignAll_Click;

Edit: Also you want to move it from Page_PreInit to Page_Init but you may find more success with it in Page_Load (outside the IsPostBack). Here's an example that works for me:

On the ASPX page:

<asp:Panel ID="TestPanel" runat="server" />

Codebehind:

protected void Page_Init(object sender, EventArgs e)
{
    //Init used because TestPanel doesn't exist yet
    CreateTestButton();
}

private void CreateTestButton()
{
    var lb = new LinkButton();
    lb.Text = "hello";
    lb.Command += lb_Command;
    TestPanel.Controls.Add(lb);
}

void lb_Command(object sender, CommandEventArgs e)
{
    throw new NotImplementedException();
}

Going over your code it looks like your adding another column into your GridView and creating a button inside that column for each row - but you've got the column being added on Page_Load and the button being created and bound to it before in Page_PreInit

Hugo Yates
  • 2,081
  • 2
  • 26
  • 24
  • Didn't seem to work. I also read that the control should be added to the page but I am not sure how to do that and I never did it with my other controls. – hardya Feb 05 '15 at 16:13
  • So, I have not finalized this as an answer yet, but I have made 'the creation of the templatefield passing the linkbutton to its template and adding the templatefield to the grid' to occurr on postback as well. This did not occurr to me since I was immediately redirecting and I didn't think the control would need to be actually back in the templatefield and grid for the event to be called, I thought that was only needed for rendering. – hardya Feb 05 '15 at 16:41
  • Edited my response. Yeah controls need to be created on PostBack or they simply stop existing and their events don't get triggered. – Hugo Yates Feb 05 '15 at 16:45
  • This becomes even more confusing now. I read that I MUST create dynamic controls in Page_PreInit. However I was creating them in the template InstantiateIn because that was the only place I would know how many controls which controls I actually needed, what their attributes should be set to! – hardya Feb 05 '15 at 16:49
  • It depends what your doing, a small thing like a button can be (re)created in Page_Load if it's a child of an existing control but if you want to dynamically set the master page for example you need to do that in PreInit – Hugo Yates Feb 05 '15 at 16:58
  • Re: "...Yeah controls need to be created on PostBack or they simply stop existing...". I was already constructing the control I think the issue was I didn't put it anywhere, the first change I made was to make sure that as well as being created it was added to the grid again on post back and that fixed it. The second thing I did was to move the creation into the template InstantiateIn which was the only place I could have the knowledge I needed for the controls. It still works fine. I have nothing in preinit – hardya Feb 05 '15 at 19:11