Setting AngularJS ng-model from .NET code behind - asp.net

I know this isn't a good approach or the Angular way of doing things. However, I have a legacy VB.NET application and I've implemented AngularJS in it for doing AJAX requests and working with data and the simplicity (I'm well versed with Angular).
My issue is ng-model overrides the value I retrieve from the server and set to the textbox. I don't really need much databinding but several directives I use 'require ng-model' which messes with the old web forms.
Here's the VB code behind setting the inputs .Text value :
'Invoiced Date - Load Initial Value Code
Try
'Dim valNumber As String = JAG.General.modFunctions.FormatFieldValue(oLoadLoadCore.LDC_Number.ToString, "int", "TextBox")
Dim valInvoicedDate As String = NASTek.Common.Lib.DB.modADO.GetDataValue("Select convert(varchar,LDC_InvoicedDate,101) from LoadCore where LDC_Key = " & Me.PrimaryKey, Me.conn).ToString
Me.txtInvoiced.Text = valInvoicedDate
Me.txtInvoiced.Attributes.Add("ng-model", valInvoicedDate.ToString())
Catch ex As System.Exception
Me.txtInvoiced.Text = ""
'Me.Context.Trace.Write("LoadLoadCore.txtInvoiced", "Unable to load initial value: " + ex.ToString() + "")
End Try
Here's the control being added the the placeholder :
Me.plhInvoiced.Controls.Add(Me.txtInvoiced)
Me.txtInvoiced.Attributes.Add("ng-model", "dateInvoiced")
Me.txtInvoiced.Attributes.Add("datepicker-popup", "MM/dd/yyyy")
Me.txtInvoiced.Attributes.Add("show-weeks", "false")
Me.txtInvoiced.Attributes.Add("show-button-bar", "true")
Me.txtInvoiced.CssClass = "FormTextBox"
Me.txtInvoiced.TextMode = TextBoxMode.SingleLine
Me.txtInvoiced.ID = "txtInvoiced"
I'm using the AngularUI datepicker directive which requires ng-model, I'm not an expert with directives at this time. I tried removing the ng-model from the 'require' line in the angularui bootstrap.js file and the popup would render without the calendar.
Does anyone know of a work around to this scenario?
Again, I know it's not elegant and the code/app are really awful and old. I hate working on it but I'm trying to make it easier to do simple CRUD operations and displaying data. If there's no work around then I'll just have to query the info and set it in the js Controller, but I didn't want to have to do that as I don't need the ng-model at this point on these particular fields. I'm mainly trying to run third party calls that render simple data on the webform and that's why I'm not needing the databinding in particular.

Related

How to access the controls of an Active Reports (Data Dynamics)

The company I work for uses the Active Reports from DataDynamics to generate their reports and they asked me if I could do a web viewer of the reports where you could move the fields around.
So the way I figured I could do this was to load the empty reports (with only the fields like they appear in the designer in VS2012) in divs and use Jquery for the moves than create the report dynamically.
The thing is, I cannot find a way to access the controls of the report. I've been googling hwo to do this for a whole day but I cannot seem to find a solution.
We are using Active Reports 6, VS2012 and vb.net.
Each Section in the report has a Controls collection that reveals the collection of controls in that section. The topic on the Sections collection has a good example of how to programatically add controls to the collection. An excerpt with some comments to help explain is below:
' Insert Group Header and Footer Sections:'
Me.Sections.InsertGroupHF()
' Set some proprties to configure those sections:
CType(Me.Sections("GroupHeader1"), GroupHeader).DataField = "CategoryID"
Me.Sections("GroupHeader1").BackColor = System.Drawing.Color.SlateBlue
Me.Sections("GroupHeader1").CanGrow = True
Me.Sections("GroupHeader1").CanShrink = True
CType(Me.Sections("GroupHeader1"), GroupHeader).RepeatStyle = RepeatStyle.OnPageIncludeNoDetail
Me.Sections("GroupHeader1").Height = 0
' Create a TexBox control & Set some properties to configure that control
Dim txt As New TextBox()
txt.DataField = "CatagoryID"
txt.Location = New System.Drawing.PointF(0.0F, 0)
txt.Width = 2.0F
txt.Height = 0.3F
txt.Style = "font-weight: bold; font-size: 16pt"
' Add the TextBox to the GroupHeader section:
Me.Sections("GroupHeader1").Controls.Add(txt)
The ActiveReports 6 documentation has a walkthrough named Run Time Layouts that builds an entire application that builds a report layout in code. That's a good way to learn exactly how to manipulate a report via code.
#activescott & #Michael, the documentation links changed, but they are still available. For the ActiveReports 6 Documentation go here, and the walkthrough for Run Time Layouts is here.

VB.Net PayPal Integration Description

I am working with a historical PayPal system in vb.net. I am struggling to add individual item descriptions or names for the products that the user is paying for. It is using the NVPSetExpressCheckout and the data is meant to display on the PayPal website when the user is about to pay. Instead however I am getting constant issues which I assume must be due to syntax or just the way I am trying to do it.
Here is the current code which works:
Dim ppSet As New NvpSetExpressCheckout()
ppSet.Add(NvpSetExpressCheckout.Request._AMT, Decimal.Parse(litTotal.Text))
ppSet.Add(NvpSetExpressCheckout.Request.CURRENCYCODE, "GBP")
Dim basePath As String = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, String.Empty) + Request.ApplicationPath
ppSet.Add(NvpSetExpressCheckout.Request._RETURNURL, basePath & "paypal.aspx")
ppSet.Add(NvpSetExpressCheckout.Request._CANCELURL, basePath & "cancel.aspx")
I have then tried to add the a description using many methods such as:
ppSet.Add(NvpSetExpressCheckout.Request.L_DESC0, "First Item")
I however am simply getting errors like these:
'L_DESC0' is not a member of 'Encore.PayPal.Nvp.NvpSetExpressCheckout.Request'.
This issue is driving me mad and I can not find a fix. All documentation including the XML says that this is the correct way. I did try to just use the DESC field which worked however all the items just got displayed as one paragraph instead of being on separate lines. Help extremely appreciated.
Found the solution. It was due to the L_AMTn request not being submitted which is basically the item amounts. Once this adds up to the total cost it submits perfectly.

Quotes in Asp.NET MVC View

In View,
#{
Layout = "~/Views/Shared/site.cshtml";
ViewBag.Title = "Organizations";
var organizations = "";
foreach (var org in Model)
{
organizations += "'" + org.Name + "':'" + org.Id + "',";
}
organizations = organizations.Substring(0, organizations.Length - 1);
}
Result operation: organizations = "'Passport':'14f0eac0-43eb-4c5f-b9fe-a09d2848db80','Bank':'ad1d77d8-7eb1-4a4c-9173-b0f2f7269644'";
Output the data in section JS code.
But when viewing the source code of the page in the browser, not getting what wanted.
What's the problem? How to make a normal quotes?
JS: "data": "#organizations"
Result in view webpage returned "data": "'Passport':'14f0eac0-43eb-4c5f-b9fe-a09d2848db80','Bank':'ad1d77d8-7eb1-4a4c-9173-b0f2f7269644'"
OK cool Q,try this source:
#{
var model = Model.ToArray().Select(org => string.Format("'{0}':'{1}'", org.Name, org.Id));
var data = string.Join(",", model);
}
#Html.Raw(data)
What if you change
"data": "#organizations"
to
"data": "#Html.Raw(organizations)"
#Html.Raw(content)
And check this one out: http://jeffreypalermo.com/blog/what-is-the-difference-in-lt-variable-gt-and-lt-variable-gt-in-asp-net-mvc/
In your code example here, the culprit is the
#organizations
that generates the quotes. You could instead use:
#Html.Raw(organizations)
Okay, that's great, but by creating JSON, you are doing work the framework could be doing for you. You probably want a model that the framework can serialize for you. This way you don't even need any code in your view header at all.
#Html.Raw(JsonConvert.SerializeObject(Model.ToDictionary(m => m.Name, m => m.Id))))
Above, note that I'm using Json.NET, which you probably want to NuGET into your project because Microsoft is moving to it. No point in figuring out how to use the old JSON serializer to do this. Also note that unless you are using the model for other purposes, you might choose to do the dictionary conversion outside the view, reducing the code-work in the view.
Next, if you are embedding JSON in the view, you might want to consider one of two other options. If the amount of data will be small, consider using the join method proposed by eli (here), but instead encoding it in HTML 5 "data-" elements and loading it using JavaScript. That way you keep javascript out of your view. It's just one more step of confusing when debugging javascript, looking for variables that are initialized by dynamically-generated HTML.
Better yet, create a reusable HTML helper method to transform your model into data attributes: How to use dashes in HTML-5 data-* attributes in ASP.NET MVC
Finally, if there are MANY JSON elements, consider sending the data separately. Render it with a simple Json() method in your controller, and get it with a simple jQuery $.json() in your client-side code. See here for an example
Good luck!

syntax for dynamic controls? in asp.net and vb.net

Welp, I'm frustrated now.. partly because I'm new to asp and this seems so simple and I cant get it to work but have in vb several times.
I've volunteered for a simple webform survey for customers and I've stuck on creating dynamic controls/ lables for the questions.. The number of questions will vary so I'd hate to change this webform everytime.
I'm stumbling over lines 6, 8 & 10: QuestionLabel1.Text = SQLRead.GetString(0)
I'd rather do something like this:
QuestionLabel(cntr).Text = SQLRead.GetString(0)
or
("QuestionLabel" & cntr).Text = SQLRead.GetString(0)
or even
Me.Controls.Add("QuestionLabel" & cntr)
For cntr = 1 To 5
Dim QuestionLabel As New Label()
Dim Panel1 As New Panel()
Me.Controls.Add(QuestionLabel)
QuestionLabel.ID = "QuestionBox" & cntr
QuestionLabel1.Text = SQLRead.GetString(0)
SQLRead.Read()
QuestionLabel2.Text = SQLRead.GetString(0)
SQLRead.Read()
QuestionLabel3.Text = SQLRead.GetString(0)
I know that the syntax is wrong but I'm hoping you guys can look it over, get a sense of what I'm trying to accomplish and tell me if asp.net will allow this and if so what I'm missing.
THANKS!
Assuming that you're utilizing .ascx for your client scripting and vb.net for your code-behind. You should consider using a Repeater control. It's designed specifically for things like this (adding controls dynamically). This architecture would allow you to
use the knowledge you already have to create your SQL statements, and combine it with a
relatively simple ASP approach.

Get property value of control in a dynamically created usercontrol

I'm having trouble recovering a reference to a user control that is created dynamically so I can get the values of its properties or child controls.
I have a custom control called BookingObject.ascx which resides in App_Code.
I can create the controls programmatically without a problem but, later on, when I try to get a reference to the control, in order to access its child controls values, I just keep getting null.
Heres some code:
Dim MainItem As BookingItem = TryCast(LoadControl(GetType(BookingItem), Nothing), BookingItem)
MainItem.ID = "Item_" & NumberOfControls.ToString() 'Item_0
Dim lblid As Label = MainItem.FindControl("lblID")
lblid.Text = (NumberOfControls + 1).ToString()
NewItemPH.Controls.Add(MainItem)
Me.NumberOfControls += 1
This works fine and I can set the labels value successfully.
However:
Dim MainItem As BookingItem = TryCast(Me.Page.FindControl("Item_0"), BookingItem)
Dim product_id As DropDownList = MainItem.FindControl("product_id")
I get a null reference exception when trying to FindControl("product_id") as MainItem is nothing.
I've been struggling with this issue for hours now and it's something I assumed would be simple. I'm sure i'm just missing something small.
Can anyone tell me what I'm doing wrong?
All help is appreciated.
UPDATE
Never mind, I was being stupid and searching Me.Page for the control when I should have been searching NewItemPH.
Thanks to the mystery person who gave me the clue and then deleted their comment minutes later.

Resources