1

I'm trying to create ASP.NET buttons programmatically inside an update panel in my SharePoint instance, but because of the page life cycle, I can not attach server side events on buttons.

Here is the code:

TableCell tcellbutton = new TableCell();
b.Click += new EventHandler(b_Click);
b.CausesValidation = true;
tcellbutton.Controls.Add(b);
tr.Cells.Add(tcellbutton);
table.Rows.Add(tr);
panel1.Controls.Add(table);

void b_Click(object sender, EventArgs e)
{
    string studentnumber = (sender as Button).ID.ToString().Substring(3, (sender as Button).ID.ToString().Length - 3);
    TextBox t = panel1.FindControl("txt" + studentNumber) as TextBox;
}

Is there another way to create and attach buttons in Sharepoint?

2
  • Fix your formatting and put in a full code snippet (what method or event are you adding the controls in, for example - this is almost certainly the problem and you've left it out) and it might get you a little more help. Commented Oct 20, 2010 at 8:03
  • Hi Ryan, My problem here is basically I can not attach events to controls during runtime, It has to be during the Page Initialization, I can override and run the same code without any error but my question is if it is available in the runtime as well.. Commented Oct 20, 2010 at 8:39

3 Answers 3

2

Ok here is how I solved it, Thanks for all replies, I was looking for a way to attach an event to a button that is created dynamically during runtime (after initialization). Hope It works for others as well.

<script type="text/javascript">
    function ButtonClick(buttonId) {
        alert("Button " + buttonId + " clicked from javascript");
    }
</script> 

protected void Button_Click(object sender, EventArgs e)
{
    ClientScript.RegisterClientScriptBlock(this.GetType(), ((Button)sender).ID, "<script>alert('Button_Click');</script>");
    Response.Write(DateTime.Now.ToString() + ": " + ((Button)sender).ID + " was clicked");
}    

private Button GetButton(string id, string name)
{
    Button b = new Button();
    b.Text = name;
    b.ID = id;
    b.Click += new EventHandler(Button_Click);
    b.OnClientClick = "ButtonClick('" + b.ClientID + "')";
    return b;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to accept your own answer as correct (by pressing on a tick)
1

You should add your code in PreInit event, code below work good:

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);
    Button bb = new Button();
    bb.Click += new EventHandler(bb_Click);
    bb.CausesValidation = true;
    bb.ID = "button1";
    Panel1.Controls.Add(bb);
}

private void bb_Click(object sender, EventArgs e)
{
    Response.Write("any thing here");
}

4 Comments

is it possible to call OnPreInit() in another Event ?
You don't call OnPreInit - the web part infrastructure calls it on your behalf. nishantrana.wordpress.com/2009/02/14/…
you just override OnPreInit, not call it.
@Kubi I think you can't, try below solution of @Kubi
1

You are creating dynamic controls. Your code should execute in each PageLoad event. Remove IsPostBack for the part of code where you are creating the buttons is my advice.

If you don't do this, you will create the controls, but each time when PageLoad event occurs, your control will be deleted and the application will not follow your events. With other words you should always recreate the controls.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.