I need to move this new button into this DIV that already exists. This seems to me like it should work but doesn't. What am I doing wrong?
Button button5 = new Button();
button5.Text = "Five";
button5.CssClass = "buttonCSS";
button5.Click += new EventHandler(button5_Click);
button5.ID = "button5";
this.Controls.Add(button5);
string myscript = #"
var navFooter = document.getElementById('NavFooter');
var button5 = document.getElementById('button5');
navFooter.Controls.Add(button5);
";
Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", myscript, true);
I know the two objects are being found. Any ideas?
(Note: I cannot use jQuery.)
Thank you.
Controls.Add() is C#, not JavaScript. To append an element to another element with JavaScript, use appendChild():
navFooter.appendChild(button5);
Why are you doing this through javascript?
If you change the containing <div> to a <asp:Panel>, then you can add the newly created button straight into this panel. The <asp:Panel> is rendered simply as a <div> html element, and you can add the CssClass to this to keep the styles the same.
Gives you overall greater control on the server side and you don't need to worry about the javascript. Especially since you are doing server side work anyway.
Just add runat="server" to the div, and add it in the code behind:
<div id="dvButtonContainer" runat="server" ... >
Another option is to use a Panel, as it outputs a div anyway:
<asp:Panel ID="dvButtonContainer" runat="server" ... >
And in the code behind:
Button button = new Button();
dvButtonContainer.Controls.Add(button);
EDIT: Since DIV is not accessible, here is a workaround:
Button Button1 = new Button();
Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", String.Format("document.getElementById(\"div1\").appendChild({0});", Button1.UniqueID), true);
Related
I'm trying to make it so if your Session["LoggedIn"] is true, the wuc (Web User Control, which is the Navbar and is connected to the MasterPage which is connected to the page) prints a logout button.
so I have it like this:
Response.Write("<a runat='server' ID='lblLogout' class='nav-link' CausesValidation='False' OnServerClick='lblLogout_Click'>Logout</a>");
the CodeBehind function looks like this:
protected void lblLogout_Click(object sender, EventArgs e)
{
Session.RemoveAll();
Session["LoggedIn"] = false;
Session["IsAdmin"] = false;
Session["Username"] = "";
}
now, I've tried everything, if I use response.write like that, I can't click it, it just doesn't fire or do anything, if I don't use it inside Response.Write, it does work..
I even tried prinitng an asp:LinkButton instead to see if that works, but it doesn't print out anything when I use it like:
Response.Write("<asp:LinkButton class='nav-link' runat='server' ID='lblLogout' Text='Logout' CausesValidation='False' OnClick='lblLogout_Click' />");
Now the solution that I found was putting another page for logout and placing the function on PageLoad, which works, but I'm wondering if I can make it work so I can use the function from CodeBehind, instead of having to go to another page.
my working solution:
if ((bool)Session["LoggedIn"] == true)
{
Response.Write("<li class='nav-item'> <a runat='server' ID='lblLogout' class='nav-link' CausesValidation='False' href='../PagesForVisitor/wfLogout.aspx'>Logout</a></li><li><a class='navbar-brand' href='#'><img src='" + GetSource() + "' width = '30' height = '30' alt = ''/ ></a></li>");
}
(this one prints out a profile pic as well)
p.s using Bootstrap for styling, not sure if that matters, thanks a bunch..
You should use
<asp:Button id='' runat='server'>
tag
or use css to custom the button look depending on the style.
You can make it a hyperlink in the properties.
I'm listing the contents of my basket by adding fresh HTML to a panel so it lists it on the aspx page. I'm also trying to add an asp:button at the end which will allow users to remove the items, however the button isnt showing!
Code;
string sHTML = #"<div class='item_bar'>
<div class='item_id'>" + id + #"</div>
<div class='item_title'>" + name + #"</div>
<div class='item_price'>" + cost + #"</div>
<asp:Button class='button' Text='Remove' runat='server' CommandArgument='"+name+#"' OnClick='removeItem' />
</div>";
basketDiv.Controls.Add(new LiteralControl(sHTML));
thanks
If you are going to add controls such as the ASP:BUTTON, you would need to add them not via string but actually doing it
this.pnlFrame.Controls.Add(new Button() {
ID = "buttonId1",
Text = "Text for new button"
});
Keep in mind that this needs to be registered when the page is actually being compiled so that is why it is not working in your case.
Check this link also Adding button to panel dynamically and getting it's parent ID
If you do not want events being captured at runtime, and would like to do the actions via jquery, you can add it using string like you are doing, and add a normal HTML input with type submit
<input type='submit' value='New button'/>
However then when clicked, you need to create scripts on the client side to capture those events.
Create button and then set the properties u want then add in panel
// add the litenal control - DIVs
basketDiv.Controls.Add(new LiteralControl("<div class='item_bar'><div class='item_id'>" + id + "</div><div class='item_title'>" + name + "</div><div class='item_price'>" + cost + "</div>"));
// create button and Add it to basketDiv
Button button = new Button();
button.Name = "Button1";
// you can added other attribute here.
button.Text = "New Button";
button.Location = new Point(70,70);
button.Size = new Size(100, 100);
basketDiv.Controls.Add(button);
// close the parent Liternal "DIV class='item_bar'"
basketDiv.Controls.Add(new LiteralControl("</div>"));
I have an asp .net hyperlink control declared like this:
<li runat="server" id="liveChatCtrl" Visible="false"><asp:LinkButton runat="server" ID="hlnkLiveChat" CausesValidation="false" OnClick="hlnkLiveChat_Click">Live Chat Support <i class="icon icon_next_03 fr"></i><i runat="server" id="iconChat" class="icon_chat_online"></i></asp:LinkButton></li>
My problem is that the contents of the linkbutton disappears on postback. Any ideas why this is happening?
On load I execute the following code on the linkbutton or it's children:
string absoluteURL = UtilManager.Settings.Item(Utils.BrandID, "URL:absoluteURL");
string chatLink = "StartChat.aspx";
if (HttpContext.Current.User.Identity.IsAuthenticated)
chatLink = "LiveChat.aspx";//~/
//else
// chatLink = "SalesChat.aspx";
string link = absoluteURL + chatLink;
hlnkLiveChat.Attributes["onclick"] = string.Format("javascript:window.open( '{0}', 'chat', 'status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,height=505,width=420,left=30,top=30');", link);//"openPopup('" + link + "','chat'); return false;";
liveChatCtrl.Visible = true;
A guess...
I believe you may have the code in your Page_Load (or Init) inside an if(!IsPostBack)
If this is the case, move it outside of this if statement as you need it to run as your default visible for your liveChatCtrl is false
Either that or re-code a little so that your default visible is true and you run a check on postback to hide it if needed.
I want to insert html with a few controls +style from the code behind ( asp.net c#) how can I do it ?
You could use a <asp:PlaceHolder> then add controls to this.
e.g.
Image img = new Image();
img.ImageUrl = "/someurl.jpg";
img.CssClass = "someclass";
img.ID = "someid";
img.AlternateText = "alttext"
PlageHolderId.Controls.Add(img);
This would produce the html
<img src="/someurl.jpg" class="someclass" id="someid" alt="alttext" />
You can then do this will any control, literal, hyperlink, button, table, etc...
You can add <asp:Literal> controls in the markup, then set their Texts in code-behind.
Make sure to set Mode="PassThrough" to prevent them from escaping the HTML.
You can add server-side controls by adding them to the Controls collection of any existing control (such as an <asp:Panel>)
Add a few <asp:PlaceHolder>'s to your template file in the <head> and <body>
Then use PlaceHolder1.Controls.Add();
I put a <asp:Panel ID="myPanel" runat="server"/> , and in the codebehind i add controls with:
myPanel.Controls.Add(...)
If you want to insert HTML code direct to the panel, use
myPanel.Controls.Add(new LiteralControl("Your HTML goes here!"))
Instead of Literal control you can use either HtmlGenericControl
HtmlGenericControl div = new HtmlGenericControl();
div.ID = "div";
div.TagName = "div";
div.Attributes["class"] = "container";
form1.Controls.Add(div);
How can i get same functionality in windows forms as in the next example. When i have two links one beneath, and when i click first link a panel is visibleunder it and next link is shifted. When i click again the panel is invisible and second link shifted back.
<script type="text/javascript">
function toggleDivState(divName)
{
var ctl = window.document.getElementById(divName);
if (ctl.style.display == "none")
ctl.style.display = "";
else
ctl.style.display = "none";
}
</script>
<a href="javascript:toggleDivState('poll<%# Eval("ID") %>');">
<div style="display: none;" id="poll<%# Eval("ID") %>">
Something like this?
on click:
control1.Visible = !control1.Visible;
control2.Visible = !control1.Visible;
??
You can use panels that have the 'Dock' property said to 'Top' - you can then adjust the height of said panel to suit.
Sound like you need a FlowLayoutPanel with FlowDirection = TopDown.
Put within this Panel your Link, Panel, Link2 and Panel2. Within the LinkClick event you set the Panel.Visible = !Panel.Visible.