<ContentTemplate>
<div class="detail_purchase_button">
<a class="commandbutton" href='/Courses?RestoreFilters=1'>Return to Catalog</a>
<%# linkAddToCart %>
</div>
</ContentTemplate>
string url = "/Cart?AddItem={0}", DataItemID;
linkAddToCart = new HyperLink();
linkAddToCart.CssClass = "commandbutton";
linkAddToCart.NavigateUrl = url;
linkAddToCart.Text = "Add To Cart";
The button within the anchor tag shows up on the page. However, the Hyperlink button does not appear at all.
The second block of code is running in the Page_Load event (I will put it in a method after I get it to work) and is referencing a public Hyperlink field.
Thanks for any help.
Define the hyperlink control via markup in the presentation file.
<asp:HyperLink id="lnkAddToCart" runat="server" />
Place it where you need it to be. You can still reference its properties in your codebehind.
lnkAddToCart.CssClass = "commandbutton";
lnkAddToCart.NavigateUrl = url;
// etc.
If you were to define the control dynamically instead, you would need to add it to the appropriate container, such as a panel or placeholder
<asp:Panel id="theContainer" runat="server" /> or
<asp:PlaceHolder id="theContainer" runat="server" />
...
// define the HyperLink as in your original code snippet
theContainer.Controls.Add(lnkAddToCart);
However, unless you absolutely need to dynamically create the control, adding it to the the ASPX at design time is best. You can always set Visible="false" (to the markup, .Visible = false; in code) if it does not need to be displayed all the time.
Your question seems a bit vague but I am trying to answer based on what I understand out of it. Please ignore if I misunderstood your question.
It is not possible to add an hyperlink like this. First put a placeholder(i.e. panel) within the contenttemplate and then add the hyperlink into the placeholder from code behind.
<a class="commandbutton" href='/Courses?RestoreFilters=1'>Return to Catalog</a>
<asp:Panel id="pnlLink" runat="server"></asp:Panel>
</div>
</ContentTemplate>
And then in the code behind
string url = "/Cart?AddItem={0}", DataItemID;
linkAddToCart = new HyperLink();
linkAddToCart.CssClass = "commandbutton";
linkAddToCart.NavigateUrl = url;
linkAddToCart.Text = "Add To Cart";
pnlLink.Controls.Add(linkAddToCart);
Try setting an ID to the linkAddToCart control that you create dynamically. Your code does not define an ID for it. With that being said, I would recommend doing this the way Anthony suggested. I don't see the need to do this kind of thing. If you want to have some logic for making appear in certain cases, just define it in your markup with Visible="false" and make it visible when you need to.
Related
I know its maybe unusual, but i want add htmlGenericControl to a div (it's outside a repeater) in ItemDataBound from CodeBehind..
HtmlGenericControl slider = (HtmlGenericControl)e.Item.FindControl("slider");
htmlGenericControl input = new HtmlGenericControl("input");
input.Attributes.Add("type", "radio");
input.Attributes.Add("name", "slide_switch");
input.Attributes.Add("id", string.Format("projectImage-{0}", item.ProjectImageId));
slider.Controls.Add(input);
but its return null everytime. this is the aspx code:
<div class="slider">
<asp:Repeater ID="rptProjectImages" runat="server" OnItemDataBound="rptProjectImages_ItemDataBound">
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
</div>
Didnt work with asp.net too long, but probably this should help
<div id="myDiv" runat="server">...</div>
and in codebehind myDiv should be accessible.
Several issues with this code.
First, your div is client-side only, from server-side point of view it's just a string. Turn it into server-side control with:
<div class="slider" runat="server" ID="slider">
Second, FindControl looks for immediate children only, in your case children of the repeater item. slider is not one of those. Moreover, it is not a part of the repeater item template and should be accessible as in in code behind, so just
slider.Controls.Add(...
That is unless slider and the repeater you showed are a part of some other template of some "outer" control. In which case make sure to use that "outer" control to call FindControl on.
Finally, don't mess with id. I bet this is either going to be overridden by ASP.NET, or will cause issues on the page. Instead set client ID mode to static and assign ID property:
input.ClientIDMode = ClientIDMode.Static;
input.ID = string.Format("projectImage_{0}", item.ProjectImageId);
This is eventually output the same value for id you needed, but in more ASP.NET compliant way. One note though is that I replaced "-" with "_" - server side controls cannot have hyphens in ID
FindControl finds a control within another, but does so looking for the control's id. Your "slider" control has no id, it uses a class named "slider" but has no id.
You will need to define the control as
<div runat="server" id="Slider" class="slider">
<asp:Repeater ID="rptProjectImages" runat="server" OnItemDataBound="rptProjectImages_ItemDataBound">
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
</div>
The runat="server" tells the framework to instantiate that control in your code behind. The id will be the name of the object that is that control. Then in your code, you can do
htmlGenericControl input = new HtmlGenericControl("input");
input.Attributes.Add("type", "radio");
input.Attributes.Add("name", "slide_switch");
input.ID = string.Format("projectImage-{0}", item.ProjectImageId);
Slider.Controls.Add(input);
I want to change the source of the iframe from a listbox. And here is my code.
<iframe id="iframeID" frameborder="0" width="100%" height="300px"></iframe>
<asp:Button ID="btnView" runat="server" Text="Pop Up HTML Preview"
OnClientClick="ShowPopUp();" />
ShowPopUp = function() {
var x = document.getElementById('<%=ListBox1.ClientID %>');
var val = x.options[x.selectedIndex].Value;
document.getElementById("iframeID").src = val;
But error says I have an undefined iframe.
What is wrong?
Please help me. Thanks :)
You may change the source using jquery:
$("#content").attr('src', 'http://your new source here');
so you can invoke this method when the selected item in the listbox is changed.
If it doesn't do anything from C#, make a normal button - ASP tends to add fancy prefixes to its controls' IDs (like ContentPlaceHolder1_btnView), and those can't be accessed that way by .ID from C# code. So you can either chceck these IDs (which are able to change if you move them somewhere else) by browser's source code inspector, or use
<input type="button" ID="btnView" Value="Pop Up HTML Preview"
OnClick="ShowPopUp();" />
(Modify your JS accordingly)
use value, JavaScript is case sensitive
var val = x.options[x.selectedIndex].value;
and change OnClientClick as below
<asp:Button ID="btnView" runat="server" Text="Pop Up HTML Preview"
OnClientClick="ShowPopUp(); return false;" />
Button click event make full post back request to server. So again your Ifram will have default src value after postback. Here you have not given value as default src. So nothing will get after click on button. after you add return false button click will not postback after click.
I have a asp label that I need to be able to change according to the code behind. How can I do this?
ASPX: (The first part works correctly only for "TestA#abc.com" and the second part dynamically changes the label (EmailLabel) according to the "if" statement in the code behind. How can I integrate these two so the label is mailto? Thanks.
<p>Email at TestA#abc.com.</p>
<p>Email at <asp:Label ID="EmailLabel" runat="server"></asp:Label>.</p>
Code Behind:
public changeLabel()
{
if (//Some Condition Here)
{
this.EmailLabel.Text = "TestA#abc.com";
}
else
{
this.EmailLabel.Text = "TestB#abc.com";
}
}
What you are trying to do there won't work. Label's render out as <span> tags, so it will never be "clickable". You want to do something more like this:
<p>Email at TestA#abc.com.</p>
<p>Email at <asp:LinkButton ID="EmailLabel" runat="server"></asp:LinkButton>.</p>
And then instead of changing the Text property, change the NavigateUrl property.
You could also use an HtmlControl, which is basically a standard HTML tag that you add the runat="server" attribute to.
<p>Email at <a id="EmailLabel" runat="server" href=""></a>.</p>
You would then be able to modify this <a> tag via server side code, the properties will be slightly different, but now you've got a real live anchor tag to work with.
This other SO question might also be helpful: How to launching email client on LinkButton click event?
<html>
<body>
<span id="foo"
onclick="document.getElementById('foo').innerHTML = 'never say never'"
>
Click Me!
</span>
</body>
</html>
For your Label, just remember that .Text is the server equivalent of .innerHTML so you can put whatever HTML you want right into the asp:Label when setting .Text. Just watch out for cross-site-scripting exploits.
I am using a button that has to be invible and should be used by a javascript function.
<asp:Button ID ="btnDummy1" runat="server" Visible ="true" OnClick="btnSubmit1_Click" width="0px" height="0px"/
I cannot keep visible = false as it the javascript will not use invible content in the poage. I havetried to give width=0 and height=0, still it showws up in Chrome. What do you guys think i should do?
Thanks in advance :)
A pretty clean approach in ASP.Net it give it a "hidden" class:
<asp:Button ID ="btnDummy1" runat="server" CssClass="hidden" />
Then in your stylesheet:
.hidden { display: none; }
If you set it to Visible="False" then the code will not be executed.
Instead I think you should wrap it in a <div> and set display:none via css:
<div style="display: none;">
<asp:Button ID ="btnDummy1" runat="server" OnClick="btnSubmit1_Click" />
</div>
adding style="display:none" would hide the button till you make it visible again
<asp:Button ID ="btnDummy1" runat="server" OnClick="btnSubmit1_Click" style="display:none"/>
How about
style="display:none"
for the button instead of Visible = "true".
Can you just use a hidden form field in this case? This is generally the preferred method of passing information along.
See http://www.tizag.com/htmlT/htmlhidden.php
<asp:Button ID="btnMyOrders" runat="server" Text="my orders" CssClass="dropdown-item" OnClick="btnMyOrders_Click" Visible="False" />
Example then in Form(){
btn.Visible = true; to show it again
}
I think the first question you should ask yourself is : why would I put in my HTML doc a button that should not be visible ?
An HTML document should be used ONLY TO DESCRIBE A CONTENT'S SEMANTICS. So an HTML doc should ONLY contain the content you want to publish and extra data to explain the content's SEMANTIC !! NOTHING about the way it is displayed, NOTHING about the way it behaves !!
All displaying and behaviors questions should be managed with CSS and Javascript, NEVER within HTML itself.
Any element needed for Javascript only purpose should be added in the doc by Javascript itself !
You should never find, in an HTML doc, such things as prev/next buttons for a javascript picture gallery for example. The HTML doc should only contain the pictures list, than your JS script will change the way this list is shown, making it a eye candy gallery and add buttons for navigation.
So in your example, your saying you want to add in your HTML doc an invisible button with some Javascript actions on it.... that's probably an example of some content that should never be in the HTML doc.
I am working on an asp.net web application where I have predefined panel in my project with
CSS. Now i want to create another panel with same design and CSS at run time at multiple times. I have a button control when i will click that button it will add another panel.
Please help me to add another panel with same criteria.
If it is something that you plan on reusing, I'd suggest you utilize a user control for this. You can them simply add a new instance of the control on your page.
A few things worth looking into:
https://web.archive.org/web/20210707024005/http://aspnet.4guysfromrolla.com/articles/081402-1.aspx
http://aspalliance.com/565
http://msdn.microsoft.com/en-us/library/c0az2h86.aspx
If you wanted to accomplish this with a postback to the page, add this to your event...
//MyControl = Custom User Control
var myControl = (MyControl) Page.LoadControl("MyControl.ascx");
this.ControlContainer.Controls.Add(myControl);
Like RSolberg said, you could write a User Control and add it multiple times:
<my:UserControl id="MyControl1" runat="server" />
<my:UserControl id="MyControl2" runat="server" />
<my:UserControl id="MyControl3" runat="server" />
Of course, your User Control can be as simple or as complex as you like, thus having repeated functionality on your page.
However, depending on your exact needs you might want to consider something like an ASP.NET Repeater, or ListView, or DataGrid control. With something like a Repeater, you can bind data to it, and have that data be displayed in a list/grid, that has a common look and feel. You can give your Repeater a HTML/CSS template for the header, items, and footer sections too to make it look consistent and professional.
<asp:Repeater id="MyRepeater" runat="server">
<HeaderTemplate>
<h1>Products</h1>
</HeaderTemplate>
<ItemTemplate>
<p>
Product name: <%# Eval("ProductName") %>
</p>
</ItemTemplate>
</asp:Repeater>
and in your code just do this:
MyRepeater.DataSource = products;
MyRepeater.DataBind();
There are many ways of doing what you're asking in ASP.NET - be a bit more specific and we'll be able to give you more specifc help.
Couldn't you just create a new panel in the code behind on the button's "On_Click" event? That would be my suggestion. You may need to have a placeholder to add the panel into something so it appears on the page.