How to keep child controls and only delete surrounding parent control? - asp.net

I have a piece of code like this:
<div id="container" runat="server">
<div id="parent" runat="server">
<div id="child" runat="server">
<p>Some Content</p>
</div>
</div>
</div>
In a certain situation i want to remove the parent DIV and leave the child DIV intact.
Using something like this removes the complete html (parent + child):
container.Controls.Remove(container.FindControl("parent"))
or
parent.visible = false
Is it possible to keep the html within the parent DIV (child DIV) and removing the surrounding parent DIV?
Any help appreciated.
Marcellino

Try this
<div id="container" runat="server">
im container
<br />
<div id="parent" runat="server">
im parent
<br />
<div id="child" runat="server">
i am child
<br />
</div>
</div>
</div>
<asp:Button runat="server" Text="remove" OnClick="remove_clicked" />
<input type="button" value="client remove" onclick="remove();" />
1. For Server side solution
protected void remove_clicked(object sender, EventArgs e)
{
HtmlGenericControl tempChild = child;
container.Controls.Remove(parent);
container.Controls.Add(tempChild);
}
2. For Client side solution
<script type="text/javascript" language="javascript">
function remove() {
var container = document.getElementById('<%= container.ClientID %>');
var parent = document.getElementById('<%= parent.ClientID %>');
var child = document.getElementById('<%= child.ClientID %>');
container.removeChild(parent);
container.appendChild(child);
}
</script>

Related

How can I change my defautl tab in jQuery

I want to change the default tab in my jQuery code here:
$(function () {
$("#tabs").tabs({
activate: function () {
var selectedTab = $('#tabs').tabs('option', 'active');
$("#<%= hdnSelectedTab.ClientID %>").val(selectedTab);
},
active: "<%= hdnSelectedTab.Value %>"
});
});
Here are some of my tabs.
<div id="tabs">
<ul id="tabs-name">
<li>Buscar contrato</li>
<li>Cotización</li>
<li>Client</li>
<li>Tiempo</li>
<li>Equipo</li>
<li>Componentes</li>
<li>Intervención terminado</li>
<li>Garantia y reparacion</li>
<li>Archivos</li>
<li>Expedicion</li>
</ul>
<asp:HiddenField ID="hdnSelectedTab" runat="server" Value="0" />
<div id="tabs-searchContractSection" class="menusection">
<contratoBuscarWebControl:contratoBuscarControl ID="WebControl3" runat="server" />
</div>
<div id="tabs-quoteSection" class="menusection">
<cotizacionWebControl:cotizacionControl ID="Header2" runat="server" />
</div>
<div id="tabs-clienteSection" class="menusection">
<ClientWebControl:clientControl ID="Header4" runat="server" />
</div>
<div id="tabs-TimeSection" class="menusection">
<tiempoControlerWebControl:tiempoControler ID="Header5" runat="server" />
</div>
I want to make "tabs-quoteSection" the default tab how can I adjust my jQuery to make this happen. Thank you for any help
I generally suggest to separate client side and server side scripting. You can use .NET results to populate JS yet it may be better to POST or GET the data you need from the back-end.
Consider the following example:
$(function() {
function getTabIndex() {
var min = 0;
var max = $("#tabs-name li").length - 1;
var i = -1;
// Ask .NET for Index via POST
// Push resulting Integer to 'i' variable
if (i == -1 || i > max) {
i = max;
}
return i;
}
var defaultTabIndex = getTabIndex();
$("#tabs").tabs({
active: defaultTabIndex
});
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div id="tabs">
<ul id="tabs-name">
<li>Buscar contrato</li>
<li>Cotización</li>
<li>Client</li>
<li>Tiempo</li>
<li>Equipo</li>
<li>Componentes</li>
<li>Intervención terminado</li>
<li>Garantia y reparacion</li>
<li>Archivos</li>
<li>Expedicion</li>
</ul>
<asp:HiddenField ID="hdnSelectedTab" runat="server" Value="0" />
<div id="tabs-searchContractSection" class="menusection">
<contratoBuscarWebControl:contratoBuscarControl ID="WebControl3" runat="server" />
</div>
<div id="tabs-quoteSection" class="menusection">
<cotizacionWebControl:cotizacionControl ID="Header2" runat="server" />
</div>
<div id="tabs-clienteSection" class="menusection">
<ClientWebControl:clientControl ID="Header4" runat="server" />
</div>
<div id="tabs-TimeSection" class="menusection">
<tiempoControlerWebControl:tiempoControler ID="Header5" runat="server" />
</div>
Hope this helps.

How To change attribute in html elements in asp.net

this is my asp codes in my page
<asp:GridView BorderStyle="None" BorderColor="White" BorderWidth="0px" CellPadding="0" AlternatingRowStyle-BackColor="" ID="grdProducts" Width="100%" runat="server" AutoGenerateColumns="False" ShowHeader="False" AllowPaging="True" DataSourceID="sqldsProducts">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<!--begin col-lg-6 col-md-6 -->
<li class="list_item col-lg-12 col-md-12 portfolio">
<div class="recent-item">
<figure class="portfolio_1">
<div class="bwWrapper touching medium">
<asp:Image ID="Image1" ImageUrl='<%# Eval("PImg") %>' runat="server" />
</i>
</div>
<figcaption class="item-description">
<h5 id="PTitle"><%# Eval("PTitle") %></h5>
<p id="PDesc"><%# Eval("PDesc") %></p>
<div class="go_link">
Read More
</div>
</figcaption>
</figure>
</div>
</li>
<!--end col-lg-6 col-md-6 -->
<div style="height:20px;"></div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<pagerstyle cssclass="pagination" HorizontalAlign="Center" />
</asp:GridView>
for example i want to add some css attributes for this code
h5 id="PTitle"<%# Eval("PTitle") %>/h5 but i dont know how to find this id and add attributes
You can do this using jQuery. Make sure you have jQuery embedded in your page.
Then you can access PTitle element using jQuery selector and assign CSS to it as follows
$( document ).ready(function() {
$("#PTitle").css('background-color','#ff0000');
});
EDIT:
If you have ASPX page
<h5 id="PTitle" runat="server">Test</h5>
Then Code Behind:
PTitle.Style.Add("display", "none");
Depends on when you want to add the Styles. Here are few options.
If you want to add it in design time just add a style attribute as you would for other html elements.
<h5 id="PTitle" style="font-size:12px"><%# Eval("PTitle") %></h5>
If you want to do it from code behind (server side) you need to add the runat="server" attribute to the element first. In this case you won't add the style attribute in design time.
<h5 id="PTitle" runat="server"><%# Eval("PTitle") %></h5>
Then you can access this control in code behind using it's id as follows.
for (int i = 0; i < grdProducts.Rows.Count; i++)
{
HtmlGenericControl PTitle = (HtmlGenericControl)grdProducts.Rows[i].FindControl("PTitle");
if (PTitle != null)
{
PTitle.Style.Add("font-size", "12px");
}
}
Or, you could use the Attributes property as follows.
for (int i = 0; i < grdProducts.Rows.Count; i++)
{
HtmlGenericControl PTitle = (HtmlGenericControl)grdProducts.Rows[i].FindControl("PTitle");
if (PTitle != null)
{
PTitle.Attributes.Add("style", "font-size:12px");
}
}
If you need to do this in client side make use you JavaScript/ JQuery.
Okay here's all I needed to solve my problem:
LiteralControl litc=new LiteralControl();
litc.Text = "<style type='text/css'> #PTitle{text-align:right} #PDesc{text-align:right} </style>";
this.Page.Header.Controls.Add(litc);

asp:listbox loosing value on auto postback

I have an asp list box that is populated from the code behind
<asp:ListBox ID="UploadsApprovedCountries" EnableViewState="true" runat="server" Rows="1" AutoPostBack="True" OnSelectedIndexChanged="SelectedCountryChange"></asp:ListBox>
protected void Load_Countries()
{
ListItem ps = new ListItem("Please select", "");
ps.Value = "";
UploadsApprovedCountries.Items.Add(ps);
foreach (string cntry in Countries.CountriesArray())
{
UploadsApprovedCountries.Items.Add(new ListItem(cntry, cntry));
}
}
and in my Page_Load I call it like this
if (!IsPostBack)
{
Load_Countries();
}
and on autopostback calls this method
protected void SelectedCountryChange(object sender, EventArgs e)
{
filter = UploadsApprovedCountries.SelectedItem.Value;
Response.Write("line 589");
}
However nothing I seem to do ever actually calls this method. The filter string is never populated and "Line 589" is never wrote out.
Can anyone help out
the whole of my asp.net page
asp:Content ContentPlaceHolderID="ContentMainBody" ID="LocalMainBody" runat="server">
<h1> administration</h1>
<div class="clearboth"></div>
<div>
<div class="demo">
<div id="tabs">
<ul>
<li>Uploads awaiting approval</li>
<li>Uploads approved</li>
<li>Sign-ups awaiting approval</li>
<li>Sign-ups approved</li>
<li>Rejected</li>
</ul>
<div id="tabs-5">
<h2>Rejected submissions</h2>
<div id="rejectedSubmissions" runat="server">
</div>
</div>
<div id="tabs-1">
<h2>Uploads awaiting approval</h2>
<div id="matUnautherised" runat="server">
</div>
<div class="clearboth"></div>
</div>
<div id="tabs-3">
<h2>Uploads live on site</h2>
<asp:ListBox ID="UploadsApprovedCountries" EnableViewState="true" runat="server" Rows="1" AutoPostBack="True" OnSelectedIndexChanged="SelectedCountryChange" OnTextChanged="SelectedCountryChange"></asp:ListBox>
<div id="matAutherised" runat="server">
</div>
<div class="clearboth"></div>
</div>
<div id="tabs-2">
<h2>Sign-ups awaiting approval</h2>
<div id="signups" runat="server">
</div>
<div class="clearboth"></div>
</div>
<div id="tabs-4">
<h2>Sign-ups live on site</h2>
<div id="signupsapproved" runat="server">
</div>
</div>
</div>
</div>
</div>
I have just noticed that if I change my code in my listbox to this
<asp:ListBox ID="UploadsApprovedCountries" EnableViewState="true" runat="server" Rows="1" AutoPostBack="True" OnSelectedIndexChanged="SelectedCountryChange">
<asp:ListItem Value="test1" Text="test1"></asp:ListItem>
<asp:ListItem Value="test2" Text="test2"></asp:ListItem>
<asp:ListItem Value="test3" Text="test3"></asp:ListItem>
<asp:ListItem Value="test4" Text="test4"></asp:ListItem>
<asp:ListItem Value="test5" Text="test5"></asp:ListItem>
</asp:ListBox>
and don't populate it from the data source then it works as it should. Is there a difference between adding the items from the code behind and adding them directly like above?
simple really once you know how
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Load_Countries();
UploadsApprovedCountries.EnableViewState = true;
}

asp.net code behind does not redirect when using jquery mobile

I have a jquery mobile page that uses a dialog page to perform an action. When the buttons are clicked I execute the code in my code behind which works perfectly however, I need to redirect or close the dialog page which for some reason it is not working. Here is my code for the dialog page:
<form id="form1" runat="server">
<div>
<div data-role="header" role="banner">
<h1 role="heading">Claim Issue?</h1>
</div>
<div data-role="content" data-theme="c" class="ui-corner-bottom ui-content ui-body-c" role="main">
<div data-role="fieldcontain">
<label for="CompanyLBL">Company:</label>
<asp:Label ID="CompanLBL" runat="server"></asp:Label>
</div>
<div data-role="fieldcontain">
<label for="IssueLBL">Issue:</label>
<asp:Label ID="IssueLBL" runat="server" Text="Label" ></asp:Label>
</div>
<div data-role="fieldcontain">
<label for="FBVersionLBL">Version:</label>
<asp:Label ID="FBVersionLBL" runat="server"></asp:Label>
</div>
<div data-role="button">
<asp:Button ID="ClaimBTN" runat="server" Text="Yes" onclick="ClaimBTN_Click" />
</div>
<span class="ui-btn-inner ui-btn-corner-all"><span class="ui-btn-text">Cancel</span></span>
</div>
</div>
Code Behind:
//Claim Issue
protected void ClaimBTN_Click(object sender, EventArgs e)
{
try
{
Tier2IssuesSDS.UpdateCommand = "UPDATE Tier2Issues SET Tier2ID = #Tier2ID, IssueStatusID = #IssueStatusID, DateLastModified = #DateLastModified, UserLastModified = #UserLastModified WHERE ID = #ID";
Tier2IssuesSDS.UpdateParameters["ID"].DefaultValue = Request.QueryString["IssueID"].ToString();
Tier2IssuesSDS.UpdateParameters["Tier2ID"].DefaultValue = Session["UserID"].ToString();
Tier2IssuesSDS.UpdateParameters["IssueStatusID"].DefaultValue = "2";
Tier2IssuesSDS.UpdateParameters["DateLastModified"].DefaultValue = DateTime.Now.ToString();
Tier2IssuesSDS.UpdateParameters["UserLastModified"].DefaultValue = Session["Username"].ToString();
Tier2IssuesSDS.Update();
Response.Redirect("~/Account/Tier 2/Admin/Mobile/Queue.aspx");
}
catch(Exception ex)
{
Response.Redirect("~/Account/Tier 2/Admin/Mobile/Queue.aspx");
}
}
What am I missing or what is the work around to get this thing to close the dialog or successfully redirect the browser to the starting page?

ASP.NET make a panel visible on click of hyperlink (whilst also cuasing postback for page navigation)

I may be asking the impossible but let me set out my problem:
I have a menu in a MasterPage which uses images and mouseover mouseout events for design purposes.
On one of the menu options I need to display a set of sub menus options on the click of the parent menu item. The menu item itself also needs to navigate to a specified url.
I was originally trying to use an AJAX accordion panel but as I only had one accordion panel it was always displaying the sub menu items and was not collapsing.
I have also tried putting the options in a div and setting the display via javascript. This worked but then was overwritten once the page navigation postback occurred.
Here is the source:
<%# Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%# Register Src="LeadHeader.ascx" TagName="LeadHeader" TagPrefix="uc1" %>
<%# Register Src="~/LeadFooter.ascx" TagName="LeadFooter" TagPrefix="uc2" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
var revert = new Array();
var inames = new Array('home', 'whoweare', 'whatwedo','ourapproach', 'ourvalues', 'contact');
// Preload
if (document.images) {
var flipped = new Array();
for(i=0; i< inames.length; i++) {
flipped[i] = new Image();
flipped[i].src = "images/"+inames[i]+"2.jpg";
}
}
function over(num) {
if(document.images) {
revert[num] = document.images[inames[num]].src;
document.images[inames[num]].src = flipped[num].src;
}
}
function out(num) {
if(document.images) document.images[inames[num]].src = revert[num];
}
function ShowHide(elementId)
{
var element = document.getElementById(elementId);
if(element.style.display != "block")
{
element.style.display = "block";
}
else
{
element.style.display = "none";
}
}
function UpdateText(element)
{
if(element.innerHTML.indexOf("Show") != -1)
{
element.innerHTML = "Hide Details";
}
else
{
element.innerHTML = "Show Details";
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
<uc1:LeadHeader ID="LeadHeader" runat="server" />
</asp:ContentPlaceHolder>
<div id="nav">
<div class="menu-item">
<a href="Default.aspx">
<img src="Images/home.jpg" alt="home" id="home" onmouseover="over(0)" onmouseout="out(0)"
class="right" /></a>
</div>
<div class="menu-item">
<a href="AboutUs.aspx">
<img src="Images/whoweare.jpg" alt="who we are" id="whoweare" onmouseover="over(1)"
onmouseout="out(1)" class="right" /></a>
</div>
<%-- <asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<cc1:Accordion ID="Accordion1" runat="server" AutoSize="None" FadeTransitions="true"
TransitionDuration="350" FramesPerSecond="40" RequireOpenedPane="false" >
<Panes>
<cc1:AccordionPane runat="server">
<Header>--%>
<div class="menu-item">
<a href="WhatWeDo.aspx">
<img src="Images/whatwedo.jpg" alt="what we do" id="whatwedo" onmouseover="over(2)"
onmouseout="out(2)" class="right" onclick="ShowHide('divDetails');UpdateText(this);" /></a></div>
<%--/Header>
<Content>--%>
<div id="divDetails" style="display:none;">
Management Development<br />
Leadership Development<br />
Personal Development<br />
Team Building & Facilitation<br />
One to One Coaching
</div>
<%-- </Content>
</cc1:AccordionPane>
</Panes>
</cc1:Accordion>
--%>
<div class="menu-item">
<a href="OurApproach.aspx">
<img src="images/ourapproach.jpg" alt="our approach" id="ourapproach" onmouseover="over(3)"
onmouseout="out(3)" /></a>
</div>
<div class="menu-item">
<a href="OurValues.aspx">
<img src="images/ourvalues.jpg" alt="our values" id="ourvalues" onmouseover="over(4)"
onmouseout="out(4)" /></a>
</div>
<div class="menu-item">
<a href="ContactUs.aspx">
<img src="images/ContactUs.jpg" alt="contact us" id="contactus" onmouseover="over(5)"
onmouseout="out(5)" /></a>
</div>
</div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="server">
<uc2:LeadFooter ID="LeadFooter" runat="server" />
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
If I read this correctly you need the nav buttons to direct you to another page and open a sub-menu to display further options.
The easiest way to do this would be to create the entire sub menu on the master page and hide it in your global style sheet. Each page could then contain a line of css to show the appropriate panel no JavaScript required.
A more efficient way would be to use this.Master.Page.FindControl("myPanelId") to manupulate the necessary item server-side.
I can't visualize exactly what you are doing but I have used jQuery accordion menus to do something similary. My server side code built a nested unordered list with links and the appropriate images. I let the hover event switch the accordion panel so that I could still click on any of the links to go to the corresponding page. If this is closer to what you want I can give you a general idea of the code / css necessary.
Hope that helps.

Resources