making div/htmlgeneric control visible using asp.net - c#

I have a user control with a div like this:
<div runat="server" id="pnlShippingMethods" class="checkoutstep">
<div class="steptitle">
<%=GetLocaleResourceString("CheckoutOnePage.ShippingMethods.Title")%>
<div style="float: right;">
</div>
Date from checkout page one for ship method update panel is <%= DateTime.Now.ToString() %>
</div>
<asp:Panel runat="server" ID="pnlShippingMethodsContent" class="stepcontent">
<nopCommerce:CheckoutShippingMethod ID="ctrlCheckoutShippingMethod" runat="server"
OnePageCheckout="true" />
</asp:Panel>
</div>
I am making in visible = false on page load where this control is placed. Then from another control on the same page I am trying to make it visible like this:
HtmlGenericControl pnlShippingMethods = this.Parent.Parent.Parent.Parent.Parent.FindControl("pnlShippingMethods") as HtmlGenericControl;
pnlShippingMethods.Visible = true;
I am able to make visible/invisible user control CheckoutShippingMethod from other user control but not the div. Please suggest how to make it visible

You can use public method instead of it.
1) Make public method/Property in the custom control where you want to show/hide
panel.
public void ShowPanel(bool isVisible)
{
this.pnlShippingMethods.Visible = isVisible;
}
2) Call this from the other control to show hide panel.
yourCustomrControlObject.ShowPanel(true);

Related

Hiding a button inside a div from code

I have a aspx page and i want to hide the button from cs file based on my some condition
My .aspx looks like :
<asp: Content Id="contentid" >
<% if (!IsRedeemCardFlowOptin)
{ %>
<ul id="ulid" class="abc">
</ul>
<div class="bcd" id ="div1">
<div id="div2"></div>
<div id="div3"></div>
<div id="div4" runat="server">
<h4><%= m_AutoRenewInfo.NewPageContent.ArCsidOffHeader%></h4>
<button class="abc bcd cde" title="Button" id="buttondiv"><span>Button</span></button> //Want to hide this button
</div>
</div>
<% } %>
</asp:Content>
Now in the cs file i want to hide the button with id "buttondiv", how can i do that
In my cs file, i try this 2 things but it doesnt work
Control myDiv = (Control)FindControl("buttondiv");
myDiv.Visible = false;
Or
foreach (Control c in contentid.Controls)
{
if (c.ID == "buttondiv")
{
c.Visible = false;
}
}
Can anyone let me know
In order to be used as a server-side control, the button would need to be a server-side control. Add runat="server":
<button class="abc bcd cde" title="Button" id="buttondiv" runat="server">
Then (unless you've broken the designer somehow, it's been a while since I've used Web Forms) you should have an HtmlControl object in your class that you can set without the need to "find" the control:
this.buttondiv.Visible = false;
Kindly keep this in css file.
#buttondiv{
display:none;
}
Or apply style inline like below:
<button class="abc bcd cde" title="Button" id="buttondiv" style="display:none"><span>Button</span></button>

Setting Background of a Div in a Repeater?

First post here. I am trying to set the background image of a DIV within a asp:repeater. The image will need to change with each repeating DIV item and therefore I need to return the image URL from the database.
I can set it every time to the same image by hard coding the URL into the DIV like below:
<asp:Repeater ID="repList" runat="server" OnItemDataBound="repList_ItemDataBound">
<ItemTemplate>
<div id="bGContainer" runat="server" class="formLayout" style=" background-position:Left; width:980; background-image: url(images/image1.jpg);">
<div class="innerDiv">
<asp:Image ID="imgImage1" runat="server" />
</div>
</div>
however, what I am trying to do in the code behind is set the background to each bGContainer to the image I have within the dataset.
Here is my code behind that sets the value to an ASP:image that appears within the inner DIV:
protected void repList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.DataItem != null)
{
OnlineList currentOnlineList = (OnlineList)e.Item.DataItem;
imgImage1.ImageUrl = currentOnlineList.imageUrl;
}
}
So what I am trying to do is somehow call the bgContainer DIV and set the backgroundimage to a value in a value I return in my list.
Any ideas?
Thanks in advance!
you cant access imgImage1 directly. you have to find the control like this.
 System.Web.UI.WebControls.Image img=(System.Web.UI.WebControls.Image)e.Item.FindControl("imgImage1");
img.ImageUrl= currentOnlineList.imageUrl;

How to hide specific jqueryui tab based on some condition?

I am working on an application in which a selected tab should not visible to specific users.My code is
<div id="tabs">
<ul>
<li>Log Tickets</li>
<li>Open Tickets</li>
</ul>
<div id="divLogTickets" runat="server" style="padding: 25px;">
</div>
</div>
if (getUserRole(Convert.ToString(Session["UserId"])) == "HR")
{
//hide tab
}
How to hide a specific tab based on specific user role.
You can add id and runat="server" attributes to the elements that you want to access from the code behind and set the .Visible property in code behind.
For example if you want to hide Log Tickets tab, here's what your aspx code should look like:
<div id="tabs">
<ul>
<li id="liLogTickets" runat="server">Log Tickets</li>
<li>Open Tickets</li>
</ul>
<div id="divLogTickets" runat="server" style="padding: 25px;">
</div>
</div>
Then set the visibility of liLogTickets and divLogTickets in code behind:
if (getUserRole(Convert.ToString(Session["UserId"])) == "HR")
{
//hide Log Tickets tab
liLogTickets.Visible = false;
divLogTickets.Visible = false;
}
You can use $(selector).hide(); method hide.
For eg:
if (getUserRole(Convert.ToString(Session["UserId"])) == "HR")
{
//hide tab
$('#userId').hide();
}
After the validations , i.e. you have validated that this particular user you want to hide it from (as you have mentioned in your code) further you can use the hide() function to hide that particular element.
$('#Id_of_Element').hide();

Get append control value from code behind in ASP.NET

I have a div that contains multiple hidden field which generate by using jquery .append()
function add(ctr)
{
$('.divContainer').append(ctr.clone());
}
In the client side i will have something like
<div class="divContainer">
<div class="dynamicCtr">
<input type="hidden" name="hf_1" id="_1" value="4515">
</div>
<div class="dynamicCtr">
<input type="hidden" name="hf_2" id="_2" value="4422">
</div>
</div>
All the controls inside divContainer are clone by using jquery.
I tried below but doesn't work.
HtmlControl divHave = this.divHave;
foreach (Control control in divHave.Controls)
{
if (control is HtmlInputHidden)
{
HtmlInputHidden hf = (HtmlInputHidden)control;
Response.Write("Value :" + hf.Value);
}
}
Is there any way i can access all the hidden field within the divContainer from code behind?
When you postback, every cloned divs by jquery are lost by server.
Try to use
<asp:Panel ID="divHave" runat="server">
<!-- resolves to a DIV -->
</asp:Panel>
And insert div at server side with Ajax
HtmlGenericControl div = HtmlGenericControl("div")
div.Id = "myid";
divHave.Controls.Add(div);

Switch Div Class in Repeater based on Checkbox.Checked Using C#

I have a repeater that will present a set of titles and checkboxes (some checked some not). Each is wrapped in a div with a background colour. All I want to do is change the background colour for the checkboxes that are already checked so they are easily identified on the page.
Here's the repeater:
<asp:Repeater ID="rptCartridges" runat="server" OnItemDataBound="rp_ItemDataBound">
<ItemTemplate>
<div class="cartridgebox">
<span class="cartridgeboxl"><%#Eval("cartName") %></span>
<span class="cartridgeboxr">
<asp:CheckBox ID="chkCart" name="chkbox" Checked = '<%#Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "cartChecked"))%>' runat="server" />
<asp:HiddenField ID="hfCartID" runat="server" Value='<%#DataBinder.Eval(Container.DataItem, "cartID")%>' />
</span>
</div>
</ItemTemplate>
</asp:Repeater>
All I really want to do is change the class cartridgebox to cartridgeboxchecked if the checkbox is returned as checked.
I have tried manipulating rp_ItemDataBound. Where it goes wrong is the actual changing of the class inline. I've tried using if statements, add runat="server" to the div and populating a variable and then using Response.Write inside the class statement. But nothing seems to work.
What seems the neatest way would be to use rp_ItemDataBound like so:
protected void rp_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
string chkboxClass = "cartridgebox";
CheckBox chk = (CheckBox)e.Item.FindControl("chkCart");
HiddenField hfCartID = (HiddenField)e.Item.FindControl("hfCartID");
// Adding the hide.Value Attribute to the chk.Text field.
chk.Attributes.Add("Text", hfCartID.Value);
if (chk.Checked == true)
{
chkboxClass = "cartridgeboxchecked";
}
else
{
chkboxClass = "cartridgebox";
}
}
But I lack the understanding to pass the variable chkboxClass to the div's class dynamically. Of course I am probably looking at this completely wrong so any guidance would be appreciated.
Use following markup for div in ItemTemplate : <div class='<%# ((bool)Eval("cartChecked"))? "cartridgeboxchecked" : "cartridgeboxl" %>' >
If you need to change div's class on checkbox change immediatelly, consider to add onclick client-side event handler to checkbox in ItemDataBound Repeater's event handler

Categories