I want to set individual DIVs inside a grid visible/hidden programmatically like so, but it isn't working. How would you fix this code?
foreach (DataGridItem dgItem in dgW.Items)
{
HtmlGenericControl dvGoodRow =
(HtmlGenericControl)dgItem.FindControl("dvGoodRow");
HtmlGenericControl dvBadRow =
(HtmlGenericControl)dgItem.FindControl("dvBadRow");
dvGoodRow.Visible = true;
dvBadRow.Visible = false;
}
Debug sessions show that the Visible attribute I set above does take effect at least in my watch window, but visually on the browser I see no change. I have a grid like the one below:
<asp:datagrid id="dgW" AutoGenerateColumns="False" ShowHeader="False" ShowFooter="False" runat="server" DataKeyField="SID" CellPadding="0" GridLines="None" AllowSorting="True" OnItemDataBound="dgW_ItemDataBound">
<HeaderStyle />
<FooterStyle>
</FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="SID" SortExpression="SID">
<ItemTemplate>
<asp:Label ID="lblSID" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "SID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Extension" SortExpression="ExtensionStopDate">
<ItemTemplate>
<asp:Label id="lblExtensionStopDate" Runat="server" text='<%# String.Format("{0:MM/dd/yyyy}", DataBinder.Eval(Container.DataItem, "ExtensionStopDate"))%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<div id="dvGoodRow" runat="server" hidden>
<div>
<asp:Label runat="server" ID="lblGoodRow">
<i id="icnGoodRow" runat="server"></i>
</asp:Label>
</div>
</div>
<div id="dvBadRow" runat="server" hidden>
<div>
<asp:Label runat="server" ID="lblBadRow">
<i id="icnBadRow" runat="server"></i>
</asp:Label>
</div>
</div>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
Remove that 'hidden' property and use visible
<div id="dvGoodRow" runat="server" visible="false">
<div id="dvBadRow" runat="server" visible="false">
Here's what I ended up doing to fix the code: I switched to showing/hiding ASP.NET Panels instead of DIVs!
<asp:TemplateColumn>
<ItemTemplate>
<asp:Panel runat="server" ID="pnlGoodRow" Visible="False">
<div id="dvGoodRow">
<asp:Label runat="server" ID="lblGoodRow">
<i id="icnGoodRow" runat="server"></i>
</asp:Label>
</div>
</asp:Panel>
<asp:Panel runat="server" ID="pnlBadRow" Visible="False">
<div id="dvBadRow">
<asp:Label runat="server" ID="lblBadRow">
<i id="icnBadRow" runat="server"></i>
</asp:Label>
</div>
</asp:Panel>
</ItemTemplate>
</asp:TemplateColumn>
So in the code, I now hide/show the Panel elements instead of DIVs:
foreach (DataGridItem dgItem in dgW.Items)
{
System.Web.UI.WebControls.Panel pnlGoodRow =
(System.Web.UI.WebControls.Panel)dgItem.FindControl("pnlGoodRow");
System.Web.UI.WebControls.Panel pnlBadRow =
(System.Web.UI.WebControls.Panel)dgItem.FindControl("pnlBadRow");
pnlGoodRow.Visible = true;
pnlBadRow.Visible = false;
}
This works!
Related
I have a gridview which is generated by asp.net. I have a id row which I have hidden using css. However this causes a visual problem here is the problem in question.
I know that the problem is caused due to the last td in the html that looks like this <td> </td>.
Has you can see on this image.
when i remove that line of code it works and my gridview looks like this.
why does asp generate this code <td> </td>. how can i remove it any help would be very appreciated thank you.
Here is the code for my gridview
<asp:GridView ID="gvLocation" runat="server" AutoGenerateColumns="false" ShowFooter="true"
ShowHeaderWhenEmpty="true" AllowPaging="True" OnPageIndexChanging="gvLocation_PageIndexChanging"
OnRowEditing="gvLocation_RowEditing" OnRowCancelingEdit="gvLocation_RowCancelingEdit"
AllowSorting="true" onsorting="gvLocation_Sorting" ridLines="None" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" CellPadding="3">
<%-- Theme Properties --%>
<Columns>
<asp:TemplateField HeaderText="Armario" SortExpression="armario">
<ItemTemplate>
<asp:Label CssClass="gridTextbox" ID="lblCloset" Text='<%# Eval("armario")%>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox CssClass="gridTextbox" ID="txtCloset" Text='<%# Eval("armario")%>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox CssClass="gridTextbox" ID="txtClosetFooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cajon" SortExpression="cajon">
<ItemTemplate>
<asp:Label CssClass="gridTextbox" ID="lblDrawer" Text='<%# Eval("cajon")%>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox CssClass="gridTextbox" ID="txtDrawer" Text='<%# Eval("cajon")%>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox CssClass="gridTextbox" ID="txtDrawerFooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ImageUrl="~/Images/edit.png" runat="server" CommandName="Edit" ToolTip="Edit" Width="20px" Height="20px"/>
<asp:ImageButton ImageUrl="~/Images/delete.png" runat="server" CommandName="Delete" ToolTip="Delete" Width="20px" Height="20px" OnClick="deleteItem"/>
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ImageUrl="~/Images/save.png" runat="server" CommandName="Update" ToolTip="Update" Width="20px" Height="20px" OnClick="updateItem"/>
<asp:ImageButton ImageUrl="~/Images/cancel.png" runat="server" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px"/>
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ImageUrl="~/Images/addnew.png" ID="addNewLocation" runat="server" CommandName="AddNew" ToolTip="Add New" Width="20px" Height="20px" OnClick="addNew"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ubicacion id" SortExpression="ubicacion_componente_id" ItemStyle-CssClass="hideGridColumn" HeaderStyle-CssClass="hideGridColumn">
<ItemTemplate>
<asp:Label CssClass="gridTextbox" ID="lblLocationID" Text='<%# Eval("ubicacion_componente_id")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Add here is the code for the css that hides the first id column
.hideGridColumn
{
display: none;
}
There are 2 solution for you to remove redundant <td> </td>
Javascript version
var table = document.getElementById("mytable");
for (var i = 0; i < 3; i++) {
for (var k = 0; k < 5; k++) {
var str = table.rows[i].cells[k].innerHTML;
str = str.replace(/( )*/g, '');
if (str.length) alert(str);
}
};
How to remove with javascript this case?
Jquery version
$(function() {
$("table tr").each(function() {
var tds = $(this).find("td"); //find all td
var descriptionTD = tds.eq(1); //get the td for the description
//get the remaining tds that only contain " "
var emptytds = tds.not(descriptionTD).filter(function() {
return $(this).html() === " ";
});
//if all the remaing tds are empty
if (emptytds.length === tds.length - 1) {
emptytds.remove();
descriptionTD.prop("colspan", tds.length).prop("width", "100%");
}
});
});
remove <td> that has and make sub header
<td> </td>
Its the recommended pattern, cause it works on every browser. Try to run your table without   in Edge Browser, you will see that breaks the table layout.
<td>/td>
Without ' '
<td> </td>
With ' '
I have a Repeater with 2 LinkButtons that need to be displayed next to each other to build a graphical unit. Depending on the window size the line break would break that unit. How can I tie these 2 elements together? I was fiddling with div but the result was that each result of the repeater is displayed in a new row, while I need them to be displayed horizontal.
<asp:Repeater runat="server" id="RepeaterUsers" >
<ItemTemplate>
<div style="display:inline;">
<div style="white-space:nowrap;">
<asp:LinkButton ID="LB1" runat="server" OnClick="SelectUser"><asp:Label runat="server" Text='<%# Bind("Name") %>' ID="RepeaterName"/>
</asp:LinkButton><asp:LinkButton ID="LB2" runat="server" OnClick="EliminateUser"><img src="img/close33.png"></asp:LinkButton></div><asp:Label runat="server" Text=" " Height="24px" ID="LabelSpace"/></div>
</ItemTemplate>
</asp:Repeater>
The final solution is:
<asp:Repeater runat="server" id="RepeaterUsers" >
<ItemTemplate>
<div style="float:left">
<table class="RepeaterTable">
<tr>
<td class="RepeaterTD">
<asp:LinkButton ID="ButtonHWReminderAudit" runat="server" OnClick="SelectUser" Font-Size="11px" CommandArgument='<%# Bind("Name") %>' CssClass='<%# Eval("CSSName") %>' ><%# Eval("DisplayName") %></asp:LinkButton></td>
<td class="RepeaterTD">
<asp:LinkButton ID="LinkButton4" runat="server" CommandArgument='<%# Bind("Name") %>' OnClick="EliminateUser" Font-Size="11px" CssClass='<%# Eval("CSSX") %>'>
<img src="img/close33.png" border="0" style="vertical-align:text-bottom;height:21px"></asp:LinkButton></td>
<td class="RepeaterTD">
<asp:Label runat="server" Text=" " Height="25px" ID="LabelSpace"/></td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:Repeater>
and CSS:
.RepeaterTable {border-spacing:0;border-collapse:collapse;float:left;}
.RepeaterTD {padding: 0px;}
The column "Name" carries white spaces which occasionally might lead to a line break within one cell. Therefore I had to replace whitespace with a non-breakable-space (nbsp;) first.
In order to avoid gaps between the cells, you have to set spacing and padding to 0. This would provoke that no distance is kept between the tables. To set horizontal and vertical distances I added an additional cell.
I think you are looking for float: left
<asp:Repeater runat="server" ID="RepeaterUsers">
<ItemTemplate>
<div style="float: left; margin-bottom: 24px; clear: both">
<asp:LinkButton ID="LB1" runat="server"><%# Eval("Name") %></asp:LinkButton>
<asp:LinkButton ID="LB2" runat="server"><img src="img/close33.png"></asp:LinkButton>
</div>
</ItemTemplate>
</asp:Repeater>
Update
You could also use a table to align items, that always works. Although that is a bit old-school and against HTML-purists.
<table cellspacing="0" cellpadding="3" border="0">
<asp:Repeater runat="server" ID="RepeaterUsers">
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="LB1" runat="server"><%# Eval("Name") %></asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LB2" runat="server"><img src="img/close33.png"></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
issue.aspx
<div id="model_input_stock" runat="server" class="form-group well">Dynamically need to insert new textbox and lable with in new div</div>
Issue.aspx.cs
protected void Button_Grid_DT_Assign_Click(object sender, EventArgs e)
{
//use assigned in selecting issued quantity view
foreach(var item in StockCode_List)
{
model_input_stock.InnerHtml.???
}
}
Is their any best alternative way, i'm new in ASP.NET??
You can add an ASP.net panel inside of your div for first and then inside of your foreach loop initialize a new textbox and add it to your panel:
Textbox txt = new Textbox();
txt.id = "txt1";
Txt.Text = "My Text";
Panel.Controls.Add(txt);
Use the following code to do so:
Default.aspx:
<div>
<asp:TextBox ID="txtRows" runat="server"></asp:TextBox>
<asp:Button SkinID="b" ID="btnAddRow" runat="server" Text="Click To Proceed" OnClick="btnAddRow_Click" Width="220px" />
<asp:GridView SkinID="grv" ID="GridViewProducts" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="SL No.">
<ItemTemplate>
<%#Container.DataItemIndex + 1%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category">
<ItemTemplate>
<asp:DropDownList ID="ddlCategoryID" runat="server" AutoPostBack="True">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Name">
<ItemTemplate>
<asp:DropDownList ID="ddlItemID" runat="server" AutoPostBack="True">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Type">
<ItemTemplate>
<asp:DropDownList ID="ddlUnitTypeID" runat="server" AutoPostBack="True">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:TextBox ID="txtDes" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div style="padding: 10px 0px;">
<asp:Panel ID="Panel1" runat="server" Visible="false">
<asp:Button ID="btnSave" runat="server" Text="Submit" OnClick="btnSave_Click" />
<asp:Label ID="lblMsg" runat="server"></asp:Label>
</asp:Panel>
</div>
In the code-behind, Default.aspx.cs:
private void AddRowsToGrid()
{
List<int> noofRows = new List<int>();
int noOfInputs = Convert.ToInt32(txtRows.Text);
for (int i = 0; i < noOfInputs; i++)
{
noofRows.Add(i);
}
GridViewProducts.DataSource = noofRows;
GridViewProducts.DataBind();
if (GridViewProducts.Rows.Count > 0)
{
Panel1.Visible = true;
}
else
{
Panel1.Visible = false;
}
}
protected void btnAddRow_Click(object sender, EventArgs e)
{
AddRowsToGrid();
}
Finally you'll have the following output giving input 10:
trying to get value from textbox called tbAantalgv in a gridview within an updatepanel after button click. standard value after databind for tbAantalgv = 0, but when i change the textbox value to 1, the textbox value will NOT change.
For Each gvr As GridViewRow In gvOnderdelenLos.Rows
If gvr.RowType = DataControlRowType.DataRow Then
Dim lblCode As Label = gvr.FindControl("lblCode")
Dim lblArtikel As Label = gvr.FindControl("lblArtikel")
Dim tbAantalgv As TextBox = gvr.FindControl("tbAantalgv")
End If
Next
<asp:UpdatePanel ID="pnlOnderdelenLos" runat="server" style="width:95%; border:solid 3px white; display:none;" UpdateMode="Conditional" >
<ContentTemplate>
<asp:ImageButton ID="imgOnderdelenLosToevoegen" runat="server" OnClientClick="imgOnderdelenLosToevoegen()" ImageUrl="~/Afbeeldingen/Icons/table_row_insert.png"
style="vertical-align:text-top; float:right; margin-right:2%" />
<asp:SqlDataSource ID="dsOnderdelenLos" runat="server" ConnectionString="<%$ ConnectionStrings:AftersalesConnectionString %>"
SelectCommand="EXEC spAppPnlOnderdelenLos">
</asp:SqlDataSource>
<asp:GridView ID="gvOnderdelenLos" runat="server" DataSourceID="dsOnderdelenLos" AllowSorting="False" PagerSettings-PageButtonCount="20" ShowHeader="false"
AllowEditing="True" AutoGenerateColumns="False" CellPadding="3" GridLines="Both" AllowPaging="False" style="width:100%; margin-left:2px;">
<HeaderStyle CssClass="Gridview" />
<Columns>
<asp:TemplateField HeaderText="Artikelnummer" SortExpression="Code" ItemStyle-Width="25%" >
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# Bind("Code")%>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Artikel" SortExpression="Omschrijving" ItemStyle-Width="25%">
<ItemTemplate>
<asp:Label id="lblArtikel" runat="server" Text='<%# Bind("Omschrijving")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Aantal" SortExpression="Aantal" ItemStyle-Width="25%">
<ItemTemplate>
<asp:TextBox id="tbAantalgv" runat="server" Width="95%" text='<%# Bind("Aantal")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Kostenplaats" SortExpression="Kostenplaats" ItemStyle-Width="25%">
<ItemTemplate>
<asp:DropDownList ID="dlKostenplaats" runat="server" Width="100%">
<asp:ListItem Text="Werkplaats"></asp:ListItem>
<asp:ListItem Text="Verkoop"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="btnOnderdelenLosToevoegen" runat="server" OnClick="btnOnderdelenLosToevoegen_click" style="display:none;"/>
I do not understand what the issue can be.
Thanks in advance.
Frank
I have 4 labels inside a div, it looks like this:
I want them to display in one single line, how do I do this?
The code for labels:
<div style="float:left; width:50%">
<asp:UpdatePanel ID="loginPanel" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Label ID="LoginLabel" runat="server" CssClass="label-info" Font-Bold="true" ForeColor="White" Width="300px"/>
</ContentTemplate>
</asp:UpdatePanel>
<%--Login ID--%>
<asp:UpdatePanel ID="RolePanel" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Label ID="ShowRole" runat="server" CssClass="label-info" Font-Bold="true" ForeColor="White" Width="300px"/>
</ContentTemplate>
</asp:UpdatePanel>
<%-- role --%>
<asp:UpdatePanel ID="timePanel" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Label ID="ShowTime" runat="server" CssClass="label-info" Font-Bold="true" ForeColor="White" Width="300px"/>
</ContentTemplate>
</asp:UpdatePanel>
<%--<br />--%>
<asp:UpdatePanel ID="projPanel" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Label ID="ProjCount" runat="server" CssClass="label-info" Font-Bold="true" ForeColor="White" Width="300px"/> <%--project counter--%>
</ContentTemplate>
</asp:UpdatePanel>
</div>
I don't know with asp.net, but with a single css :
.label-info{
display : inline-block;
float : left;
}
It appears the part that has
<%--<br />--%>
is breaking the line apart. Once changing that, the CSS bit display: inline-block; will do the trick.
Please use following css property, because <asp:label/> gets rendered as <div>.
div.lable-info{
display:inline;
}