hi...........
i have try to create some dynamic control at run time in my application .

but i cant handle it event or cant mantain its state.


can any body tell me ?

Try this sample code.

protected void Page_Load(object sender, EventArgs e)
    {
            Button btn = new Button();
            btn.ID = "btnTest1";
            btn.CommandArgument = "arg" + i;
            btn.CommandName = "YourCommandName";
            btn.Text = "Delete";
            btn.Click += new EventHandler(btnTest_Click);
}


protected void btnTest_Click(object sender, EventArgs e)
    {

    }

hi ramesh thank for reply but i want to create dynamic but not in page load event . i want to create but in tree view template when its expand event fire i have try to create button or any control but when next any event fire all dynamically generated control get remove i cant handle it state

You can create dynamic control in Page Load or Page Init events only. Otherwise they won't be retained in ViewState and vanished between postback.

If you want to create a dynamic control when tree view is expanded, then check the value of Request.Params["__EVENTTARGET"] in the page load event and create dynamic control in the page load event based on that.

The Request.Params["__EVENTTARGET"] may have your tree node information if the tree view node post back the page.

thats means no any solution for creating control in application expect page load event? is there anyway i can store this in viewstate ...if yes then what about the event handling

No. creating controls after page init or page load will lead to problems.

A good approach to handle tree node event to create dynamic control is to check the value of Request.Params["__EVENTTARGET"] in page load and then create it.

Sorry i missed the last line in the page load event in the code in my fist post.

protected void Page_Load(object sender, EventArgs e)
    {
            Button btn = new Button();
            btn.ID = "btnTest1";
            btn.CommandArgument = "arg" + i;
            btn.CommandName = "YourCommandName";
            btn.Text = "Delete";
            btn.Click += new EventHandler(btnTest_Click);
            PlaceHolder1.Controls.Add(btn);
           //Here PlaceHolder1 is a place holder control.

}


protected void btnTest_Click(object sender, EventArgs e)
    {

    }

Ramesh:

No. creating controls after page init or page load will lead to problems.

A good approach to handle tree node event to create dynamic control is to check the value of Request.Params["__EVENTTARGET"] in page load and then create it.

if you are using a placeholder it not complusory that the control is created in the page load or init
try it out

I have posted the tested version of the code(except last line in page load event which was missed in copy/paste).

Even if you are using place holder, then you should add dynamic controls only page load or page init events. Adding dynamic controls in child control's event like button_click will be vanished between postback.

i know it disappears, but is there anyway that i can hold that control and it should not get disappeared on yhe postbakc..!

also i want number of dynamic control add in tree view instead of tree node also maintain its command argument and some info for nest processing i.t node contain button or some lebal or check box
label show node info
check box contain some other operation
button trigger some other
when perticular button in tree view trigger not only its state mantian but also all open node contain dyanamic control also maintain

amar_interface,
One thing to remember : Instantiate controls in either Page_Init or Page_Load event. You must have some home-made measure to save the state.

{
create an interface of an object

create the properties for the control by using the object

create an event for that control by using the operator "+="

add that control to the container.


ex:

{
button b=new button();
 
b.text="ADD";
b.autosize=true;
b.location=new point(x,y);

b.click+=function;

this.Controls.Add(b);
}

public void function(.......,.......)
{
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.