Trying to set view inside UpdatePanel in PageLoad - c#

I'm having trouble with a combination of an UpdatePanel and MultiView.
I have an UpdatePanel at my top level, inside I have a bunch of imagebuttons - their click events set the view on the Multiview, and each View has an UpdatePanel inside of it with my binding.
Everything works great - but I am trying to set the View via the Querystring, so I can send a user to a particular view.
When I try and set the View from PageLoad - it says 'object does not exist'. So I figured I'd try it in Page_LoadComplete, which works great - but then my imagebuttons don't work to switch the view like they originally did.
What am I missing! Thanks!
void Page_LoadComplete()
{
tabSelect= Request.QueryString["tab"];
if (tabSelect.Contains("Community"))
{
MultiView1.SetActiveView(Community);
btnCommunity.ImageUrl = "images/tabCommunity_on.png";
}
}
<asp:ScriptManager id="ScriptManager1" runat="server"/>
<asp:UpdatePanel id="UpdatePanel1" childrenastriggers="true" updatemode="Always" runat="server">
<ContentTemplate>
<asp:ImageButton id="btnCommunity" onclick="" runat="server">
<asp:MultiView ID="MultiView1" ActiveViewIndex="0" runat="server">
<asp:View ID="Community" runat="server">
<asp:UpdatePanel id="UpdatePanel1" childrenastriggers="true" updatemode="Always" runat="server">
//data controls in here
</asp:UpdatePanel>
</asp:View>
<asp:View id="tabFriends" runat="server">
<asp:UpdatePanel id="UpdatePanel2" childrenastriggers="true" updatemode="Always" runat="server">
//data controls in here
</asp:UpdatePanel>
</asp:View>
</asp:MultiView>
</ContentTemplate>
</asp:UpdatePanel>

UPDATE: After reviewing your code in further detail, I believe figured out the problem.
I made the following adjustments to the code:
If the querystring is not passed, set tabSelect to empty string and thus avoiding a null object reference exception on the next line.
Set the ImageUrl path to include ~ (for root).
Please try the code below:
void Page_LoadComplete()
{
string tabSelect = Request.QueryString["tab"] ?? string.Empty;
if (tabSelect.Contains("Community"))
{
MultiView1.SetActiveView(Community);
btnCommunity.ImageUrl = "~/images/tabCommunity_on.png";
}
}

Related

Could not find UpdatePanel with ID on page postback

I have below structure in my asp.net application.
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="overlay"></div>
<asp:Panel ID="pnlMain" runat="server" Visible="true">
<asp:UpdatePanel ID="upProfile" runat="server" UpdateMode="Always">
<ContentTemplate>
<div>
<table>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="upAttachment" runat="server" UpdateMode="Always">
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
<asp:Panel ID="pnlSuccessMessage" runat="server" >
<asp:Label ID="lblSuccessMessage" runat="server"></asp:Label>
</asp:Panel>
I have different textfields and dropdowns in the table of upProfile updatepanel and file upload control in upAttachment updatepanel. Now when I select one radio from the same update panel, I am getting below error and the upAttachment does not show up on page.
Uncaught Error: Sys.InvalidOperationException: Could not find UpdatePanel with ID 'MainContent_upAttachment'. If it is being updated dynamically then it must be inside another UpdatePanel.
I have searched a lot and tried below different things, but still it is not working
1. Set the upAttachment UpdateMode to Conditional.
- In this case, it does not throw error, but the file upload control which is inside that updatepanel, it does not show up
2. Place the upAttachment updatepanel inside another updatepanel, but that too is not working.
I don't know what to do in this case. Can anyone help me on this?

Set Ajax Tab Container Width to 100%

I have an ajax container which will be created dynamically in C#. I would like the container width to be set to 100%. Have researched online for quite awhile but still can't figure out how to set the width to 100%.
This is my codes:
//Aspx file
<div>
<asp:scriptmanager ID="ScriptManager1" runat="server">
</asp:scriptmanager>
</div>
<asp:updatepanel ID="UpdatePanel1" runat="server" ScrollBars="Horizontal">
<contenttemplate>
<asp:placeholder ID="PlaceHolder1" runat="server"></asp:placeholder>
<asp:Chart runat="server">
</asp:Chart>
</contenttemplate>
</asp:updatepanel>
//Cs file
AjaxControlToolkit.TabContainer container = new AjaxControlToolkit.TabContainer();
container.ID = "TabContainer";
container.EnableViewState = false;
container.Tabs.Clear();
container.Height = Unit.Pixel(2000);
container.Width = Unit.Pixel(2000);
container.Tabs.AddAt(0, GetManualTab());
Question: How to set Ajax tab container width to 100% if the container is created dynamically in c#?
Appreciate if anyone provide me help on this. Thanks a lot!!
container.Width = Unit.Percentage(100);

ASP.net Gridview moving to top of page instead of refreshing in it's current location

I have a webpage that is just a bunch of gridviews on the page, they display data pulled from a SQL database (using Stored procedures). I have the whole page using a javascript defined refresh function, and it works great (timer is set for every 10 mins).
I have one Gridview that I want to refresh every 15 seconds (they want that data to be the most current always). I have tried 3 different ways to get this functionality, but each has the same side effect. Instead of refreshing that specific gridview, it makes another gridview at the top of the page with the data. I'm not sure what I'm missing here, so any help would be appreciated.
Below is the code for the grid view I'm having issues with, if you need more code, please let me know.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="PDUpdatePanel" UpdateMode="Conditional" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="15000">
</asp:Timer>
<td valign="top">
<asp:GridView ID="PD" AutoGenerateColumns="true" runat="server">
</asp:GridView>
</td>
</ContentTemplate>
</asp:UpdatePanel>
EDIT
I have tried the above (using the script Manager/UpdatePanel/Trigger), I have tried using this (using the Timer1_Tick to call to rebind)
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="PDUpdatePanel" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="15000" ontick="Timer1_Tick">
</asp:Timer>
<asp:Panel runat="server">
<td valign="top">
<asp:GridView ID="PD" AutoGenerateColumns="true" runat="server">
</asp:GridView></asp:Panel>
</td>
</ContentTemplate>
</asp:UpdatePanel>
When this I tried the simple, just call the SP and rebind
protected void Timer1_Tick(object sender, EventArgs e)
{
DataAccess.PDGet(GridView PD);
}
public static void PDGet(GridView PD)
{
//Create connection
using (AdoConnNet conn = new AdoConnNet(Config.GetConfigKeys("AppName"), Config.GetConfigKeys("LogPath")))
{
conn.SetConnection(Config.GetConfigKeys("SERV_CONVEYOR"), "Conveyor");
string spTimeOutCountGet = Config.GetConfigKeys("TimeOutCountGet");
using (SqlCommand cmd = new SqlCommand())
{
//Call SP and load the results
using (SqlDataReader dr = conn.RunSPReturnDataReader(spTimeOutCountGet, cmd))
{
PD.DataSource = dr;
PD.DataBind();
}
}
}
}
Lastly, I tried making a helper function one that get the data from the Database, and another to do the binding (grasping at straws at this point).
The side effect is the same with every way I tried. Instead of refreshing the GridView PD in it's current location, it makes a new Gridview at the top of the page (and pushing the rest of the gridviews down). I'm not savvy with Web pages, so I'm unsure why it's drawing it at the top instead it's current location.
Thanks for the help!

Post Data from contentpage to Masterpage using ajax technique

I'm working on web application based on ASP.Net and C#.
When i click the button in Content page, I want to pass data(string) to Masterpage and show it to the user. I know the technique and codes but the problem is with Ajax.
Since the button in content page is inside of Updatepanel, when i click the button, message will not be appear in masterpage. Because only part of the page wil be refrsh and i do not know, how to refesh master page
Code inside contentpage:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder_Body_Member" ...>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ... ></asp:TextBox>
<asp:Button ID="ShowText" ... />
</ContentTemplate>
</asp:UpdatePanel>
Code inside masterPage (actually this is a nested masterPage):
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder_TopRightSide" runat="server"> ... </asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder_Menu" runat="server"> ... </asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder_Body" runat="server">
<asp:ContentPlaceHolder ID="ContentPlaceHolder_Body_Member" runat="server"> </asp:ContentPlaceHolder>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder_Footer" runat="server">
<asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
<ContentTemplate>
<asp:Label ID="DataFromPage" ... ></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Button is insde of "ContentPlaceHolder_Body_Member" and the label is inside of "ContentPlaceHolder_Footer"
To achieve the result that you need you just need to make a little change on your Master page design like the code below:
On your master-page place the content-place-holder inside the update panel first:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="DataFromPage" runat="server" Text="Label"></asp:Label>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
Then in your Content page you will have something like this (almost the same as yours)
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Finally in your code-behind on button-click event you could assign your value to the master page's label control like the code shown below:
protected void Button1_Click(object sender, EventArgs e)
{
Label myLabel = Page.Master.FindControl("UpdatePanel1").FindControl("DataFromPage") as Label;
myLabel.Text = "this is a message from my content page";
}
And here is the result :
UPDATE 1
Ok remember if you have a nested update panel then the way you find the control in that one is a bit different also if you need to make changes on the other content page you need to force the update panel to make the changes in your code behind, so your code should be like this:
//your button control's event needs to find the update panel and the label both:
protected void Button1_Click(object sender, EventArgs e)
{
UpdatePanel myUpdatePanel = Page.Master.Master.FindControl("ContentPlaceHolder_Footer").FindControl("UpdatePanel") as UpdatePanel;
Label myLabel = Page.Master.Master.FindControl("ContentPlaceHolder_Footer").FindControl("DataFromPage") as Label;
myLabel.Text = "this is a message from my content page";
myUpdatePanel.Update(); //force the panel to get updated
}
Cheers

Not using asp listbox well

All I wish to do is simply click a button and the text in a textbox is automatically added as an item in the listbox. Shouldn't this be straight forward? Whilst debugging, the item is added and I can see the text by watching ListBox1.Items[0], but nothing is displayed in the web page. I had the same problem which i did not solve, in a console application! Can some one please guide me to what I am doing wrong?
protected void Button1_Click(object sender, EventArgs e)
{
ListBox1.Items.Add(new ListItem(TextBox1.Text));
}
Many thanks
Edit:
In a past project, I used the DataSource property, which worked perfectly. I have never yet managed to use the add Items! May be there is some sort of refresh or update?
Page code:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:ListBox ID="ListBox1" runat="server" Height="150px" Width="295px"></asp:ListBox>
<asp:UpdatePanel ID="updatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
Looks like your listbox is outside of the update panel. Pop it inside the update panel:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="AddItem" />
</ContentTemplate>
</asp:UpdatePanel>
You have to move the ListBox into the UpdatePanel, otherwise it will not be updated.
The reason for that is, that ASP.NET is sending the whole HTML of the UpdatePanel back to the client. Since the ListBox is not part of the UpdatePanel, it won't be updated.

Categories