I have a asp panel as below.
<asp:Panel ID="panele" runat="server">
<tr><td><fieldset ID="fsFieldset" class="crossbrowserfieldselect">
<legend align="left">Show the current values</legend>
</td>
<tr> <td><asp:Textbox id ="txtstudent" runat"server"></td>
<asp:panel>
There are many more text boxes and dropdownlists inside the fieldset and to reduce the complexity I did not put the entire code. I have this aspx page and it has 3 modes(select, update,insert). In select mode, aspx page should not display panel. By default the asp panel is visible. I am making it invisible in one of the functions in code behind that drives select mode as shown below. All the controls are readonly in this mode.
enter code here
function selectvalues ()
{
panele.Visible=False; /*code behind */
}
In insert mode, panel should be displayed and working as expected. In update mode, when user tries to edit date of birth value (it is date picker control), the panel should be displayed.Update mode also uses the above function and only when date of birth value is selected the panel should be displayed. Hence I wrote a javascript code to display panel as below.
<Datechooser Width="100px" runat="server" ID="BirthDate"
onselect="return setpanelcontrols();"
</Datechooser>
enter code here
function setpanelcontrols()
{
var objpanele=document.getElementById("<%=panele.ClientID%>");
objpanele.style.display="block";
return true;
}
When I select date of birth, the panel is not displayed. I dont know whats wrong. I really appreciate any help on this.
enter code here
Controls with Visible=False are not rendered as part of the HTML. Try hiding and showing trough CSS like this in your code behind:
panele.CssClass= "myHidePnlclass";
or what I think will fit your current code:
panele.Style["display"] = "none";
When you set the asp:Control Visible Property to false from code behind. The browser doesn't render that element and you can't make it visible from client side (jquery, javascript) then.
To hide it from code behind Replace:
panele.Visible=False;
With
panele.Attributes.Add("style", "display:none");
I see that there is problem with tags. Panel is rendered as div and i have applied the css style with visibily hidden. Finally when i tried to inspect element, i found an interesting thing. Below is the asp code as I posted before. However I added a div to see whats happening.
<tr><td><fieldset ID="fsFieldset" class="crossbrowserfieldselect">
<legend align="left">Show the current values</legend>
</td>
<tr> <td><asp:Textbox id ="txtstudent" runat"server"></td>
<asp:panel>
</div>
Below is the css via inspect element
<div id="BasicInfo_pane" style="visibility:hidden;">
<div id="BasicInfo_panele" class="styleset1" style="visibility:hidden;display:none;">
</div></div>
<table>
<tbody>
<tr>
<td> fieldset="fsFieldset"........ </td> </tr> </tbody> </table>
Clearly for some reason the panel has ended before fieldset as div's ended before fieldset (i think start and end tags are not properly organized, dont know why). And I think this is the reason the fieldset inside panel is not hidden. Panel is hidden and as fieldset is not hidden the output is displayed in browser and I misunderstood that panel is not hidden.
Use
ClientIDMode="Static"
on the asp panel and then in JavaScript:
function selectvalues ()
{
if(YOUR CONDITION){
$('#panele').hide();
}
}
Related
The situation is I want to add new button in my html aspx file (existing file). But I keep getting Server Error in '/' Application (Runtime Error) in the page after adding new button. Below is the error:
enter image description here
If I remove back the html markup, the page come back OK.
enter image description here
<asp:button id="btnSelect" runat="server" onclick="select_Click" text="Select"/>
Then, I add inside html markup page , for example below, the Select Button will show a popup message for Selected Date, it's ok like I want. But I cannot add query to save the selected date into SQl database here because the select_Click button event is inside html markup aspx page. It's look like this button is inside Content Control and I'm not allowed to add button inside the existing file of html markup aspx. Then, when I add select_Click event function in my Example.aspx.cs page the button will do nothing. Supposedly the select_Click event function is inside Example.aspx.cs file.
Anyone got any ideas why this happen???
Note: The existing file is created by previous developer.
<script runat="server">
protected void submit_Click(object sender, EventArgs e)
{
string targetdate = Request.Form[TargetDate.UniqueID];
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Selected Date: " + targetdate + "');", true);
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="container">
<table id="table2" style="text-align: center; width: 100%">
<tr>
<td class="auto-style1" style="text-align: center; height: 51px;">
<asp:Label ID="lblTitle" runat="server" Text="Case Detail" Font-Bold="True" Font-Size="XX-Large" Font-Names="Verdana" Font-Underline="True"></asp:Label></td>
</tr>
</table>
</div>
</asp:Content>
Thankyou for your time by reading this.
Well, first there is "zero" advantage to put the code inside of a script tags with runat=server.
You will get 100% the SAME effect if you just move that code behind.
(and I suggest you do).
However, looking close at your page, we see it is a "child" page. (in other words, you have a master page in effect here).
Remember, ALL THAT using script with runat=server does is inject the code in CODE behind.
but, that means of course you have to place that good old plain jane code stub inside of the content part since that is ALSO where the current page is being injected/placed.
So, really, I would suggest that you DUMP this idea, and there is no advantage I can think of. Why put code in the markup when you WILL AND ALREADY have a code behind page?
Now, all you doing is creating two places to look at code, and worse yet, when you use ?
All it does is move the code to code behind location anyway!!!
So, your example has to be this:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script runat="server">
protected void submit_Click(object sender, EventArgs e)
{
string targetdate = Request.Form[TargetDate.UniqueID];
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Selected Date: " + targetdate + "');", true);
}
</script>
<div class="container">
<table id="table2" style="text-align: center; width: 100%">
<tr>
<td class="auto-style1" style="text-align: center; height: 51px;">
<asp:Label ID="lblTitle" runat="server" Text="Case Detail" Font-Bold="True" Font-Size="XX-Large" Font-Names="Verdana" Font-Underline="True"></asp:Label></td>
</tr>
</table>
</div>
</asp:Content>
In other words, you have to place that code inside of the "content", since that is where all your markup and "code" has to be placed, and that REALLY amounts to just placing that "stuff" into the "form" tag area on a regular (non master/child page).
However, as I pointed out, really?
Just take that code, cut it out, right click, view->code, and then just paste that code into the code behind. (remove the script tags).
The result will be 100% the same operation.
So, just keep in mind that using "script" with runat="server"?
All it really does is just move that code to the code behind module, and you not find nor see any difference (then if you just had placed that code in the code behind module anyway!).
However, as noted, since you have a master/child page?
Then both markup, and any code for that "child" page?
it has to be placed inside of the content template.
I should also point out in your sample markup? I don't see a button that would run that code stub anyway - so that's missing and that makes even less sense.
So assuming you did add a button inside of that content template?
Then it most certainly could have a click event that runs that button click code stub you have.
So keep in mind the rules that apply to JavaScript code?
They do NOT apply to code behind, and thus the placement rules for script vs script=runat=server does NOT apply in ANY way at all here.
Script tags with runat=server means:
Please move that server side into the code behind code module for that given page. The code running will be 100% the same as if you had placed that server side code directly into the code behind module in the first place. (again: 100% the same result).
So, yes, you should be able to place a button inside of the content template.
like this:
I am using ASP.NET for a web page in order to make some server calls that involve looking up user organization information. Based on that information, we need to either hide or display a div. In the header I have a C# function that definitely runs. I have tried the following lines to hide the div.
divID.Style.Add("display","none");
and
divID.Visible = false;
In the body, I am currently using an asp:Panel that runs at server and contains the id "divID". No matter what I do, I can't get the div to hide (without manually putting the styling in). I tried putting the scripts before and after the body, and it didn't make a difference. Any suggestions on the best way to do this would be appreciated.
EDIT:
Here is the C# initiating code.
<script runat="server" language="C#">
void getUserInfo(Object sender, EventArgs ev){
The rest of the C# code is irrelevant, but the relevant line shown above is definitely being run.
The HTML portion looks something like this.
<asp:Panel runat="server" id="divID" style="width:200px; height:130px; ">
<div style="text-align:center">Test Data</div>
</asp:Panel>
C# code is always compiled and run from the server-side, and so cannot impact the state of a page once rendered unless you use postbacks or callbacks. If you want to change the visible state of a control on the client-side, you will need to use Javascript on the client side (possibly triggered by a button click) to show and hide the control.
As an example, check out the solution at the link below.
https://forums.asp.net/t/1603211.aspx?Show+hide+div+on+button+click+without+postback
<script type="text/javascript">
function ToggleDiv(Flag) {
if (Flag == "first") {
document.getElementById('dvFirstDiv').style.display = 'block';
document.getElementById('dvSecondDiv').style.display = 'none';
}
else {
document.getElementById('dvFirstDiv').style.display = 'none';
document.getElementById('dvSecondDiv').style.display = 'block';
}
}
</script>
<asp:Button ID="btn" runat="server" Text="Show First Div"
OnClientClick="ToggleDiv('first');return false;" />
<asp:Button ID="Button1" runat="server" Text="Show Second Div"
OnClientClick="ToggleDiv('second');return false;" />
<br />
<div id="dvFirstDiv" style="display: none;">
First Div
</div>
<div id="dvSecondDiv" style="display: none;">
Second Div
</div>
In the header I have a C# function that definitely runs.
If you're talking about the HTML page header - no, it definitely not running. C# code is executed only server side.
Based on your post, I'm assuming we're talking WebForms here and yo have a script block in your aspx file. While this is fine, I recommend placing the server-side code into a code behind file.
So all you need to do is to add a handler for the PreRender phase of the page life cycle and place your logic for showing/hiding the div in there.
public void Page_Prerender(object sender, EventArgs e)
{
divID.Visible = false;
' OR
'divID.Style.Add("display","none");
}
Note that setting the Visible property of a WebForms control excludes the control from rendering to the page, whilst setting display: none renders it as HTML but it isn't displayed on the page.
Try in backcode: divID.Controls.clear();
This worked for me.
I have a parent page containing an iframe, an ASP label and a button.
When the user click on the parent button, it will display some table results inside the iframe.
Now i would like to display the parent label with different text and color depending on the results of the iframe.
So i m trying in the code behind (C#) of the iframe to retrieve the label from the parent and assign it a CssClass. But no matter what i do it always come back with a null reference when trying to find the control from the parent.
Can someone please help?
Parent page code:
<div class="search-row">
<div class="search">
<button id="btnSearch" type="submit" class="button button-block">Search</button>
</div>
<div class="search" style="padding-top: 20px;">
<asp:Label ID="wrapperResponse" CssClass="resp" runat="server">TEST</asp:Label>
</div>
</div>
<div id="iframeDiv">
<iframe name="my_frame" width="100%" height="350px" src="Results.aspx" scrolling="no" frameborder="0"></iframe>
</div>`
The Results.aspx will display a table with some html build dynamically.
Now let s say if resulting Table has one row, i would like Label WrapperReponse to be in Green and says "1 row" but if 2 rows then i would like it to be in Red and says "2 Rows"
In my Results.aspx.cs i tried
String test= String.Format("{0}", Request.Form["wrapperResponse"]);
Label statusResponse = (Label)this.FindControl("wrapperResponse");
Label statusResponse2 = (Label)Parent.FindControl("wrapperResponse");
Any ideas are welcome!
thanks
Well, just a couple of points needed mentioning here. Food for thought.
In order to access the asp label control, you need to do it on the parent page instead of on the results page.
I would suggest you reconsider the design of the page. Do you really have to use the iframe here? Why not place your button and label controls on the results.page directly? Or perhaps make results.aspx as an ASCX user control, so you can embed it on the parent page.
Hope it helps
So there is a a table and a text box in one of the cells
<td>
<asp:TextBox ID="tbSomeTextBox" Columns="5" runat="server"> %
</td>
This textbox gets shown if a certain selection is made in a drop down. The problem is that I would like the "%" character to also be hidden or shown with the textbox.
I have tried putting the whole textbox control inside a DIVand in my JQuery hiding the DIV at the same time I hid the textbox.
<td>
<div id="divSomeDIV"><asp:TextBox ID="tbSomeTextBox" Columns="5" runat="server"> % </div
</td>
But I get an error in my java script that id="divSomeDIV" doesn't exist in the current context.
$("#<%=divSomeDiv.ClientID%>").hide();
Wrapping that single character in a asp:Label seems like overkill.
Any suggestions?
divSomeDiv is running client-side (i.e. no "runat=server"), so there's no need for
$("#<%=divSomeDiv.ClientID%>").hide();
Just do
$("#divSomeDiv").hide();
create an asp.net label, set the label value to be %, and make them both visible or both not visible at the same time... ?
If you were getting "does not exist" in your Javascript it's probably because your Javascript is running before the div is created in the dom. In other words, this is incorrect.
<script>$("#divSomeDiv").hide()</script>
<div id="divSomeDiv">...</div>
You can either put the Javascript after the div
<div id="divSomeDiv">...</div>
<script>$("#divSomeDiv").hide()</script>
Or you can make your Javascript run after the DOM has loaded.
<script>
$(function() {
$("#divSomeDiv").hide()
});
</script>
<div id="divSomeDiv">...</div>
I have a simple web form which has a couple list boxes and a search button. When the button is clicked, it returns a DataSet. If the dataset contains records, I set the asp:label which is initially set to false to true, but this is not happening. If the dataset has records and the visible property is set to true, the label still does not show up.
I have also tried putting the label and a couple other controls in an html table and setting a runat="server" attribute on the table and changing the visibility on that, but it does not show either.
Here is aspx code:
<table>
<tr>
<td>
<asp:Label ID="lblSortBy" runat="server" Text="Sort By:" Visible="false">
</asp:Label>
<asp:DropDownList
ID="ddlSortBy"
runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="ddlSortBy_SelectedIndexChanged">
<asp:ListItem Value="Gross">Gross</asp:ListItem>
<asp:ListItem Value="Population">Population</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
Here is simplified code behind when a button is clicked:
public void GetData()
{
DataView dv = GetReportData().DefaultView;
if(dv.ToTable().Rows.Count > 0)
{
lblSortBy.Visible = true;
}
else
{
lblSortBy.Visible = false;
}
}
I have a couple Update Panels around some ListBoxes and a GridView, but not the Label and Dropdown. Would this cause an issue?
I did a test, I set a label that was in an update panel to false if records were found and the label disappeared, so it is working if it is in an update panel.
If I'm not mistaken, your label should exist on an updatepanel, because as far as the static HTML page is concerned, the one and only time that your current label exists, it's set to be not visible. You would have to reload the whole page to make it visible again.
If the button is inside an UpdatePanel, then the Table, Label, etc. also have to be inside an UpdatePanel to get updated. Otherwise only the contents of the UpdatePanel get updated when clicking the button (this is what's called partial page-rendering).
So if the button is in an UpdatePanel, you have two possibilities to solve the problem:
put the table, Label, DropDownList etc. into the same UpdatePanel
or put them in another UpdatePanel and set the UpdateMode of that property to Always, so that it gets updated, even if a Postback was initiated by a control within another UpdatePanel.
See this page in MSDN for details.
You just need runat="server" on the label itself; though Visible should default to True.
Make sure you add a ForeColor to avoid mixing it in w/ background.
Debug to ensure your label has content and it's not in another control whose Visible=False.
If the table is changing visible and is the parent container of the label I don't believe it is necessary to change the label's visibility at all as it should always be set to visible.
I am assuming that you are gonna hide the ddl as well if there is no data. Have you tried putting a panel around both of them and setting its visibility to true
if you are returning rows and your button is in an updatepanel, then is your label and ddl in that updatepanel as well
thanks its really useful, put Lable in a update panel.
<ContentTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="LinkNM" runat="server" Text="Learn>" BackColor="Transparent" style=" color: #6699FF;text-decoration-color:none;border:none;font-size:x-large" OnClick="LinkNM_Click"/>
<asp:Label ID="lblChapterName" runat="server" BackColor="Transparent" style=" color: #6699FF;text-decoration-color:none;border:none;font-size:x-large" ></asp:Label>
</td>
</tr>
</table>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnFileUpload" />
</Triggers>
</asp:UpdatePanel>