0

I am having an ASP.WebSite in which I have added some form controls dynamically on aspx.cs using

form1.Controls.Add( " Object of Control ");

Also a have already added a button on form in aspx file using

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Add" UseSubmitBehavior="false" />

now when I run the program it converts the Button1 into an HTML submit button as below

<input type="button" name="Button1" value="Add" onclick="javascript:__doPostBack('Button1','')" id="Button1" />

and the form tag becomes

<form name="form1" method="post" action="Add1.aspx" id="form1" enctype="multipart/form-data">

when I click the Button1 it submits the form instead of calling the function on Code Behind.

How am I able to call the method Button1_Click specified on OnClick event of Button1?

Please Help.

1
  • This statement onclick="javascript:__doPostBack('Button1','')" will cause to call Button1_Click event. Are you sure you are not getting this event fired? Commented Apr 25, 2012 at 12:32

3 Answers 3

4

Have you implemented a click-event handler in codebehind?

protected void Button1_Click(Object sender, EventArgs e)
{
    Button btn = (Button)sender;
}

Button.Click Event

I don't know how this question is related to your dynamic control question since Button1 is added declaratively.

Sign up to request clarification or add additional context in comments.

7 Comments

I edited the OP's title. You're right didn't have anything to do with dynamic controls.
Actually, I want to get the values of Controls which are added dynamically on the onclick event of Button1 which is added declaratively. But when I click on Button1 it just submits the form intead of calling Button1_Click. Is there any other way to dynamically add Controls and get values on buttonClick event?
@PrasadJadhav: 1.) You haven't shown how you're adding controls dynamically(" Object of Control " is not a control). 2.) Submitting a page is typical for a postback. So before the button's click event is handled on the server it must be submitted/postback. 3.) What kind of controls do you want to add dynamically? 4.) Where do you want to add them(f.e. in a Panel/Div/GridView etc). 5.) How do you want to detect these controls(f.e. by a similar ID's or the location)? 6.) Why are you adding them dynamically at all and not using any builtin web databound controls?
OK. 1) Most of the controls are TextBoxes and I am adding using TextBox TextBox1 = new TextBox(); TextBox1.ID = name; form1.Controls.Add(TextBox1); 3) Typically I am adding TextBox, DropDownList and CheckBox. 4) I am adding them directly on form. Using Style control I am giving position on the form.
5) To detect I am having a String variable which has all the names of ID's of dynamically added controls. and i am getting value using Textbox tbox=(TextBox)Page.FindControl("name"); 6) According to req the variable to be entered by user are changing. so indtead of creating seperate form i am adding controls dynamically on the same page. If i start creating separate page for each req then it will become so hard me to keep track of each page as there are lots of diff. req pages. My prob is I just want to get values entered by user in dynamic controls and add them to database.
|
1

I shoud have added the button control dynamically as follow:

    b1.ID = "EDIT";
    b1.Text = "Add";
    b1.Click +=new System.EventHandler(this.Button1_Click);
    b1.Style["Position"] = "Absolute";
    b1.Style["Top"] = "10px";
    b1.Style["Left"] = "20px";
    form1.Controls.Add(b1);

It is now calling the function Button1_Click on the click event of Button1. I this case I have to create the function Button1_Click manually i.e. not just double clicking on the design page.

Comments

-2

the controls you are adding dynamically, you have to add it on page load.

Then it will work fine.

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.