Asp:Label is not shown when visible is set to true? - c#

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>

Related

dynamically adding controls does not displayed inside panel in asp.net web form

I have a web form with a table where one row contains panel to add fileupload controls dynamically
the code is as below
<tr>
<td style="width:70%;display:block; overflow:visible;" >
<asp:Panel ID="ImagePanel" runat="server">
<uc1:AddNewImage runat="server" id="AddNewImage" /></asp:Panel>
<asp:Button ID="AddImage" OnClick="Unnamed_Click" Text="Add New Image" runat="server" CausesValidation="False" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="Please Enter Model Items" ForeColor="Red" ControlToValidate="TxtItems"></asp:RequiredFieldValidator>
</td>
</tr>
In button click event i am adding new usercontrol to ImagePanel using following code
Controls.AddNewImage Obj = (Controls.AddNewImage)LoadControl(#"~/Controls/AddNewImage.ascx");
this.ImagePanel.Controls.Add(Obj);
Problem is only two controls are displayed inside the panel, i need to allow upto five controls, but these controls are not displayed inside panel. What should i need to do to get displayed these controls inside the panel. do i need to set any css for Panel. Panel is inside a tag.
That's because dynamically added controls are cleared when page PostBack. Simply solution is to add <asp:HiddenField id="hidCount" runat="server" value="1"/> and update your click event like this:
int count = int.Parse(hidCount.Value)++;
hidCount.Value = count.ToString();
for(i=0;i< count;i++) {
Controls.AddNewImage Obj = (Controls.AddNewImage)LoadControl(#"~/Controls/AddNewImage.ascx");
this.ImagePanel.Controls.Add(Obj);
}
This will work perfectly.

Can a CollapsiblePanelExtender expand from code behind?

I have added a checkbox to the rows of a gridview that when clicked will expand a panel in the row. My gridview needs to use paging, so I am saving in a session variable the state of the current page before the page change. When the user clicks back to a page I am repopulating the checkboxes, but this does not expand the panels. Is there away to expand the panels from the code behind?
<asp:CheckBox runat="server" Text="Order Updated Records" ID="cbUR" Visible='<%# !DBNull.Value.Equals(Eval("AnyBox"))%>' />
<asp:CollapsiblePanelExtender ID="cInst" runat="server" TargetControlID="inst" Collapsed="true" AutoExpand="true" AutoCollapse="false" ExpandControlID="cbUR" CollapseControlID="cbUR" />
<asp:Panel ID="Inst" runat="server">
<asp:TextBox runat="server" ID="txtInst" TextMode="MultiLine" Width="200" />
</asp:panel>
I tried adding the panel, textbox and panel extender from the code, but could not get it to work. I read on a different post that the whole gridview would need to load from the code for this to work.
I would love to use something like
<asp:CollapsiblePanelExtender ID="cInst" runat="server" TargetControlID="inst" Collapsed='<%#!Convert.ToBoolean(rowItems[index].ToString()) %>' />
Would it be better not use the CollapsiblePanelExtender and find a different way to show the panels?
You could try adding a public/protected method:
public bool IsCollapsed(object rowId) {
//get row by ID here and return true if collapsed
return ....
}
that would return a value for a row that needs to be expanded or collapsed.
and use it like:
Collapsed='<%# IsCollapsed(Eval("RowId")) %>'
where RowId is a property that represents the ID of the item.

how to stop page jumping and maintain both panel & browser scroll position when user click on datalist item?

I have a datalist inside the panel
<asp:Panel ID="Panel1" runat="server" ScrollBars="Vertical">
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" OnItemDataBound="DataList1_ItemDataBound" CaptionAlign="Left">
<ItemTemplate>
<asp:ImageButton ID="btn1" runat="server" ImageUrl='<%#"~/Images.ashx?Name=" +DataBinder.Eval(Container.DataItem, "Path") %>'
OnCommand="Item_Command" CommandArgument='<%# Eval("Id").ToString() +";"+Eval("Path")%>' />
</ItemTemplate>
</asp:DataList>
</asp:Panel>
On Item_Command I am adding the style to btn1 such as border and color, and getting the id of selected item
string[] str = e.CommandArgument.ToString().Split(';');
Id = Convert.ToInt32(str[0]);
When I am selecting the item page is jumping, suppose I have scrolled browser scrollbar to bottom and selected an image browser scroll is going on top and again its coming on bottom, On Item_Command I am enabling and disabling few other btn which is out side of datalist, if i am keeping the datalist inside the updatepanel then all other btn is not getting enable or disable thats why i am not using update panel. I have all the controls inside the update panel. I tried with how to retain the browser scroll position after any event gets fire but its not working, i am setting
Page.MaintainScrollPositionOnPostBack = true;
AutoEventWireup="true"
that also not working for me. I am using VS 2010, 4.0 with C#.
Some one plz tell me how to stop page jumping and maintain both panel1 and browser scroll position when user click on datalist item?
thanks in advanced..
Use a clientside function on Client click event of your image button like this
javascript:void(0);
, it works for me in update panel,
<asp:ImageButton OnClientClick="javascript:void(0);" ID="btn1" runat="server" ImageUrl="~/Help2.png" AlternateText="asas"/>
Edit
Put the button & datalist inside a updatePanel. Set
UpdateMode=Conditional
for the udpatePanel.
When you perform your operation on the dataList row. Just update the update panel manually like this
// Your image button event
YourUpdatePanel.Update();
This coupled with what I posted earlier should fix your problem. Also you might not need MainScrollPositionOnPostBack=true by using above.
Damien.
Well, here's the thought: use either one big update panel, that covers the whole page to maintain page scrolling OR use multiple update panels to cover all items you want to have updated!
Try to set MaintainScrollPositionOnPostback true within Page tag:
<%# Page (...) MaintainScrollPositionOnPostback="true" (...) %>

Setting aspx table's visibility to true fails

I'm puzzled here. I have a webform with 3 tables in it. I want to show/hide them according to certain conditions. It all works fine except in one situation.
this is what I have:
<asp:UpdatePanel ID="upGeneral" runat="server" >
<ContentTemplate>
<table id="tab1" runat="server" visible="true" width="100%">
...
</table>
<table id="tab2" runat="server" visible="false" width="100%">
...
</table>
<table id="tab3" runat="server" visible="false" width="100%">
...
</table>
</ContentTemplate>
</asp:UpdatePanel>
then, I have a few buttons added to the page, and depending on which one is pressed, I'll change the table's visibility.
My problem is that, under certain conditions, I'm changing tab3's visibility to true, and tab1 and tab2's to false, and while tab1 will have it's visibility set to false, tab3 will not have it's visibility set to true... sigh!
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
...
if (!editMode)
{
tab1.Visible = false;
tab2.Visible = false;
tab3.Visible = true;
}
}
}
in debug mode, as I go through these instructions, tab3.visibile = true will not change tab3's property!
Has this happened to you before? How did you solve it?
Thanks a lot!
The problem could lie with the update panel.
If you are trying to update content from outside of the update panel then you will need to specify the control that will update the panel in the post back element of the panel
<asp:PostBackTrigger ControlID="Button1" EventName="Click">
Without specifying the trigger then the update panel won't update the properties within it.
Well, it's possible that your problem lies here:
if (!Page.IsPostBack)
This will cause the statement inside the if condition to be ran only once - when the page
is firstly loaded.
When a postback is issued (i.e you pressed a button), this statement won't be reached.
Try changing to
if (Page.IsPostBack)

ASP.NET: Custom Control Reloads Whole Page

I'm working on a ASP.NET site with C# code.
Now the trouble starts when I create a custom control programmatically. The control displays in a panel, but when I click one of the buttons of the control it does nothing. If I click them twice, the user control disappears.
Using the debugger, I found that it's doing a postback, which is strange because I tried using buttons and setting the usesubmitbehavior to false; it's still sending postbacks.
Here is where the control is inserted on the default.aspx file
<asp:UpdatePanel runat="server" ID="contentHolderUpdatePanel"
UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel runat="server" ID="contentPanel">
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
Here is the ASPX from ListadoAuditoria of the control.
<asp:UpdatePanel runat="server" ID="auditorTableUpdatePanel" UpdateMode="Conditional">
<ContentTemplate>
<asp:Table runat="server" ID="auditorTable" BorderWidth="0" Width="100%">
<asp:TableHeaderRow HorizontalAlign="Center">
<asp:TableHeaderCell>Button
</asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel runat="server" ID="formHolderUpdatePanel" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label runat="server" ID="testLabel" Text="bbbbbbbbbbbbb" ></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
The method that the button invokes should change the text of the label testLabel from "bbbbbbbbbbbbb" to "aaaaaaaaaaa". Obviously I'm doing an auditorTableUpdatePanel.Update() after I modify the text.
The control CS
protected void Page_Load(object sender, EventArgs e)
{
loadAudits();
}
public void loadAudits()
{
for(int i=0;i<10;++i)
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
ImageButton deleteButton = new ImageButton();
deleteButton.ImageUrl = "~/image.gif";
deleteButton.Click += generateNewPart;
deleteButton.EnableViewState = true;
deleteButton.ID = i.ToString();
cell1.Controls.Add(deleteButton);
row.Cells.Add(cell1);
auditorTable.Rows.Add(row);
}
}
public void generateNewPart(object sender, EventArgs e)
{
tumadre.Text = "aaaaaaaaaaaa";
formHolderUpdatePanel.Update();
}
And here is the code when I generate the control and insert it into the panel:
Panel panel = (Panel)Page.FindControl("contentPanel");
UpdatePanel updatePanel = (UpdatePanel)Page.FindControl("contentHolderUpdatePanel");
ListadoAuditorias listadoAuditorias = (ListadoAuditorias)LoadControl("~/CargaDeAuditoria/ListadoAuditorias.ascx");
panel.Controls.Add(listadoAuditorias);
updatePanel.Update();
I looked over the Internet and didn't found anything.
I'm not sure where exactly the code to dynamically add the controls is, but it must be called on EVERY postback to re-add the controls. You can't just add it once and forget about it. When you postback, the page will re-render with the markup in your aspx page (which does not have your dynamic controls, obviously). The values from the dynamically added controls will still be in ViewState, but the controls will not be re-rendered.
I'm not entirely sure what I'm looking at; I don't know if the "code of the control" is the code for the ListadoAuditorias control that you're loading. If so, I didn't notice any buttons.
So I could be wrong here, but the first thing that pops out at me is that it looks like you're loading the ListadoAuditorias control and then adding it to a normal Panel control. If one of the controls inside of ListadoAuditorias triggers a postback, and it's not contained within an UpdatePanel, then yes, I'd expect the page to do a postback and reload, unless you have specified the ChildrenAsTriggers and UpdateMode properties to be something other than their default values (I think). So I would just suggest that you take a look at where you are adding your controls. Make sure that they're contained within an UpdatePanel, if that's what your intention is.
Also, note that the UseSubmitBehavior property of the Button control does not prevent the button from initiating a postback. That property only determines whether the button gets rendered as <input type="submit" /> or <input type="button" />. In the latter case (when you set UseSubmitBehavior to false) the control still renders javascript in the HTML element's onclick attribute to cause a postback.
EDIT: I've amended my explanation regarding UpdatePanel control to mention the ChildrenAsTriggers and UpdateMode properties.

Categories

Resources