Show/Hide div tag javascript - asp.net

I have a gridview column that's gets a large amount of text from the server. So instead of showing all that text after the grid loads I want to show it only if a user clicks an Expand link then closes it with a collapse link. Here is what I have. Please note that I already know that I can put both javascript functions in one; I'm testing right now in two separate functions.
<script type="text/javascript" language="javascript" >
function hidelink() {
var col = $get('col');
var exp = $get('exp');
col.style.display = 'none';
exp.style.display = '';
}
function showlink(){
var col = $get('col');
var exp = $get('exp');
col.style.display = '';
exp.style.display = 'none';
}
<asp:GridView ID="GridView2" Width="400px" runat="server" AutoGenerateColumns="False"
AllowPaging ="True"
BackColor="White" BorderColor="#999999"
BorderStyle="None" BorderWidth="1px"
CellPadding="3" DataKeyNames="APPID"
DataSourceID="SqlDataSource3"
PagerSettings-Mode="NextPreviousFirstLast" EnableSortingAndPagingCallbacks="True">
<PagerSettings Mode="NextPreviousFirstLast" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<Columns>
<asp:BoundField DataField="stuff" HeaderText="Name" ReadOnly="True"
SortExpression="app" />
<asp:BoundField DataField="Description" HeaderText="Short Descr"
ReadOnly="True" SortExpression="des" />
<asp:TemplateField HeaderText="Long Descr" SortExpression="data">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("data") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<div id="col">
<asp:LinkButton ID="expand" runat="server" OnClientClick ="hidelink();return false;">Expand</asp:LinkButton>
</div>
<div id="exp" style="display:none">
<asp:LinkButton ID="collapse" runat="server" OnClientClick ="showlink();return false;">Collapse</asp:LinkButton>
</div>
<asp:Panel ID="Panel1" runat="server" >
<table>
<tr>
<td> <%#Eval("LongDescription")%>
</td>
</tr>
</table>
My issue is that only the first record does everything it should. (expand/collapse) but the other rows only expand and does not hide the expand link in the div tag. It is only finding the id of the first row because when the expand button is hit on any other row it changes the first row to show the collapse link. How can i fix this?

The problem is that because you have repeating elements, the ids of the DIVs are being reused. This is illegal in HTML. The id property of each element must be unique. A better way to handle it is to pass in a reference to the current element to the handler and have it derive the element that it needs to operate on by traversing the DOM.
<div>
<asp:LinkButton ID="expand" runat="server" OnClientClick ="hidelink(this);return false;">Expand</asp:LinkButton>
</div>
<div style="display:none">
<asp:LinkButton ID="collapse" runat="server" OnClientClick ="showlink(this);return false;">Collapse</asp:LinkButton>
</div>
Note: I'm using jQuery in these functions as it makes it easier to traverse the DOM. You can do the same with your own DOM traversal functions and by setting the style properties if you like.
function hidelink(ctl) {
var myDiv = $(ctl).closest('div');
myDiv.hide();
myDiv.next('div').show();
}
function showlink(ctl){
var myDiv = $(ctl).closest('div');
myDiv.hide();
myDiv.prev('div').show();
}

You need to use unique IDs for each row. IDs can only apply to one element in the page, and your code is applying one ID to all the instances of this large column in the table.
Alternatively, you can just use the DOM methods to locate the right element to show/hide. For example:
<div>
Expand
<table style="display: none;">
<tr>
<td><%#Eval("LongDescription")%></td>
</tr>
</table>
</div>
Then for your script:
function showHideDesc(link) {
var table = link.parentNode.getElementsByTagName("TABLE")[0];
if (table.style.display == "none") {
table.style.display = "";
link.innerHTML = "Collapse";
}
else {
table.style.display = "none";
link.innerHTML = "Expand";
}
}

Related

how to get a client id from ajax tab container

i didn't get id from ajax tabcontainer
HERE code id need to get a id from tab_container how to do !!!!!!!
<script type="text/javascript">
function my()
{
var con = document.getElementById("TabContainer1").value;
alert(con);
}
</script>
<asp:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0" Height="100%" Width="50%">
<asp:TabPanel ID="tabpnl1" runat="server" HeaderText="Role Master">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
<asp:Button ID="btn" runat="server" OnClientClick="my()" />
</ContentTemplate>
</asp:TabPanel>
its shows undefined
to get TabContainer1 in javascript you need to change logic of your javascript function
instead of
function my()
{
var con = document.getElementById("TabContainer1").value;
alert(con);
}
try using
function my()
{
var con = document.getElementById("<%= TabContainer1.ClientID %>");
alert(con);
}
As runtime while loading element in dom its ID will be changed. So instead of using static id as "TabContainer1" you can get ClientID which will be rendered at DOM.
Also value will not be work I guess as its container so its better to use .innerHTML instead of .value.
or alternative you can change your html tag
<asp:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0" Height="100%" Width="50%" ClientIDMode="Static">
I have added ClientIDMode="Static" so it will use "TabContainer1" in DOM as static id will be generated. In this case, you do not need to change your javascript code.
Hope this help

ASP.NET Repeater FindControl not working for Label, but works for Textbox

I'm facing issues trying to get controls from my repeater. I have one label and one textbox. The label is giving me a null reference error, however the textbox is working.
Markup:
<asp:Repeater ID="PalletsRepeater" runat="server" OnItemDataBound="PalletsRepeater_ItemDataBound">
<ItemTemplate>
<div style="margin-left: 20px; margin-top: 5px;">
<asp:Label lbl="lblPalletId" Text='<%#"Pallet "+Eval("PALLETID")%>' runat="server" />
<asp:Label Text=", Qty = " CssClass="field-label-blue" runat="server" />
<asp:TextBox ID="txtPalletItemQty" runat="server" Text='<%# Eval("ITEMQTY") %>' step="1" type="number" />
</div>
</ItemTemplate>
</asp:Repeater>
Code Behind:
foreach (RepeaterItem repeaterRow in PalletsRepeater.Items)
{
// This fails.
string palletId = ((System.Web.UI.WebControls.Label)repeaterRow.FindControl("lblPalletId")).Text;
// This works.
string palletItemQty = ((System.Web.UI.WebControls.TextBox)repeaterRow.FindControl("txtPalletItemQty")).Text;
}
I have researched this on Stack Overflow and most answers involve using the OnItemDatabound event, but when I tried to implement those answers, it still comes back null for the label. Personally I don't understand the suggestions as far as using the OnItemDatabound event.
Change
<asp:Label lbl="lblPalletId" Text='<%#"Pallet "+Eval("PALLETID")%>' runat="server" />
Into
<asp:Label id="lblPalletId" Text='<%#"Pallet "+Eval("PALLETID")%>' runat="server" />
You cannot find it because it has no ID

How to do a count on table's <tr> and make some andother background-color in vb.net

In ASP.net I'm having this in the frontend, and in the backend(vb) I want to do a count on all <tr>'s on this page and every other (so the 2nd, 4th, 6th,...) has to have another background color.
How do I count all <tr>'s on a page and how can tell them to give every other one another background-color?
This is the frontend:
<p class="title"><asp:Label ID="Label1" runat="server" Text="Title 1"></asp:Label></p>
<table class="table">
<tr><td>Person 1</td><td>Bestuurder</td></tr>
<tr><td>Person 2</td><td>Zaakvoerder</td></tr>
</table>
<p class="title"><asp:Label ID="Label3" runat="server" Text="Title 2"></asp:Label></p>
<asp:Button ID="btn_add_beheerder" runat="server" Text="BEHEERDER TOEVOEGEN" class="btn_add"/>
<table class="table">
<tr><td>Person 3</td><td>Beheerder</td></tr>
<tr><td>Person 4</td><td>Beheerder</td></tr>
</table>
The backend is just going to be on a page_load.
I think the best way to do this is putting every <tr> in an array and then every array[i] that's dividable by 2 give another background-color?
Is this the best way to work?
you could of course do this in a gazillion different ways.
Here just one suggestion.
<asp:GridView id="gv" runat="server" DataSourceID="ds">
<Columns>
<asp:BoundField DataField="Person" />
<asp:BoundField DataField="Something else"/>
</Columns>
<AlternatingRowStyle BackColor="Blue" />
</asp:GridView>
<asp:SqlDataSource runat="server" ID="ds"
ConnectionString="your connectionString"
SelectCommand="--select stuff">
</asp:SqlDataSource>
note the "AlternatingRowStyle"
Modifying your css class should be enough
.table tr:nth-child(odd) {
background: silver;
}
if you are working with dynamically generated table then add the css class attribute
HtmlTable myTable = new HtmlTable();
// initialization
myTable.Attributes.Add("Class", "table");

Radiobutton Text alignment Issue

I am working in asp.net and have radiobutton list and I want to align their text as I require.
Here is what I have currently:
I want to make them like this:
EDIT:
Secondly, when I click Ages From radiobutton, I display a div against this like:
and when I click back to All Ages radio button, I want to hide that div. But SelectedIndexChanged doesn't work second time and onwards. It only works first time.
Code of aspx:
<table>
<tr>
<td>
<asp:RadioButtonList ID="rdoAge" runat="server" RepeatDirection="Horizontal"
onselectedindexchanged="rdoAge_SelectedIndexChanged" AutoPostBack="true" >
<asp:ListItem Text="All Ages" Value="All Ages" Selected="True"></asp:ListItem>
<asp:ListItem Text="Ages From" Value="Ages From"></asp:ListItem>
</asp:RadioButtonList>
</td>
<div id="divAge" runat="server" visible="false">
<td>
<asp:TextBox ID="txtAgeFrom" runat="server" CssClass="textEntry2" MaxLength="3" Width="65"></asp:TextBox>
</td>
<td>
<asp:Label ID="lblTo" runat="server" Text="To"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtAgeTo" runat="server" CssClass="textEntry2" MaxLength="3" Width="65"></asp:TextBox>
</td>
</div>
</tr>
</table>
Code of cs file:
protected void rdoAge_SelectedIndexChanged(object sender, EventArgs e)
{
switch (rdoAge.SelectedValue)
{
case "All Ages":
divAge.Visible = false;
break;
case "Ages From":
divAge.Visible = true;
break;
}
}
I'll be grateful if anyone suggests something useful for this issue.
Thanks in advance.
That was the problem of missing closing tag. I must have missed a closing tag of some control. I re-added all controls with taking care of closing tags. Now it is working fine.
Thanks all for helping.
try using css sytel
<style type="text/css">
table.radioWithProperWrap input
{
float: left;
}
table.radioWithProperWrap label
{
word-wrap: break-word;
}
</style>
<asp:RadioButtonList runat="server" CssClass="radioWithProperWrap" ....>
switch (rdoAge.SelectedItem.Text)
In the Source Code, Define the Update Panel like this:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
and then in the OnSelectedIndexChanged event, add
UpdatePanel1.Update();

how to update label value of gridview with another value

I have gridview with ajax rating control where i am updating rating value on change event and i am saving that data into the database using sql datasource and i want to show that change value into another label .But when i am updating the value it makes change in database but not in the label. please tell me how can i change lbel value at that same time i am using these code.
<asp:GridView ID="GVTweet" runat="server" AllowSorting="True" DataKeyNames="id" AllowPaging="true"
AutoGenerateColumns="False" GridLines="Horizontal" PageSize="15" Width="700px"
onselectedindexchanged="GVTweet_SelectedIndexChanged"
onrowcancelingedit="GVTweet_RowCancelingEdit"
onrowediting="GVTweet_RowEditing"
onrowupdated="GVTweet_RowUpdated" onrowupdating="GVTweet_RowUpdating"
DataSourceID="SqlDataSource2"
onpageindexchanging="GVTweet_PageIndexChanging" >
<HeaderStyle />
<Columns>
<asp:TemplateField HeaderImageUrl="~/images/rate1.png"
HeaderStyle-CssClass="headerCss1" HeaderText="Rate(1-5)"
SortExpression="Rating" >
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="auto">
<tr style="width:150px;" valign="top" >
<td style="height:30px;">
<asp:Label ID="lblTotalRate" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "TotalRate")%>'></asp:Label>
</td>
</tr>
<tr style="width:150px;" valign="top" >
<td style="height:30px;">
<asp:UpdatePanel ID="updtpnlTweet" runat="server">
<ContentTemplate>
<cc1:Rating ID="rateTweet" runat="server" CurrentRating='<%# Bind("Rating") %>'
EmptyStarCssClass="empatyStarRating" FilledStarCssClass="filledStarRating"
MaxRating="5" onchanged="rateTweet_Changed" StarCssClass="ratingStar"
WaitingStarCssClass="savedStarRating">
</cc1:Rating>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</table>
</ItemTemplate>
<HeaderStyle CssClass="headerCss1" />
</asp:TemplateField>
this is html that i using and i want that rating value after updation in this label lblTotalRate.
protected void rateTweet_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
{
try
{
// cast rating control which has initiated the call:
AjaxControlToolkit.Rating myRating = (AjaxControlToolkit.Rating)sender;
// regular expression which will help identifying row number:
System.Text.RegularExpressions.Regex rexLineNo = new System.Text.RegularExpressions.Regex("ctl\\d+");
// update the record based on the recodrd id
this.updateRating(this.ProductId(rexLineNo.Match(myRating.UniqueID).ToString()), e.Value);
// the above line does the following:
// rexLineNo.Match(myRating.UniqueID).ToString() - finds "ctl and line number
//GVTweet.DataSourceID = "SqlDataSource2";
// GVTweet.DataBind();
GVTweet_RowUpdated( sender2, e2);
// this.Page.Form.Action = "AddRating()";
// SqlDataSource2_Updated(sender1, e1);
}
catch
{
}
}
If any buddy has please tell me .javascript and both is ok for me.
This is code behind code
enter code here
Seems that a piece of code is missing...
anyway, as far as I can see from the code, the label is not into an UpdatePanel. If the expected behavior is to change asynchronously both of the controls you have to manage a partial update in the Label too, or trigger a complete page postback (which is not probably what you want).

Resources