How to change view in asp.net? - c#

Im wondering how i can change "views" with a button click in Visual Studio (asp.net)?
I have 2 .aspx files, one called Default.aspx and another one called Library.aspx. How can i make a button change the views to display the other aspx file?
I tried googling but didnt find anything useful. Any help here is appreciated. Thanks

Ok, assuming default.aspx is in desing mode, then just drop a button from the toolbox onto the web page.
Say, like this:
Now, double click on the button, and we are jumped to code behind.
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Library.aspx");
}
So, we put for the code to jump to the web page Library.aspx
We get this then:
And now, when I click on that button, I am jumped to the web page called Library.aspx - we should see this (assuming the web page Library.aspx exists).

Related

Display/hide control in Webpart based on edit/browse mode

I have a Visual Studio project in which I have created a Visual Webpart. In the user control I have a panel which I want to display in edit mode and hide in browse mode.
ASP.NET code snippet:
<asp:panel runat="server" ID="myControl">
C# code snippet in user control code behind:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
WebPartManager wpm = WebPartManager.GetCurrentWebPartManager(this.Page);
if (wpm.DisplayMode == WebPartManager.BrowseDisplayMode)
{
myControl.Attributes.Add("style", "display: none");
}
else if (wpm.DisplayMode == WebPartManager.EditDisplayMode)
{
myControl.Attributes.Add("style", "display: block");
}
}
This works, but if I have two same webparts on same page and put one webpart is edit mode it shows the panel in both webparts. It seems the OnPreRender event code runs for both webparts on page.
I even tried writing code as this.myControl.Attributes.Add("style", "display: block"); but it still didn't work.
I want the OnPreRender code to run only on its webpart and not modify the other webaprt on page. How can I resolve this? Is there any better (or preferred) way to do it?
NOTE: I need to use display: none because the panel would be accessed via JavaScript.
PS: This is a cross post from here as I did not get any satisfactory answers.
I do the same on the page load, and it works for me just fine .. (page has about 10 WP and only 1 behaves differently in the edit mode.)
Maybe what you mean is that you have 2 SAME webparts on the page? Then I guess both webparts will behave equally because WebPartManager.DisplayMode returns you the mode of the page, not the webpart. (see msdn).

ASP.NET and C# Redirect

I am working on a project for school, and this is an extra credit part. I have a project started in VS 2010 using master pages, and what I'm trying to do is get a "Submit" button to redirect people to the "MyAccounts.aspx" page. My current code for the ASP part for the button looks like this:
<asp:Button ID="btnTransfer" runat="server" Text="Submit"/>
I have tried adding in the OnClick option, as well as the OnClientClick option. I have also added this code to the Site.Master.cs file as well as the Transfer.aspx.cs file:
protected void btnTransfer_Click(object sender, EventArgs e)
{
Response.Redirect(Page.ResolveClientUrl("/MyAccounts.aspx"));
}
When I run this and view the project in my browser, the whole thing runs fine, but when I click on the "Submit" button, it just refreshes the current page and does not properly redirect to the MyAccounts page. Anyone have any ideas for me?
You are doing it almost correctly, you just haven't put the correct pieces together. On Transfer.aspx, your button should be:
<asp:Button ID="btnTransfer" OnClick="btnTransfer_Click" runat="server" Text="Submit"/>
and your code behind should be like what #KendrickLamar said:
protected void btnTransfer_Click(object sender, EventArgs e)
{
Response.Redirect("~/MyAccounts.aspx");
}
The OnClick event tells it what to execute on post-back when the users clicks the button. This is in the code-behind for Transfer.aspx, not the site master.

enable or disable side menu in global master file c# asp.net website

I'm writing a website in Visual Studio Web Developer 2010 (Express edition). I have created a Master file which my content is styled with.
I want to put a right hand menu in my master file, but from the actual website pages, I want to say whether it should show particular menu items. For example, the home page would have a certain set of menu items on the right where as the contact page might have another set.
Should I set it up so that the master file handles true or false as to whether to show certain menu items (default all to false)... or should I handle this from the content pages? ie: call menu functions to draw from there?
You can add a master page declaration to the page, so that you can access it programmatically like so:
<%# MasterType virtualPath="~/MasterPage.master"%>
Put that right under the Page tag on the page you want to enable or disable access from.
Then, in your code behind, you can access the master page methods, one of which can be a method to enable or disable that side menu.
Something like:
Master.MyEnableMenuMethod();
Additionally, you can add that Master Page declaration dynamically, like so:
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/MasterPage.master";
}
See http://msdn.microsoft.com/en-us/library/c8y19k6h(v=vs.85).aspx for more.
The way I've done this sort of thing is by putting some code withing the menu markup as so:
<% if(!HttpContext.Request.Path.Contains("Contact.aspx")) { %>
<li> Contacts</li>
<%}%}>
And so on...

Submit a form from code behind

I'm having trouble implementing a functionality on my c#/asp.net app.
I have a form with a RadioButtonList and a submit button.
The RadioButtonList is generated on Page_Load() from a list of objects I retrieve from the database.
I would like to automatically submit the form if there is only 1 object in the list.
I have access to my Form object, to the submit button etc... but I can't seem to find a solution (in the end I'm looking for a kind of form.Submit() ) method.
Does anyone have an idea of how I could do this ?
Thanks in advance !
EDIT >> Here is the code :
.aspx : http://pastebin.com/0E6T7dqH
.aspx.cs : http://pastebin.com/54payZJP
EDIT2 >>>
As it seems there is no way to do what I wanted to do at first, I ended up using a session variable with a response.redirect()
Source :
http://dotnetslackers.com/Community/blogs/haissam/archive/2007/11/26/ways-to-pass-data-between-webforms.aspx
Post happens in the client side. As in Page_Load you are currently executing in the server side, just call the code you want to execute on post.
Edit: For actually going to another aspx
public void Page_Load(object sender, EventArgs e) {
if(!IsPostback && OnlyOneItem) {
Server.Transfer("TheOtherPage.aspx");
}
}
Server.Transfer will maintain the entire request, so your post data will be available.
http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.transfer.aspx
Try something like this
In your Page_Load
if(!IsPostBack)
{
if(check for only one object)
{
//your submit code
}
}
I actually had to do something similar, once. Here is a way you can do it.
Asp.Net buttons have a property called PostBackUrl, and it does exactly what you would expect - it controls where the form will post if you click the button.
You can also use the RegisterStartupScript function to render javascript on the page.
Now, with these two pieces, you can achieve your goal.
if(!IsPostBack)
{
if(results == 1)
{
button.PostBackUrl = "next page's url"
//Register script to click the button using RegisterStartupScript
}
}
Now, having shown you this, I will warn you it may not make for the best user experience. When I did it, it was for a very specific case that had no other solution. The page will actually post back to the user, and they will see the page for a moment before the javascript to click the button takes effect. Additionally, when you set a button's PostBackUrl, that means that when it is clicked, your entire form will be posted to the page specified. The code behind for the current page will not fire at all, so if you have any validation, it won't run.
There's nothing wrong with letting the user click the button to submit the form even if they only have one choice. In my experience, users like to feel like they are in control on the system; they don't like it when pages just do things without their input.
Also, there is not really anything wrong with putting the information the next page needs into the session or even a database table, and using Response.Redirect. It's a fairly common practice and works reliably in most scenarios.

FieldValidators and Visual Web Part (Sharepoint 2010)

I have a visual web part (a simple form) with RequiredFieldValidators. But a problem has occured since the field validators block editing of the page. When i press Edit Page in Sharepoint it starts to load then the validators fire and the javascript is stopped.
I found a solution but i cant get it to work.
Like this
public override void CreateChildControls()
{
if(SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
((UserControl)_ctl).EnableValidators(false);
((UserControl)_ctl).EnableValidators(true);
}
//But the _ctl does not exit i only have
Control control = Page.LoadControl(_ascxPath);
A litte advice would help this sharepoint noob a great deal.
The form is just Labels, Inputs, a button, updatepanel and requiredFieldValidators
Can you not just set the ValidationGroup for the validators and button? Like this: http://msdn.microsoft.com/en-us/library/ms227424.aspx
The validators should only fire when the button is pressed.
There is something wrong with your configuration - I tested this configuration and the required field validators definitely don't block editing on the page. Do you want to post your page and your code behind? Might help to troubleshoot the problem.

Categories

Resources