Creating Buttons and text fields dynamicaly
Hi,
I want to create several textboxes and buttons in my page.
Each button has a unique ID, an they all must call the same function, but will behave defferently, like by passing a parameter...
Do you have any idea how i can do that ???
Regards
Ahmed Abughoush
Re: Creating Buttons and text fields dynamicaly
hmmm. It sound slike you need to create a fucntion and pass parameters to it on the button click
Code:
Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
Dim strFileName As String = "test.csv"
Dim strFileLocation As String = "C:\Inetpub\wwwroot\reports\"
Export(strFileLocation & strFileName, dsResults.Tables(0))
End Sub
Re: Creating Buttons and text fields dynamicaly
Dear davebat;
This function only runs at a button click, but i did not understand the part which submits the parameters..
I didn't get this part
Thanks
Re: Creating Buttons and text fields dynamicaly
Why not assign a different CommandName property to each button and then you can use this in the shared click handler.
DJ
Re: Creating Buttons and text fields dynamicaly
Hi...
Can you guide me how i can do that ???
Thanks
Re: Creating Buttons and text fields dynamicaly
First create button and assign a CommandName and event handler.
Code:
Button btnNew = new Button();
btnNew.ID = "btnNew";
btnNew.Text = "Click me";
btnNew.CommandName = "Button_Clicked";
btnNew.Click += new EventHandler(btnNew_Click);
Obviously you'll have to add this to a control collection somewhere to make it appear on the page.
Then within the event handler you can access the CommandName property:
Code:
void btnNew_Click(object sender, EventArgs e) {
if (sender.CommandName == "Button_Clicked") {
//The dynamic button was clicked!
}
}
HTH
DJ
Re: Creating Buttons and text fields dynamicaly
Wow...
Thanks a lot dj4uk
Thats exactly what i needed :thumb:
Re: Creating Buttons and text fields dynamicaly [RESOLVED]
One more thing....
How can i place them in my webpage... ??
Can I add them as to a cell in a table object ??
:blush:
Re: Creating Buttons and text fields dynamicaly
Add placeholder controls wherever you might want to add controls. If you don't add anything then nothing is displayed on the page.
To add a control just do the following after you have created the control dynamically.
Code:
phldNew.Controls.Add(btnNew);
where phldNew is the placeholder control ID.
HTH!
DJ