hardya
hardya

Reputation: 51

dynamic linkbutton not calling command when clicked

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

Upvotes: 1

Views: 1119

Answers (1)

Hugo Yates
Hugo Yates

Reputation: 2111

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

Upvotes: 1

Related Questions