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.
Related
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).
I've been struggling with this for a day. Not finding any others with my exact situation, so I figured I'd post and answer with what worked for me.
Environment: Asp.net 4.0 - AjaxControlToolkit v.7.1213.0
Problem: I have a TabContainer with 3 tabs, and based on a database value, I will make the 2nd tab invisible or not. My problem is that when I make that second tab invisible it makes the entire tabContainer invisible. When I inspect source, I can see the control is rendered on the page (tested in Firefox And Chrome), but there's now a style tag (visibility:hidden) that is coming from somewhere NOT in my code (master page, child page, style.css, c# codebehind files, etc), as far as I can tell. I have yet to find an explanation for this errant style tag. I'm not an ASP.net master, so it could be some idiosyncracy with my code, but it's also possible that this is a bug with AjaxControltoolkit.
I'll answer this with the workaround that is currently working for me.
Workaround:
Since I only need to remove / hide this from my users, I am able to use the Tabcontainer.Remove method. When using this method, my tabContainer no longer disappears after postback when it contains an invisible tab.
I replaced:
if(x.value == true)
tabpanel1.Visible == false;
with:
if(x.value == true)
tabContainer1.Tabs.Remove(tabpanel1);
I have a web page that prompts for user input via DropDownLists in some table cells. When a selection is made the selection replaces the DropDownList so that the action can only be performed once. In the case that a change needs to be made I want to be able to click a button that reloads the page from scratch. I have Googled and Googled but I have not managed to find a way to do this.
Any advice is appreciated.
Regards.
Put a link on the page with the text "Reload" and the url the url of the page. It's perfectly valid to have a page with a link to itself.
If you don't like the link idea, use a standard Button and in the click event, use Response.Redirect to redirect to the current page.
You can set an OnClick for your button that resets each DropDownList's SelectedIndex to 0 instead of reloading the page from scratch. Alternatively, you can set a Response.Redirect([the page's url]) into the OnClick as is suggested here.
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.
I'm trying to edit a textbox's text in a Page_Load form on an ASP.NET page, but when I try to edit TextBox1.Text it throws a NullReferenceException because the TextBox has not yet been instantiated. Specifically what I'm trying to implement is this: http://www.codeproject.com/KB/user-controls/popupcalendarcontrol.aspx but it is written for an older version of ASP.NET and does not work for me. Is there a way to instantiate the controls at the start of Page_Load? or another event that I can catch on load? With a normal windows form I would call InitializeComponent() in the constructor.
There are absolutely different events you can attach to but it sounds like the page isn't loading the controls properly because they should be available by that point. You can take a look at the ASP.NET Page Life Cycle for more information.
Does the TextBox sit within a bound control, such as a FormView, GridView, DataList, etc? If so, then the control won't exist until after the databinding happens. Once that event happens, you can do something like
DirectCast(myDataList.SelectedRow.FindControl("myTextBox"),
TextBox).Text
I know that I am a bit behind in getting to this question BUT I have found something unusual that I can NOT find documented anywhere. It would appear that IF your page is "under" a Master Page, and IF you refer to ANYTHING on said master page, the controls to your current page are null - EVERYONE of them. I found this out by referencing "Master.Environment" - a public string - during my InitializeCulture method and having the FIRST control on my current (Default.aspx) page be null during Page_Load() - WHATEVER the first control was.
I know I am late but if this can help ANYBODY, I want the word out.