I have a bit of a problem I was wondering if you could help me.
I have the following little bit of code:
protected void btnSubmit_Click(object sender, EventArgs e)
{
Page.Validate("RadMaterial");
Page.Validate("TopX");
int max = 0;
if (int.TryParse(txtbxHowMany.Text, out max))
{
GridView1.DataSource = this.GetMaterialData("123456",radTopx.SelectedItem.Value, "Primary", max);
GridView1.DataBind();
}
}
I have a couple of validation groups set up the first - if the click is made and the txtbxHowMany is not populated, a simple error is shown.
I also set up a validation group for the radiobutton list so that, should the user hit submit without checking a radiobutton, the required field validation should fire.
However, it is not firing. I am getting a "NullReferenceException was handled by user code."
My thinking is that because the radTopx.SelectedItem.Value is, well, null.
How would I go about getting around this little issue of mine? Again, apologies for what is most likely a ridiculously easy question.
Interesting the way you used Validate method.
See what msdn says about it.
http://msdn.microsoft.com/en-us/library/dwzxc386.aspx
Related
In my application, I have a form that users fill out, then gets approved by a manager. I have various types of forms that all use the same process, so the approval buttons are all done via a user control (which includes the functionality to update the data in the database and call the postback).
However, once I click on the "Approve" button (which is in the user control), the form information doesn't update (it still says "unapproved"). A postback is definitely happening, but not sure why the page isn't updating properly.
I can confirm that the change are being made - when I manually reload the page, it gets updated - but not on the post back.
What am I missing here?
My page:
protected void Page_Load(object sender, EventArgs e)
{
int ID;
// ensure that there's an ID set in the query string
if (Int32.TryParse(Request.QueryString["ID"], out ID))
PopulatePage(ID);
else
Response.Redirect("~/Default.aspx");
}
}
protected void PopulatePage(int ID)
{
using (WOLinqClassesDataContext db = new WOLinqClassesDataContext())
{
lblStatus.Text = wo.Workorder.status;
....
}
}
I think that the Page_Load happens before the code in the submit button. To check this just use a couple of breakpoints. So the page loads the old data since the new data are not saved yet.
You should call a method to load the data inside the OnClick method of the Approve button.
After you've submitted the changes to the database, try running db.Refresh(RefreshMode.OverwriteCurrentValues) to force the changes to be reloaded into the data context.
I have a gridview that is bound to a entitydatasource.I've creaetd this using drag and drop from the asp.net controls in the toolbox, and using an entity data model.I have had little input in the codebehind. For testing purposes I have edited the gridview and added data that is invalid. I've then clicked update to cause an exception.
So my question is I would like to try and catch the exception in my own error handler but I don't know where or how I can do this as I'm not sure which event I should be focusing on. I would just like to know where to begin with this.
Many thanks
You can trap the exception in the OnUpdated event of the EntityDataSource:
protected void EntityDataSource1_OnUpdated(object sender, EntityDataSourceChangedEventArgs e)
{
if (e.Exception != null)
{
// handle here
e.ExceptionHandled = true;
}
}
}
you won't able to given the form was designed using drag'n drop, declarative syntax. You are better off validating the user input before submitting it to the server. This should catch most exceptions.
your other option is to replace the declarative markup with code in the code behind where you can catch exceptions or call validation prior to calling SaveChanges();
you can catch the exception in global.asax but it will show generic error.
I'm trying to make a usercontrol work like a plugin: load it dynamically (using reflection) from a user's selection. After I click the button, I can see that the UI had adjusted to supposedly indicate that the user control has been loaded but I cannot the control itself. I even used viewstate but still I cannot see the control.
Please find my code below:
protected void Page_Load(object sender, EventArgs e)
{
//the scenario should be: a user clicking a button and from there,
//loads the control just below it, but still in the same page.
if (Page.IsPostBack)
LoadUserControl();
//(a)I also tried to put in a ViewState but still nothing happens.
//if (ViewState["UserControl"] != null)
//{
// UserControl uc = (UserControl)ViewState["UserControl"];
// pnlReportControl.Controls.Add(LoadControl());
//}
}
//supposedly done after a button click
private void LoadUserControl()
{
enrolmentReports = string.Concat(Server.MapPath("~"), enrolmentDll);
assembly = Assembly.LoadFrom(enrolmentReports);
Type type = assembly.GetType("TZEnrollmentReports.EnrollmentUserControl");
enrolmentMethodInfos = type.GetMethods();
//also tried this way but also didn't work
//Page.LoadControl(type, null);
UserControl uc1 = (UserControl)LoadControl(type, null);
pnlReportControl.Controls.Add(uc1);
//(a)
//ViewState["UserControl"] = uc1;
}
Please help. This is just the first step of the whole complicated process. I still have to get a dataset from that report. But I think I'm leaving that to another thread.
Thank you!
I believe that this is by design with the LoadControl(Type, Object) that it doesn't return what you are expecting.
If you change it to use LoadControl("PathOfControl") then this should work.
See this SO Q&A for more info Dynamically Loading a UserControl with LoadControl Method (Type, object[])
A suggestion that could help you solve this issue, is to change a little the approach. Usually developing a pluggable system, you base the pluggability to some interfaces. In your case, I would create an interface IPlugin that defines a method like CreateUI and some other to retrieve the data managed by the custom control internally, in some generic form.
This way, you'll delegate to the plugin implementation (your custom control) the responsability to create the UserControl properly and to return it to the caller (your page).
Once loaded the plugin implementation via reflection (something like this):
Assembly pluginDLL = Assembly.Load(System.IO.File.ReadAllBytes(fullPath));
Type pluginType = pluginDLL.GetType(step.PluginClass);
IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType);
then you can load the Control on your page:
pnlReportControl.Controls.Add(plugin.CreateUI());
try replacing following code in your page_load method
if (Page.IsPostBack) LoadUserControl(); with if (!Page.IsPostBack)
LoadUserControl();
I have a problem, I have to stop one Loading class on button click. I already checked some forums related to this. But didn't find an exact solution.
For example:
Public Sub LoadDropDown()
Dim it As Integer
For it = 0 To 1000000
DropDownList1.Items.Add(it)
Next
End Sub
I have to load the DropDown on Load button click and I have to cancel that on cancel button click.
Since populating the control happens on the server, I can't imagine way to interrupt your method from the client. The whole control is being populated, rendered, and only then sent to the client. You might interrupt the callback using ajax, but then the control wouldn't be returned at all.
An alternative could be to load the contents in chunks with ajax and append them to the control on the client-side.
There is no formal way to do what you're asking, but you should be able to achieve the same result if you refactor your code. If certain content shouldn't be loaded for certain users, do that logic in your code behind before it renders to the page.
Per your response to the other answers...
You could consider doing multiple my_ddl.items.add() calls on a timer. Would involve multiple, separate postbacks / ajax calls. For example:
1) add records for 2 seconds (instead of a fixed number of records at a time)
2) check for session("continue") = "true"
3) add more records for 2 more seconds
4) check session("continue")
...
At some point, user clicks cancel, which assigns "false" to session("continue"). Next time your loop checks session("continue"), it will see that it's false and exit.
This would give you a partially-loaded data control. You might want other code to wipe-out the partial update.
I think you could accomplish this with a Session Variable. Forgive me, but I'll have to provide the example in C#, but I'm sure you can get the general idea of this.
private bool CancelRequested
{
get
{
if (Session["CancelRequested"] == null)
return false;
else
return (bool)Session["CancelRequested"];
}
set
{
Session["CancelRequested"] = value;
}
}
public void LoadDropDown()
{
for (int it = 0; it <= 1000000; it++)
{
if (CancelRequested)
{
CancelRequested = false;
break;
}
//Your logic here
}
}
protected void btnCancelRequest_Click(object sender, EventArgs e)
{
CancelRequested = true;
}
The idea here is that the inital loop checks a Session variable to see if it should continue or break out of the loop. If you have a button on the page that will allow the user to set this Session variable to "true", they can essential communicate to the inital request and cause it to break out of the loop. I'm not sure if this would fully accomplish what you're looking to achieve, but hopefully it helps.
I have implement GridView Row Editing feature in my .net application using <asp:CommandField.
I clicked on Update button to save the record after editing the row.Now if i refresh the page or press F5 GridView_RowCommand fired again.
How can we avoid this.Is there any mechanism to identify when user press F5 OR refresh the page.Is there any method in client side or in server side.
Not exactly the best "technical" solution to your problem but you could always just do a Response.Redirect(Request.RawUrl) once you have finished doing anything you need to do in your RowCommand
Like I said, it's not the best "technical" solution but it is a solution
Dave
One method of capturing this is to maintain a session variable that is related to the page in question. In the session variable you would keep some kind of state enumeration, key or string that would determine the last action taken. You could even use a simple incremented counter, and if you ever received a postedback counter that was equal to the session variable it would indicate a page refresh rather than a new action.
Session["LastInsertedItem"] = null;
MyCustomObjType myCustomObject;
protected void Page_Load(object sender, EventArgs e)
{
myCustomObject = Session["LastInsertedItem"] as MyCustomObjType;
}
void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
If(myCustomObject == null || //!(compare your value with myCustomObject.field) )
{
// do your operations and save the values to myCustomObject and save that object back to Session.
}
else
{
// It is refreshed or same data is being insterted - don't know if second option is possible in your case.
}
}