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)
Related
I have simplified and probably made a typo or two below, but I am trying to show and hide the visibility of pnl2, this code however doesn't work. I thought as long as the panel was in another update panel I can control visibility.
Oddly enough if I debug this in Visual studio, and F10 all the way through it, it shows the visibility correctly, but when I hit my Last F10 and the page loads, it's always incorrect.
<asp:UpdatePanel runat="server" UpdateMode="Always" ID="updPanel1">
<ContentTemplate>
<asp:Panel runat="Server" ID="pnl1"/>
<asp:Button runat="Server" ID="hidePanel2" OnClick="HidePanel2"/>
</ContentTemplate>
</UpdatePanel>
<asp:UpdatePanel runat="server" UpdateMode="Always" ID="updPanel2">
<ContentTemplate>
<asp:Panel runat="Server" ID="pnl2"/>
</ContentTemplate>
</UpdatePanel>
protected void hidePanel2(object sender, EventArgs e)
{
if (pnl2.Visible == true)
{
pnl2.Visible = false;
}else
{
pnl2.Visible = true;)
}
The problem is that you are trying to update the visibility of “pnl2” from a button that is in the UpdatePanel named “updPanel1”. The postback for the button “hidePanel2” only causes the content of update panel “updPanel1” to change. The rest of the form, including update panel “updPanel2”, does not change.
The most direct solution, if the two panels are next to each other, is to put them both in one update panel. If you have some other layout then you will need to explain what it is before we can help.
I have got a grid[Grid1] that build its dataRows when a button[search] is clicked, I managed to Ajaxify it by placing it in an UpdatePanel and it worked fine. Before Ajaxifying Grid 1, another grid[Grid2] and some other controls[Text and Labels] used to get populated/updated when a row in Grid 1 was clicked .
The Grid2 and other controls used to get populated/updated on the OnItemCommand Event of Grid 1.Its the code in the OnItemCommand that binds the related data to Grid2 and other controls.
After I placed the Grid 1 in the update panel,they stopped updating. It will work fine if I place Grid2 and other controls in the same Update Panel but the page is designed in a way that I cant have those controls in the same UpdatePanel as the first Grid nor I dont intend to use another Update Panel.
I hope I'm making some sense. I'm a newbie in .Net so please excuse. Please find the code below.
<asp:ScriptManager EnablePartialRendering="true" ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers ="True">
<ContentTemplate>
<asp:DataGrid ID="grdJobs" runat="server" AllowPaging="true"
AlternatingItemStyle-CssClass="gridAltItemStyle"
AutoGenerateColumns="False" CellPadding="0"
DataKeyField="code"
CssClass="datagridBox"
GridLines="horizontal"
PagerStyle-Mode="NumericPages"
HeaderStyle-CssClass="gridHeaderStyle"
ItemStyle-CssClass="gridItemStyle"
PagerStyle-CssClass="gridPagerStyle"
Width="445px" OnPageIndexChanged="grdJobs_PageIndexChanged" OnItemCreated="grdJobs_ItemCreated" OnItemCommand="grdJobs_ItemCommand" OnItemDataBound="grdJobs_ItemDataBound">
<Columns>
<asp:BoundColumn DataField="J_ID" HeaderText="Job"></asp:BoundColumn>
<asp:BoundColumn DataField="Contract" HeaderText="Contract" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="J_Fault_Line1" HeaderText="Fault" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="j_p_id" HeaderText="Fault" Visible="false" ></asp:BoundColumn>
<asp:ButtonColumn Text="<img src=images/addFeedback.gif style=border: 0px; alt=Add Feedback>" ButtonType="LinkButton" HeaderText="Add" CommandName="Load" ItemStyle-cssClass="Col_9_Item_2"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
<asp:ImageButton ID="cmdLkp" ImageUrl="Images/search.gif" runat="server" OnClick="cmdLkp_Click" />
</ContentTemplate>
</asp:UpdatePanel>
The code below in the code behind stopped working
protected void grdJobs_ItemCommand(object source, DataGridCommandEventArgs e)
{
if (e.CommandName == "Load")
{
functionToBindDataToGrid2();
functionToBindDataToOtherControls();
}
}
protected void grdJobs_ItemDataBound(object sender, DataGridItemEventArgs e)
{
e.Item.Attributes.Add("onclick", "javascript:__doPostBack('grdJobs$ctl" + ((Convert.ToInt32(e.Item.ItemIndex + 3).ToString("00"))) + "$ctl00','')");
}
In the properties for the UpdatePanel, set the update mode to "Conditional" and ChildrenAsTriggers to "true".
Another option would be to move the button inside the update panel so that you wouldn't have to have the trigger.
A GridView is a complex asp.net server control. You will have a lot of difficulty updating Grid2 after Grid1 is updated inside of the UpdatePanel. However, it is possible to a execute JavaScript on the client after Grid1 is updated. You could update Grid1 inside of the update panel, execute JavaScript after Grid1 has been updated that will update HTML on the page. The problem is that updating Grid2 with Javascript is going to be a nightmare amount of work.
Here's an example of what I'm talking about: Ajax Enabled Gridview using JavaScript in ASP.NET. It is a total hack, a huge amount of work, and your co-workers will hate you when they have to maintain it.
If you wanted to update a label or a dropdown list then that would be possible but updating a GridView using Javascript and having those updates persist across postbacks is a daunting challenge.
Use multiple UpdatePanels on one page. It won't affect the layout.
You can then use triggers (for the panels) in order to affect which controls you want affected.
Here's a page that will help you understand.
http://www.asp.net/ajax/tutorials/understanding-asp-net-ajax-updatepanel-triggers
Your problem is down to the way you are triggering the postback on row click, i.e.: this method
protected void grdJobs_ItemDataBound(object sender, DataGridItemEventArgs e)
{
e.Item.Attributes.Add("onclick", "javascript:__doPostBack('grdJobs$ctl" + ((Convert.ToInt32(e.Item.ItemIndex + 3).ToString("00"))) + "$ctl00','')");
}
Manually writing the __doPostBack script is generally always a bad idea. I believe what you need to use instead is ClientScriptManager.GetPostBackEventReference which will create a similar looking postback script for you, but will take into account all sorts of other things including AJAX-ification of the control.
Try something like this instead:
protected void grdJobs_ItemDataBound(object sender, DataGridItemEventArgs e)
{
e.Item.Attributes.Add("onclick", ClientScriptManager.GetPostBackEventReference(e.Item, string.Empty);
}
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.
I need to display a control consistently across a set of pages. So I'm using a MasterPage to do that in ASP.NET/C#. However I also need to programmatically access this control, mostly provide options to view/hide depending on whether the controls checkbox is clicked.
Here is the Source for the MasterPage
<div id="verifyInitial" runat="server">
<asp:CheckBox ID="chkInitialVerify" runat="server"
oncheckedchanged="chkInitialVerify_CheckedChanged" />
I agree that my initial data is correct.
</div>
<div id="verifyContinuous" runat="server">
<asp:CheckBox ID="chkContinuousVerify" runat="server"
oncheckedchanged="chkContinuousVerify_CheckedChanged" />
I agree that my continuous data is correct
</div>
Now in the code behind I want to perform the following operations. Basically if a person clicks on the checkbox for the initial div box, then the initial box disappears and the continous box shows up. However it seems that the code behind for the MasterPage does not activate whenver I click on the checkbox. Is that just the way MasterPages are designed? I always thought you could do add some sort of control functionality beyond utilizing the Page Load on the Master Page.
protected void chkInitialVerify_CheckedChanged(object sender, EventArgs e)
{
verifyContinuous.Visible = true;
verifyInitial.Visible = false;
}
protected void chkContinuousVerify_CheckedChanged(object sender, EventArgs e)
{
verifyContinuous.Visible = false;
}
If you're expecting the two controls to trigger a change for that page immediately then you'll need to set the AutoPostBack property to true for both of them:
<div id="verifyInitial" runat="server">
<asp:CheckBox ID="chkInitialVerify" runat="server" oncheckedchanged="chkInitialVerify_CheckedChanged" AutoPostBack="true" />
I agree that my initial data is correct.
</div>
<div id="verifyContinuous" runat="server">
<asp:CheckBox ID="chkContinuousVerify" runat="server" oncheckedchanged="chkContinuousVerify_CheckedChanged" AutoPostBack="true" />
I agree that my continuous data is correct
</div>
Otherwise, you need an <asp:button /> or some other control on the page to trigger a postback and cause your event handlers to run. The button, or other control, can either be on your masterpage or on your content page, the choice is entirely yours.
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>