I have two tables on a webform. On a button click I want to hide one and show the other. I gave them both an Id and I want to set the tables' style="display:"
I tried this in javaScript using a function and document.getelementbyid(id).style.display='none' but it did not work.
any ideas?
Solution:
OnClientClick="javascript: tbl2.style.display='';tbl1.style.display='none';return false;"
I am assuming your tables are .net controls? If so, passing 'id' is not enough as .net does not assign the same server id as client-side id.
You need to access the ClientID property of the .net control server-side to get it's real client-side id:
MyButton.OnClientClick = string.Format("{0}.style.display=''", ControlIWantToHide.ClientID);
The above code shows how you would attach some javascript (please don't call it java!) to a .net asp:button called MyButton and then have a client-side click on that hide a control called ControlIWantToHide.
Note: the above may need tweaking to make it work across all browsers but the .net stuff is the important factor here and I suspect the ClientID is what you needed all along.
Based on your responses to comments, the elements you are toggling are not .net controls but the button triggering them is, so: (based on the function posted by ebrown):
<script type="text/javascript">
function hide(id)
{
var element = document.getElementById(id);
element.style.display='none';
}
function show(id)
{
var element = document.getElementById(id);
element.style.display='block';
}
</script>
You could trigger the toggle of an element in your page by attaching the javascript event handler server-side:
MyButton.OnClientClick = "Show(tbl1);Hide(tbl2);return false;";
..which will display the element with id tbl1 and hide the one with tbl2. This will work providing the elements you are toggling are not .net controls - ie, they do not have the runat="server" attribute. Remember, even though you are adding the javascript code to the asp:button on the server, it is only getting executed on the client when they click. Don't forget to return 'false' as shown above to stop the default post-back behaviour of the asp:button.
I think this should get you on the right track:
<table id="tbl1" onclick="javascript:show('tbl2');hide('tbl1');" >
<tr>
<td>
table 1 stuff
</td>
</tr>
</table>
<table id="tbl2" onclick="javascript:show('tbl1');hide('tbl2');">
<tr>
<td>
table 2 stuff
</td>
</tr>
</table>
<script type="text/javascript">
function hide(id)
{
var element = document.getElementById(id);
element.style.display='none';
}
function show(id)
{
var element = document.getElementById(id);
element.style.display='block';
}
</script>
Also make sure your ID's are unique, I noticed you gave a tr element the name 'tbl1' in your example code. Only the tables need to have the Id's
Edit: this will work for hiding Tr's instead (Just give the tr a unique id and use the same approach). However I believe if a table has an on click event you won't be able to reach the row's onclick event, so you will have to use one or the other.
Asp.net will change the id of any server tag using run="server". If you're trying to hide them using javascript you will have to use the id that asp.net spits out (You can see this using view source). Alternatively you could wrap the table in a div and show/hide the div with your usual method.
If you're using an asp:Table and trying to hide the table in the code behind you can use the .Visible property to achieve the same effect on postback.
Use document.getelementbyid(id).style.display="none" to hide
and document.getelementbyid(id).style.display="block" to show
If these are purely html based tables and not then try using -
document.getElementsByName('table1')[0].style.visibility='hidden';
or
document.getElementsByName('table1')[0].style.visibility='visible';
Related
My ASP .Net page has a repeater that is loaded from AJAX. This AJAX page repeater has a column of checkboxes. The repeater is inside a html table.
<input id="chkOptionSelected" runat="server" enableviewstate="false"
type="checkbox" data-bind="click: Add" />
On the main page, I have a label whose value is computed by a JavaScript function. My view model:
function VM() {
var self = this;
self.totalSqft = ko.observable(TotalSqFt);
self.Add = function () {
self.totalSqft(TotalSqFt);
return true;
};
}
ko.applyBindings(new VM());
TotalSqFt is a global variable. The label is:
<asp:Label ID="lblTotalSqFt" runat="server" ClientIDMode="Static" data-bind="text: totalSqft"></asp:Label>
The click event computes the new total in a javascript function. All the view model needs to do is update the label with the new value.
What am I doing wrong? Is it because the checkbox is inside of AJAX content? I can see it all in the view source.
like #Jeroen said, asp:Lable will processed by server and render differently at client side.
so instead you can use normal html label and use runat="server" if you want to access it at server
check this working demo http://jsfiddle.net/91mek1tk/
The direct cause of your issue is most likely that data-bind on an asp:Label is not rendered. You would need to call Attributes.Add or something similar to add it.
Having said that, you're mixing Webforms and client-side heavy tech like KnockoutJS in a way that will probably negate the advantages you'd get from using KO, or worse cause lots of inconvenient cases like the one you're having now. I suggest either moving away from asp controls to more html-oriented controls (like you did with the first input tag), or dropping KO in favor of other, simpler client side technology (which seems more appropriate anyways, since you're currently using KO merely to handle clicks, whereas it excels mostly at two-way data-binding tasks).
Try this for your javascript function:
function VM() {
var self = this;
self.totalSqft = ko.observable("totalSqft");
self.Add = function () {
self.totalSqft("totalSqft");
return true;
};
}
ko.applyBindings(new VM());
Thank you, JAG. I tweaked the demo and got it working. I had val instead of text for my label in one of the lines and hence was not seeing the reflected value. It's working now. Thanks, everyone.
I am using Kendo-UI's Grid along with the knockout-kendo scripts and I've come across a problem that I think I'm missing something silly for.
I have a few links posted in one of the grid columns, and in that I'm using knockout to set some of the attributes including a data- attribute as so:
<a class="copyBooking" data-bind="attr: { 'data-bookingid': BookingId }">Copy</a>
I also have a small piece of Javascript that is set to run when the link is clicked:
$(".copyBooking").click(function(){
var bookingId = $(this).data("bookingid");
//code to access a function via ajax'
});
All seems fine on the initial load as the code within the Javascript runs and my alert comes back with the expected results. However, when I change pages the in Kendo Grid (I have my data paged to only show 10 results at a time) something stops the Javascript from functioning.
According to the HTML generated in Firebug or it's equivalent in Chrome, the data- attribute is set correctly in the HTML, so I'm no sure if the .click isn't firing correctly or if the data- attribute itself isn't being picked up correctly.
Does anyone have any suggestions?
As the element doesn't exist after you page through your grid. You need to have this function run on the databound event so that after the grid is re-rendered it will be applied to the 'new' dom element.
The other option would be to extend your knockout model of your rows with a function and click-bind to that.
One of the features of a website I'm working on is that you can "maximize" the contents of the page, basically removing all the outer elements of the page, navigation, banners, etc.
This is currently done in what seems like the worst possible way:
<%
if (shouldBreakFrame)
{
%><!--#include file="header.include" --><%
}
%>
// Contents of page here
<%
if (shouldBreakFrame)
{
%><!--#include file="footer.include" --><%
}
%>
The footer is basically just closing tags from the header.
So I want to clean this up and am working on a master page, but I'm not sure how to properly "remove" the HTML elements that wrap the contents when we want to maximize the page. It would be easy for tags that open/close on one side or the other of the content, but what about div/tables that open at the top and close at the bottom?
Edit: To clarify exactly what the output looks like and why I can't just "hide" the elements with JavaScript code or the .Visible property here is what the output might look like and what it should look like after the surrounding elements are hidden:
<table>
<tr>
<td>Header</td>
</tr>
<tr>
<td>
Page content here
</td>
</tr>
</table>
And after hiding stuff all that is left is Page content here.
So if I just hide the table, the content would disappear as well.
You have a few options here, both require the elements to be already contained within the markup (unless you want to go down the ajax route).
The first would be using CSS to show/hide elements via the display:none attribute thats explained here. However I would have to say may favourite method would be to use jQuery. This contains two functions, show() and hide() but interestingly also includes a toggle() function which will show/hide elements depending on their current visibility. This should allow your code to be tidier and wouldn't have to worry about if(shouldBreakFrame). I found this interesting article on it which should get you started.
The only other option I can think of is either duplicate the code (in the same masterpage) or have two seperate masterpages. After all a masterpage is basically a template, and thats the part you want to change.
I'm generating a table of content in an ASP.NET page and I want to add client side OnClick actions to each cell (the action should be to set the value of a named TextBox control to the text in the clicked cell). If I could directly add stuff to the HTML output, I think I could do this with a single line of JavaScript in each cell.
Given that I'me not now using JQuery (and never have before), what is the simplest solution to adding this (including the time spent learning stuff)?
Edit: this seems to work
If you are creating your table programmatically, using HtmlControls, you can set the onclick client attribute of the HtmlTableCell:
HtmlTableCell cell = new HtmlTableCell();
cell.Attributes.Add("onclick", "someFunction();");
However, if you're using jQuery you can do something like this:
$('td').click(function(){ // Select all the table cells on the document.
$(this).html($('#textboxId').val()); // When clicked, change the innerHTML
}); // with a textbox value.
it depends how you're generating that table of content.
If bulding up using a table control, you can add JavaScript to TableCell controls using the Attributes.Add() method.
If building up with the GridView, you can add JavaScript when the RowDataBound event is raised
You should give each cell a css class... class="contentsCell"
You can then add the following script to your head tag (along with a jquery include)
$(document).ready(function(){
$(".contentsCell").click(function(){
CallYourFunction();
})
})
Where CallYourFunction() is you own function...
This will basically attach an
onclick=" CallYourFunction();"
to each td cell (or any other element) with the class "contentsCell"
Also, if you want to grab the text inside the cell, something like:
$(document).ready(function(){
$(".contentsCell").click(function(){
var mytext = $(this).text();
CallYourFunction(mytext);
});
});
Check out visualjquery.com for a great reference for jquery
Well, your question is kind of difficult to follow, but I'll give you a few pointers.
Each control has a ClientID property. This is useful for injecting into your javascript so you know the client side id of the control.
jQuery syntax for putting text into a textbox would be like the following:
$("#" + clientId).attr("value", theValue);
and the jQuery syntax for getting text from a cell would be:
$("#" + cellId).html()
Here's the scenario:
I have a textbox and a button on a web page. When the button is clicked, I want a popup window to open (using Thickbox) that will show all items that match the value entered in the textbox. I am currently using the IFrame implementation of Thickbox. The problem is that the URL to show is hardcoded into the "alt' attribute of the button. What I really need is for the "alt" attribute to pass along the value in the textbox to the popup.
Here is the code so far:
<input type="textbox" id="tb" />
<input alt="Search.aspx?KeepThis=true&TB_iframe=true&height=500&width=700" class="thickbox" title="Search" type="button" value="Search" />
Ideally, I would like to put the textbox value into the Search.aspx url but I can't seem to figure out how to do that. My current alternative is to use jQuery to set the click function of the Search button to call a web service that will set some values in the ASP.NET session. The Search.aspx page will then use the session variables to do the search. However, this is a bit flaky since it seems like there will always be the possibility that the search executes before the session variables are set.
Just handle the onclick of your button to run a function that calls tb_show(), passing the value of the text box. Something like
... onclick = "doSearch()" ...
function doSearch()
{
tb_show(caption, 'Search.aspx?KeepThis=true&q=\"' +
$('input#tb').val() +
'\"&TB_iframe=true&height=500&width=700');
}
If you read the manual, under the iframe content section, it tells you that your parameters need to go before the TB_iframe parameter. Everything after this gets stripped off.
Here is an idea. I don't think it is very pretty but should work:
$('input#tb').blur(function(){
var url = $('input.thickbox').attr('alt');
var tbVal = $(this).val();
// add the textbox value into the query string here
// url = ..
$('input.thickbox').attr('alt', url);
});
Basically, you update the button alt tag every time the textbox loses focus. Instead, you could also listen to key strokes and update after every one.
As far as updateing the query string, I'll let you figure out the best way. I can see putting a placeholder in there like: &TB=TB_PLACEHOLDER. Then you can just do a string replace.
In the code-behind you could also just add the alt tag progammatically,
button1.Attributes.Add("alt", "Search.aspx?KeepThis=true&TB_iframe=true&height=500&width=700");