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.
Related
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.
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.
I am making an asp.net web application which has to play videos. In my start page I have a hyperlink for each video. All the hyperlinks are identical, except their names. This Means they all link to the same page. Im interrested in knowing wether there is an option to know which hyperlink was clicked. I would like to retrieve the name of the hyperlink.
My code for generating the hyperlinks looks as follows:
foreach (FileInfo i in corFiles)
{
HyperLink t = new HyperLink();
t.Text = i.Name;
t.NavigateUrl = "page.aspx";
CorrectArray.Add(t);
}
return CorrectArray;
The text of the hyperlink is Unique to a video, which Means I can change the src destination of the video to play based on the text name. So the question goes as follows. Are there any way of retrieving the text name of the hyperlink when it is clicked by the user?
I hope you can help! Thanks in advance.
Regards
Magnus
You can add a link buttons instead of hyper links, if you want to make a post back to the same page.
foreach (FileInfo i in corFiles)
{
LinkButton t = new LinkButton();
t.Text = i.Name;
t.Click += new EventHandler(DynamicClick);
t.CommandName = i.Name;
CorrectArray.Add(t);
}
void DynamicCommand(Object sender, CommandEventArgs e)
{
// using e.CommandName and e.CommandArgument you can differentiate the hyperlinks
}
If i understood your question,
you have to add the name attribute in the hyperlink tag and get its values in the code behind by fetching names
You can use JQuery that is integrated into .NET to get the text of the hyperlink upon it is clicked by the user. To be able to access the Hyperlink's text from Javascript simply add it to a custon tag inside the link to have something like this <a href="page.aspx" htext="hyperlink text">. As the hyperlink html is generated from .NET server side code you have to generate this custom tag from the .NET server side context.
Why don't you use a Querystring?
t.NavigateUrl = "page.aspx?video=<someid>"
and in the page you parse the querystring to show the correct video?
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.
How do I add button dynamically using C# on ArcGIS?
I can create that using XAML, but I can't write that on C#, I checked on the website on ArcGIS, they allowed to add graphics dynamically, but there is no sample to show how to add controls such as button dynamically.
Can anyone provide me some sample code?
Thanks
Add an ElementLayer to your map (ether in xaml or in code ), then add your button to the element layer.
MyMap.Layers.Add( new ElementLayer() { id="myElementLayer" } )
// create your button.....
MyMap.Layers[ "myElementLayer" ].Add( myButton );
there is an online sample here
Use code behind to add the button during runtime. Anything you do in XAML can be done in code behind.