set object values from form - asp.net

I have the following code that sets certain object(CarsCourse) values according to what the user selected in a web form.
The code works, but an associate of mine stated that this in the worst possible way of doing this. However, he couldn't offer any suggestions.
So is there a better way of accomplishing this?
Thanks
If Not String.IsNullOrEmpty(tbDisplayName.Text) Then CarsCourse.DisplayName = tbDisplayName.Text
If Not String.IsNullOrEmpty(tbDescription.Text) Then CarsCourse.Description = tbDescription.Text
If Not String.IsNullOrEmpty(tbOfficialStartDate.Text) Then CarsCourse.OfficialStartDate = DateTime.Parse(tbOfficialStartDate.Text)
If Not String.IsNullOrEmpty(tbOfficialEndDate.Text) Then CarsCourse.OfficialEndDate = DateTime.Parse(tbOfficialEndDate.Text)
If Not String.IsNullOrEmpty(tbBtmDatepicker1.Text) Then CarsCourse.VisibleStartDate = DateTime.Parse(tbBtmDatepicker1.Text)
If Not String.IsNullOrEmpty(tbBtmDatepicker2.Text) Then CarsCourse.VisibleEndDate = DateTime.Parse(tbBtmDatepicker2.Text)
If Not String.IsNullOrEmpty(ddlDepartment.SelectedValue) Then CarsCourse.SecondarySpecialtyName = ddlDepartment.SelectedValue
If Not String.IsNullOrEmpty(ddlOptionType.SelectedValue) Then CarsCourse.OptionType = ddlOptionType.SelectedValue
If Not String.IsNullOrEmpty(ddlOfficialName.SelectedValue) Then CarsCourse.OfficialCourseID = Guid.Parse((ddlOfficialName.SelectedValue))

I think your code is not very well designed. The way of achieving this properly would be with state and validation.
So primary you have a form, which holds the current values of an object. I'd recommend to bind the corresponding object directly to the web form. Regardless of being null or empty the values are actually set.
As soon as the user presses the OK button, a Save() or Validate() method is executed on the corresponding object and the values are checked for their validity. Cancel the save process if validation fails and tell the user to update his input accordingly.

Related

VS windows forms

I was just wondering is there a less bloating and simpler way of clearing input elements say made up of text boxes, check boxes and combo boxes to a empty state, that being removing text from them etc. I have like maybe 50+ and was hoping of a tidier way of doing this.
Another scenario I have is a set of 18+ elements, each set having 2 text boxes and 2 check boxes, one being an 'enabled' check box that when selected will enable/disable its set. Would there be a way of creating a method to handle this and make the required changes rather than having to type out the same thing happening for each set?
if (cbEnhancedInputOptionEnabled1.Checked == true)
{
tbEnhancedInputOptionText1.Enabled = true;
tbEnhancedInputOptionInputID1.Enabled = true;
cbEnhancedInputOptionDefaultState1.Enabled = true;
}
else
{
tbEnhancedInputOptionText1.Enabled = false;
tbEnhancedInputOptionInputID1.Enabled = false;
cbEnhancedInputOptionDefaultState1.Enabled = false;
}
so I would have 18 of these just changing based on the number which correlates to the set. Each one being in their own cbEnhancedOptionEnabled_CheckedChanged event.
Is there a way of passing controls by reference to a method? that'll do the same as above but to the controls passed?
For the first scenario, you can create a method named, for example, ResetAllControls, where you can set the status of each control to its default state.
For the second scenario, you may implement CheckedChanged event for each checkbox to control what to be enabled/disabled.
Another option is to give prefixed name for each control in each set. For example, set1_txtBox, set2_ddl, etc. Then define a common method. The method should accept a parameter (based on the prefix or set order number) to decide which set that you want to enable/disable. In that method, you can loop through the list of controls of the Form and, based on the parameter, and enable/disable which controls that you wish. Finally, you can implement the CheckedChanged event for each checkbox that is responsible for enabling/disabling each set's status. In each CheckedChanged method, you call the common method and pass your parameter.
Let me know if these help you.

How to get the clicked choice in the closing form dialog?

When I close the Form appear this DialogBox
I need to get the action clicked in this dialog (for example Yes or No etc...) in Form's method canClose
In debug the last point is in:
\Forms\MyForm\Methods\canClose
ret = super();
How I can get the clicked choice?
Thanks in advice.
You cannot get the answer from the prompt.
The return value of super is true, if the user can leave the form.
You do not describe what you want to achieve, but "No" is the answer to your question.
The prompt comes when a value in the record is change and the users press the Esc key. If the user selects Yes, the write method is called. So you may set a flag canClose and then test it in write. But I honestly not see the reason why this would be useful.
Check some lookup and dialog forms.
They uses closeOk, closedOk, closeCancel and closedCancel methods.
There are also closeSelect and closeSelectRecord methods to assign selected record (check also selectMode method on the form).

Update Gridx with JsonStore, on event?

I am new to Web UI, Dojo, Java etc. If you are referring any advance topic please give some reading reference. It will help.
Context:
I have Gridx design using JsonStore, which takes a target + id for URL. With fixed "id" Grid loads well.
I have Dynamic Tree. It is working fine with lazy-loading etc.
Objective:
Based on click (or dblclick) event for a given node in Tree, I have to load Gridx with data. Hence, if tree node "id":7 is clicked, then JsonStore: /target/7 should be fetched and get loaded in Gridx.
Issues:
As you can guess, at start there is no valid "id" property to fill in JsonStore. In click event handler of tree, I will get this value, upon a user click. Hence, can't call gridx.startup() in "ready". Though I have "placed" the widget in "ready".
Hence, I have following snippet to handle,
<
// this function is called from tree event handler
function LatestTabGridxLoad( id ) {
console.log( "ID %s fetched.", id );
LatestTabGridxStore.idProperty = id;
LatestTabGridx.startup();
LatestTabGridx.body.refresh();
}
ready(function()
{
TabLatestAssetTree.startup();
LatestTabGridx.placeAt( "ReportMiddleTabLatestCenterContainer" );
}
Now, trouble is, at first time loading, JsonStore GET fired with /target/ alone, without any "id" without any user click. Hence, server responds with 405. Subsequently, when user clicks, again same HTTP GET without "id" results in HTTP 405. I am not able to somehow feed "id" to the GET URL. I have checked JSON, it is in perfect shape, as it works in standalone table, that is declarative(ly) defined.
Please suggest me ways to link a TREE node through its "id" to Gridx. Also suggest, if approach I am taking is right way to solve this problem.
Sorry, misunderstanding of the question. I thought it was about gridx tree but it is about regular gridx controlled by another widget.
The problem is in this line:
console.log( "ID %s fetched.", id );
LatestTabGridxStore.idProperty = id;
'idProperty' is the name of the JSON attribute which stores the ID. This will not change with each selection. If it is not set, JsonStore will assume it to be 'id'. What you intended to do was to modify the target property of the store to include the ID. This can done directly and will look something like the following (details are application specific)
LatestTabGridxStore.target = (someURL) + '/' + id;
After that, the content of gridx needs to be replaced using the new store setting. There are few ways to do it, the simplest being destroying the current gridx instance and replacing it with another one which uses the altered store.

Why does $find(strSomeOtherRadAjaxPanel) return null?

Problem context:
1) rcbComboBoxInRadPanel is a Telerik RadComboBox.
2) rcbComboBoxInRadPanel has "OnClientSelectedIndexChange" event which fires "itemSelected."
3) rcbComboBoxInRadPanel is contained a radAjaxPanel called "foo."
4) strSomeOtherRadAjaxPanel names a RadAjaxPanel that exists outside of "foo."
5) $find(strSomeOtherRadAjaxPanel) returns a valid RadAjaxPanel if executed alone.
function itemSelected(rcbComboBoxInRadPanel)
{
var strComboBoxInRadPanel = rcbComboBoxInRadPanel.get_id();
var intRecordID = rcbComboBoxInRadPanel.get_value();
$find(strSomeOtherRadAjaxPanel).ajaxRequest(intRecordID);
}
It appears that $find(strSomeOtherRadAjaxPanel) always returns null when called from the "OnClientSelectedIndexChange" event of rcbComboBoxInRadPanel.
Is there another way for me obtain a valid reference to the RadAjaxPanel using $find(strSomeOtherRadAjaxPanel)?
Any help you can offer would be helpful.
Can you obtain reference to the strSomeOtherAjaxPanel from other client handler of the combo or when it is moved outside of its ajax panel holder? If the strSomeOtherAjaxPanel is initialized properly on the client, it should be available in each of these cases.
check your rendered html source code. ASP dynamically generates clientID names so your server side IDs may not have persisted. They probably now look something like ctl100aFMLksdjflFML
Either target them some other way (like class name, jQuery search, etc) or set the client ID mode to static (if it is the only instance of this object) then try again.

Why is my ApplicationCache passing back a reference instead of a value?

This is an odd thing I've just run into.
I have a web application with a small DataTable stored in the ApplicationCache to reduce the amount of queries to a separate since the data is a lookup table that doesn't change often.
I access this DataTable twice within a given page. Once to bind the data to a drop down list in my Page_Load method:
dtDeptDivAct = GetAllDeptDivActCodes()
dtDeptDivAct.DefaultView.Sort = "LongDescription ASC"
ddlDeptDivAccount.DataSource = dtDeptDivAct.DefaultView
ddlDeptDivAccount.DataTextField = "LongDescription"
ddlDeptDivAccount.DataValueField = "Id"
ddlDeptDivAccount.DataBind()
...and once to retrieve additional data from the table when an index is selected in my ddlDeptDivAct_SelectedIndexChanged event:
Dim dtDeptDivAct As DeptDivActDataTable
If ddlDeptDivAccount.SelectedIndex > 0 Then
dtDeptDivAct = GetAllDeptDivActCodes()
dtDeptDivAct.DefaultView.RowFilter = "Id = " & ddlDeptDivAccount.SelectedValue
txtAddFundingDept.Text = DirectCast(dtDeptDivAct.DefaultView(0).Row, DeptDivActRow).Department.ToString.PadLeft(2, Char.Parse("0"))
txtAddFundingDiv.Text = DirectCast(dtDeptDivAct.DefaultView(0).Row, DeptDivActRow).Division.ToString.PadLeft(2, Char.Parse("0"))
txtAddFundingAct.Text = DirectCast(dtDeptDivAct.DefaultView(0).Row, DeptDivActRow).Activity.ToString.PadLeft(3, Char.Parse("0"))
Else
txtAddFundingDept.Text = ""
txtAddFundingDiv.Text = ""
txtAddFundingAct.Text = ""
End If
Note: The GetAllDeptDivActCodes() method is a simple method that returns the table from the ApplicationCache object.
The web page works fine. I can select my value and the proper values are insterted into the TextBox. However, when I go to a different page and come back to this page. My drop down list only has 1 value available for selection.
When I pulled up the debugger, I noticed that upon returning to the web page, when the GetAllDeptDivActCodes method returns the DataTable from the cache, the DefaultView RowFilter property was still applied to the DataTable, which was causing the problem.
I have fixed the issue for now by simply resetting the the DefaultView RowFilter once processing is done in the SelectedIndexChanged event, but why is the Application returning what appears to be a reference to the DataTable in the application cache when I was expecting a seperate copy (or value) of the object?
This is by design. Whenever you store an object in the Application State or Session State you are returned the actual object (or as you put it a reference to the object) when you access it. By design .NET objects are almost always passed by reference unless you specify otherwise. Forexample when passing objects to Functions they are passed by reference.

Resources