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.
Related
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.
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.
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.
Hi
I am trying to convert a asp page to asp.net.
I changed everything except this one line
Set dbC = Server.CreateObject("ADOX.Catalog")
Can anyone suggest an equivalent to this ADOX.Catalog in asp.net
This dbC is being used in the following way in asp
Set cm = Server.CreateObject("ADODB.Command")
cm.CommandText = Request("qry")
sN = Request("sN")
Set tmpTable = dbC.Tables(sN)
if NOT IsObject(tmpTable) then
dbC.Views.Append sN, cm
else
tmpTble.Command = cm
end if
Although I do not have direct experience, this article seems like it would be of assistance to you:
How To Retrieve Schema Information by Using GetOleDbSchemaTable and Visual C# .NET
Try adding a reference into your project called, Microsoft ADO ext.6.0 for DLL and security or something similar.
Once you add it, you'd be able to write code like:
Dim dbc as New ADOX.Catalog
Ps: Not answering from a windows m/c to verify. Got this from another forum post
I think this has to be THE most frustrating thing I've ever done in web forms. Yet one would think it would be the easiest of all things in the world to do. That is this:
I need 2 separate lists of radiobuttons on my .aspx page. One set allows a customer to select an option. The other set does also but for a different purpose. But only one set can have a selected radiobutton.
Ok I've tried this using 2 asp.net Radiobuttonlists controls on the same page. Got around the nasty bug with GroupName (asp.net assigns the control's uniqueID which prevents the groupname from ever working because now, 2 radiobuttonlists can't have the same groupname for all their radiobuttons because each radiobuttonlist has a different uniqueID thus the bug assigns the unique ID as the name attribute when the buttons are rendered. since the name sets are different, they are not mutually exclusive). Anyway, so I created that custom RadioButtonListcontrol and fixed that groupname problem.
But when ended up happening is when I went to put 2 instances of my new custom radiobuttonlist control on my .aspx page, all was swell until I noticed that every time I checked for radiobuttonlist1.SelectedValue or radiobuttonlist2.SelectedValue (did not matter which I was checking) the value always spit back string.empty and i was not able to figure out why (see http://forums.asp.net/t/1401117.aspx).
Ok onto the third try tonight and into the break of dawn (no sleep). I tried to instead just scrap trying to use 2 custom radiobuttonlists altogether because of that string.empty issue and try to spit out 2 sets of radiobuttonlists via using 2 asp.net repeaters and a standard input HTML tag inside. Got that working. Ok but the 2 lists still are not mutually exclusive. I can select a value in the first set of radiobuttons from repeater1 and same goes for repeater2. I cannot for the life of me get the "sets" to be mutually exclusive sets of radiobuttons.
As you have two groups of radio buttons that you want to function as one group of radio buttons, the solution is simple: Make it one group of radio buttons.
The only problem you have then is that the value that you get has the same name from both lists, but that can be solved by adding a prefix to the values so that you easily identify from which list the option comes.
Update: based on the new info posted as an answer. The option I proposed on my original answer corresponds to the 3. You really must consider the following:
Html radio buttons have only 1
built-in mechanism to handle the
exclusivity, which is the name.
You are explicitly requesting a no js solution, so given the above you must manipulate the Ids to achieve it. If you weren't blocking this option I am sure someone would come up with some nice jquery or js library that already supports it.
The option 3 is clearly the less invasive, as you are not forced to affect the actual data, and are not affected by future updates to it.
It's not that much code, just something extra on the List indexes, and some simple thing as:
int? list1Value = null;
int? list2Value = null;
var value = Request.Form["somegroup"];
if (value.StartsWith("List1"))
list1Value = int.Parse(value.Substring(5));
else
list2Value = int.Parse(value.Substring(5));//Assuming List2 as prefix
Original:
I saw your other question, and you just need to use the same group name. Make sure you have different values for all items regardless of the list they come from. A way to achieve this is adding something to the values, like: <%# "List1-" + Eval("ID") %> and modifying the code that reads your Request.Form["yourgroupname"].
I think you should just use RadioButtons instead of RadioButtonLists.
Here's an article that presents a solution to resolve the radiobutton naming bug.
Though this post is dated 1 year ago already, I just read it because I face the same problem.
Currently I have 1 solution using jQuery:
Client side script (you must also include jQuery)
function SetRadio(rb) {
$('input:checked').attr('checked', false);
rb.checked = true;
}
For every radiobutton (which is a listitem in a radiobuttonlist) I add the following on the serverside:
li.Attributes.Add("onclick", "javascript:SetRadio(this)");
For me this works in both IE and Firefox, with 3 radiobuttonlists, without using groupnames.
You can check each radiobuttonlist for a selecteditem/value, or you can extend the SetRadio function so it stores the selected value in a hidden field.
Regards,
M