Find generated HTML Control and assign value to it - html

Hello i generate HTML input text controls with a string builder and put it inside a div from Code Behind.
Then i also need to assign values to these generated input & update database if values change by the user.
The problem is Code Behind can't find the generated from String builder HTML Input-text Controls
You can see code example below:
Public Class WebForm1
Inherits System.Web.UI.Page
Private Sub WebForm1_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
Dim strB As New StringBuilder
For i = 0 To 5
strB.Append("<input type=""text"" value="""" runat=""server"" id=position_" & i & "/>")
Next
wraper.InnerHtml = strB.ToString
strB.Clear()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For i = 0 To 5
Dim a As HtmlInputText
a = Me.Page.FindControl("position_" & i)
a.Value = "test"
Next
End Sub
End Class

That's because you're not creating controls, you're creating strings without a context.
You might want to look at these questions:
ASP: runat=server for dynamic control
Dynamically Created Controls losing data after postback

Related

I am being asked to add functions to my code and need help doing so

I designed code for a GUI grade Calculator but now I am being asked to add 1. create a function that will contain all of your data validation code. The function should return true if all data checks have passed or a false if any of the data is invalid. If the data is valid (true returned), add the number to the list box, if it is not valid (false returned), show an error message.2)Add a Sub procedure that will reset the form fields back to their initial state. Call this sub procedure when the clear button is clicked.
2.add a Sub procedure that will reset the form fields back to their initial state. Call this sub procedure when the clear button is clicked.
create a function that will count the 90s in the listbox. Pass the listbox in a a parameter and loop the contents to count each of the 90s in the listbox. Return the count of the 90s in the list box to the caller. Use it as input in the re-write eligibility check.
This is my Code.
Public Class Form1
Private Sub BuGrade_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuGrade.Click
gradeList.Items.Add(txtgrade.Text)
txtgrade.Clear()
End Sub
Private Sub BuAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuAverage.Click
Dim total As Double
Dim avg As Double
Dim grade As Double
Dim gradecount As Integer
For gradecount = 0 To gradeList.Items.Count - 1
grade = gradeList.Items(gradecount)
total += grade
Next
avg = total / gradeList.Items.Count
lblR.Text = "The final total score is " & total & vbCrLf & "The Average score is " & avg
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
lblR.Text = String.Empty
End Sub
End Class

vb.net - grabbing the value ot a text input box displayed in a webview and bringing it back into winforms

I've got a winform that contains a webview.
At some points the webview will display html content including input boxes for the end user to throw some responses into.
Is it possible for me to interact with the input box in the webview to grab its contents programatically?
Even if its just to display that text in a messagebox as an example, that will get me well on my way!
so in the example form below, I'd be looking to grab whatever the end user entered into 'userresponse' and throw it into a variable. like...
dim response as string = 'the contents of the input box in the webview
messagebox.show(response)
any help appreciated and thanks in advance! :)
<HTML>
<Body>
<form>
<input type="text" id="userresponse" name="userresponse"><br>
</form>
</Body>
</HTML>
You can use InvokeScriptAsync() to to achieve it.
Sample code:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim html As String = File.ReadAllText("test1.html")
WebView1.NavigateToString(html)
End Sub
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ss As String = Await WebView1.InvokeScriptAsync("eval", New String() {"document.getElementById('userresponse').value;"})
MsgBox(ss)
End Sub
End Class
Result:

pass data to multiple textbox with 1 form vb.net

I have issue with passing data in VB.NET and I'm new in vb.net + mysql. I don't know is it possible or not. I have some TextBoxes and I want fill those with userID or something. I want to make this like a modal on php.
When I clik TextBox, 1 form shows up with ComboBox and submit button to pass data to TextBox on previous Form. I've done it with 1 TextBox, but I can't pass it to other TextBox. Here is my code:
Form1.vb
Private Sub TextBox4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.Click
'This is method **in my logic** can throw back data to TextBox4 from Form2.vb'
Form2.Label1.Text = TextBox4.Name
Form2.Show()
End Sub
Form2.vb
Private Sub AddTeam_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call getUSers()
End Sub
Sub getUSers()
Call connection()
Dim str As String
str = "SELECT id FROM users WHERE status='Active'"
cmd = New MySqlCommand(str, conn)
rd = cmd.ExecuteReader
If rd.HasRows Then
Do While rd.Read
ComboBox1.Items.Add(rd("id"))
Loop
Else
End If
ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim nik = ComboBox1.SelectedItem
'Here is the issue, if i pass to 1 TextBox (like TextBox4 as sample) it`s easy, but if pass to another TextBox i have no idea to do that'
Form1.TextBox4.Text = nik
Me.Refresh()
Me.Close()
End Sub
note: I know there are many ways to do so, like change TextBox to ComboBox and fill with data, but I want this method
I'm sorry for my poor logic in vb.net. I was better with PHP before.

Linq to SQL - SubmitChanges not working

So - I have a MySQL db with one employee table, using Devart Linq to SQL (for MySQL)
It has a primary key in the DataContext,
I can load the gridview,
select a row,
set an instance of Staff = to the ID and Name or the row selected in the gridview
set a Textbox = to Name of Staff
change the textbox and set the object Staff.Name to the textbox.....
but I cant submit the change back to the db!!!! Probably a stupid thing I'm doing as a newbie.
Public Class Form1
Dim db As New MydbContext.MydbDataContext
Dim bs = New BindingSource
Dim Staff As New MydbContext.Employee
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' load employee data from MySQL db in datagrid
Dim StaffQuery = From s In db.Employees Select s
bs.DataSource = StaffQuery
DataGridView1.DataSource = bs
' this works fine
End Sub
Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
'create an instance of the object Staff with the ID and Name from the datagrid
'is there a better way to do this?
Staff.EmployeeID = DataGridView1.Rows(e.RowIndex).Cells("EmployeeID").Value
Staff.Name = DataGridView1.Rows(e.RowIndex).Cells("Name").Value
MsgBox(Staff.EmployeeID & " " & Staff.Name)
TextBox1.Text = Staff.Name
'this works fine
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Staff.Name = TextBox1.Text 'edit the Name
MsgBox(Staff.EmployeeID & " " & Staff.Name) ' this works - shows new Staff.Name from textbox
db.SubmitChanges() ' this fails to make db changes
End Sub
End Class
I believe you need to add db.Staffs.InsertOnSubmit(Staff)
Where Staffs is the name of the collection of Staff objects.
However, rather than keep the context and object around all the time, why not just create the new Staff object when the user presses the save button?
Finally, you can call GetChangeSet() on your db context to see what changes are pending. This can help you understand when something strange happens.

how to parse html contents returned as a response from a webserver and show a specific tag value in a combobox in desktop application in vb.net

i am trying to fetch some data from Url using Httpwebrequest/response, i am getting response which i am showing in a msgbox. It show whole HTML contents.
Now my i want to fetch a specific tag(TD tag) value and show all its value in a combobox in vb.net desktop application.
my code to get response from webserver is :
enter code here
Imports System.IO
Imports System.Net
Imports System.Xml
Imports System.Text.Encoder
Public Class login
Private Sub login_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End
End Sub
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
End Sub
Private Sub Ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ok.Click
Dim strId As String = txt_uid.Text
Dim strPwd As String = txt_pwd.Text
Dim oEncoder As New System.Text.ASCIIEncoding
Dim postData As String = "UM_username=" + strId
postData += ("&UM_password=" + strPwd)
Dim data As Byte() = oEncoder.GetBytes(postData)
MsgBox(postData)
Dim webStream As Stream
Dim webResponse As String = ""
Dim req As HttpWebRequest
Dim res As HttpWebResponse
Dim Output As String
'Dim Posit1 As Int32
'Dim Posit2 As Int32
req = WebRequest.Create("http://localhost/basic_framework/index.php?menu=login&UM_email=" & strId & "&UM_password=" & strPwd)
req.ContentType = "application/x-www-form-urlencoded"
req.KeepAlive = False
req.Method = "POST"
res = req.GetResponse()
webStream = res.GetResponseStream()
Dim webStreamReader As New StreamReader(webStream)
While webStreamReader.Peek >= 0
Output = webStreamReader.ReadToEnd()
RichTextBox1.Text = Output
Msgbox(Output)
End While
End Sub
End Class
this code get the response from url and show it in a richtextbox or msgbox
Now i want to get a specific tag value (say, td, option values) and show it in a combobox in my vb.net application form dynamically.It would be needed to parse html content then get that tag value . Please suggest me a way......
If parsing needed , how to parse html contents to get only specific tag value in a combobox in vb.net form
Well...if you can be sure that your request returns valid XHTML (which is XML indeed), you might be able to use an XPath expression.
For the most complicated cases (e.g., an AJAX web-site, etc.) you could use HTMLUnit library with iKVM.