How to dynamically add a button using html table in asp.net - c#

I want to add dynamically button in html table.
I created a master page and another subpage.
On the sub page, I want to dynamically add a button in html table.
This is my code:
Button btnSend = new Button();
btnSend.ID = "btnSend";
btnSend.Text = "Send";
btnSend.Click += new System.EventHandler(btnSend_Click);
this.form1.Controls.Add(btnSend);
But I get an error:
form1 is not accessible from sub page.
Please help me to dynamically add a button into the html table.

If you use asp.net then you have no forms. So simply insert a button from the toolbox and set buttonName.visible = true or false as you need.

If you want to add dynamic controls to your sub page, then i suggest you to add a panel, and then simply use panel.controls.add(your button) and thats all.

Do you create the HTML table on codebehind? If so, you may add the button there.
If not, you may try putting a Literal there, and fill it with the code for an HTML button if needed.

Related

Possible to have dynamic content with accessable controls in asp.net/C#

I really hope that someone can help, pointing me in the right direction. I'm still new to the more advanced features of asp.net / C#.
What i need is a div, in that div i would want some informations from the database. furthermore i want a textbox with a number and an arrow up and down to increase/decrease the amount in the textbox. And at last a button outside the div to submit the value.
Now the tricky part (for me), is that one div is not enough, I need one div for each "person" in the database. i can easily make that, but.. if i add it dynamically from the .aspx.cs file i am not able to access the value like : textbox1.text; because textbox1 does not exist in the code before it is created.
I have looked at listview and a repeater, but those seems more like they're for making lists, and i need more than that, as i need some functionally too.
The way i would do it now is to add the div by innerhtml. and then adjust the amount by a for loop which also inserts the information from the database which i got in an array. But as said, that doesn't really do the trick when i need to access the textbox and stuff.
Thanks in advance for just looking at it.
EDIT:
I'm not looking for at complete solution, i just want some directions.
I will asume by your post description that by asp.net you mean webForms. If you're new on asp.net development, you have to know that there are different development, in the past (but still very commonly used) you would use WebForms, now at days the trends are using asp.net MVC framework.
now back to your question:
in asp.net WebForms, you have your code defined in two sides: your markup (HTML code normally in a .aspx file) and your code-behind (c# or vb code normally in a .aspx.cs or .aspx.vb).
what I would suggest you to do, is to add your logic for retrieving data from your database in your page_load() function of your code-behind, with this data you would normally use a loop to read all your results and for each result you would create your div with your textbox inside (the trick is using native .net framework classes instead of inserting the HTML directly). a simple example:
protected void Page_Load(object sender, EventArgs e)
{
string[] personsIDs; //<-- this came from your database
Dictionary<string, TextBox> textBoxes = new Dictionary<string, TextBox>();
foreach(string personID in personsIDs)
{
TextBox personTextBox = new TextBox();
personTextBox.ID = "textBox"+personID;
textBoxes.add("textbox"+personID, personTextBox);
}
I explain you:
first, I'm assuming there is an array of the personsIDs, so I created a dictionary (key -> value object) using strings as keys and TextBoxes as values.
then using a foreach I read all the personsID's, and for each one of them I create a new TextBox UIControl and add it to the dictionary using the personID as key. I also added its ID property as "textbox"+PersonID.
this way you can access your textboxes from code-behind by using your dictionary:
textBoxes["textbox"+personID] //(e.g: textBoxes['textBox11'])
but also, since your textBox.ID is equal to textbox+personID, you can also reference it after page has been rendered (e.g. using javascript).
now to add this controlers to your page, just use a container(UIControl) that already exists on your page, and use
container.controls.add(textbox);
this process can be expanded for extra elements, for instance:
1.- have a main placeholder already defined in your page:
<asp:Panel ID= "Panel1" runat = "server">
2.- in your code-behind for each person create a new panel and add all the elements you want inside that panel:
//this goes inside your foreach
Panel innerPanel = new Panel();
TextBox textBox = new TextBox();
Label label = new Label();
innerPanel.controls.add(textbox);
innerPanel.controls.add(label);
////
finally add your innerPanels in to your main panel:
panel1.controls.add(innerPanel);
so there you go, that's basically the idea, hope this helps.

Best control to handle changin images and text in a asp.net button click

I'm working on a asp.net website.. and I need a little help.. please see image as reference...
What I want to happen is when a button is click from the buttons on the right.. text would appear on the boxed space... I don't know which is the best control to use to hold the text on the space I would like text to appear..
which is the best control to hold images and texts at the same time? and how could I connect it to the button on the right?
You could use the HtmlGenericControl class. It has a constructor which accepts a string variable which initializes a new instance of the HtmlGenericControl class with the specified tag:
var div = new HtmlGenericControl("div");
It is also has InnerHtml and InnerText properties.
Setting up a div with the custom content means you could host images, text or basically anything else that you want to stuff inside a div.

Dynamically add a User Control to a page without page postback

I want to add a user control to my pages without page post backs.
I can't use Update Panels because I have file uploads control in the user controls.
I also don't want to put the custom controls in a page and then load it inside a page using jQuery load functions. Similarly I cannot use iframes.
How can I do it through some jQuery or AJAX?
This answer is posted by some user but he/she has removed the answer:
So i am posting his answer
Though i want to add a server controls in my application but i left with no choice except to use HTML Elements
StringBuilder b = new StringBuilder();
HtmlTextWriter h = new HtmlTextWriter(new StringWriter(b));
UserControl u = new Page().LoadControl("~/UserControl.ascx") as UserControl;
u.RenderControl(h);
return b.ToString();
This works only if all the controls in a USER CONTROL is of html type

Creating dynamic hyperlink in asp.net using c#

In my application I have DataSet containing name and Id of user and I want to create a dynamic hyperlink of the all the user name. Please anyone tell me how to create dynamic hyperlink using C#.
As #Ashley John said,
HyperLink DynLink = new HyperLink();
DynLink.ID = "DynLink";
DynLink.Text = "This Link Is been Created Dynamically from code behind";
DynLink.NavigateUrl = "~/TestPage.aspx";
PlaceHolder1.Controls.Add(DynLink);
I have used a placeholder as a container to hold the dynamically generated Hyperlink..
Create a new instance of Hyperlink Control
Set its URL property.
Add the Control to the placeholder(or Gridview controls collection if you are using it inside a Gridview) where you want it to get displayed.
Use a Asp.Net HyperLink control. You can create the url passed to the Hyperlink control by using String.Format().
If you want to create a list of all hyperlinks you could have a look at the Repeater and use Eval("...") in the template to render out the link. That way, you can define more in markup then in code.
We can create Dynamic Hyperlink using following Syntax:
<asp:DynamicHyperlink
ID="string"
Action="Details|Edit|Insert|List"
ContextTypeName="string"
DataField="string"
TableName="string"
OnDataBinding="DataBinding event handler"
OnPreRender="PreRender event handler"/>
For more detail just go to this link. You will get a Demo Project Showing use of Dynamic Hyperlink and Linq.

Passing querystring with HyperLinkField

I have a gridview where one column is going to be a hyperlink that will open a new browswer window and display a file so upon clicking this link I am currently using the NavigateUrl and calling a new page and setting the target to blank. My problem is that in this new page I will need to know the ID of the row that was selected. How can I pass this information? Querystring doesn't seem to work?
Check out HyperLinkField's NavigateUrlFormatString property here.
Instead of using NaviagteURL use a click event, add a session variable, and response redirect to the page. Look for it in the page load on the new page.

Categories

Resources