Pop Up Window in VB.Net(aspx.vb) Code Behind - asp.net

I have onclick button and when i debug,i found that my onclick value is NULL.Anyone Know why it cant get value for Onclick event.
Dim HTMLForm As New StringBuilder
HTMLForm.Append("<html>")
HTMLForm.AppendLine("<body onload='document.forms[""" & "form1" & """].submit()'>")
HTMLForm.AppendLine("<form id='form1' method='POST'>" )
'Set value to collection
SetRequestField("amount", CDec(lblTtlPayAmt.Text))
'Generate HTML content using collection
For Each kvp As KeyValuePair(Of String, String) In RequestFields
If Not String.IsNullOrEmpty(kvp.Value) Then
HTMLForm.AppendLine("<input type='hidden' id='" & kvp.Key & "' name='" & kvp.Key & "' value='" & kvp.Value & "' />")
End If
Next
HTMLForm.AppendLine("<input type = 'submit' value=PayNow' onclick = '" & window.open("~/Payment/Payment.aspx") & "'/>")
HTMLForm.AppendLine("</form>")
HTMLForm.AppendLine("</body>")
HTMLForm.AppendLine("</html>")
Response.Clear()
Response.Write(HTMLForm.ToString())

You have a missing ' on the line which adds the submit button:
HTMLForm.AppendLine("<input type = 'submit' value=PayNow' ....
should be
HTMLForm.AppendLine("<input type = 'submit' value='PayNow' ....

Related

ProblemwithfindTextBoxinASP.net

i have the problem, that i create a TextBox via vb.net Code while the programm ist running.
Private Sub GenerateTBSourcePath()
'Generiert eine neue leere Textbox
Dim tBox As System.Web.UI.WebControls.TextBox = New System.Web.UI.WebControls.TextBox()
tBox.Width = 400
Dim tbID As Integer = tbSourcePathID + 1
tBox.ID = "tbSourcePath_" + tbID.ToString()
pSourcePath.Controls.Add(tBox)
End Sub
When i try to search for the TextBox on pSourcePath i cant find any TextBox.
Have anyone an idea what do i wrong?
i add the TextBox in start of my programm. I can see the TextBox also in the Browser. When i want to save the Text i cant find the TextBox.
Private Sub SaveTBox()
'Für jede TBox den Eintrag in der DB aktualisieren oder einen neuen Eintrag anlegen
For Each PControl As System.Web.UI.Control In pSourcePath.Controls
Dim Controltype As String = PControl.GetType().FullName
Select Case Controltype
Case "System.Web.UI.WebControls.TextBox"
Dim tBox As WebControls.TextBox = CType(PControl, WebControls.TextBox)
If CheckExists(tBox.ID) = True
'Update
Administration_DataSource.UpdateCommand = "Update di_source set PATH = '" & tBox.Text & "' where TBID = '" & tBox.ID & "'"
Administration_DataSource.Update()
Else
'Insert
tbSourcePathID = tbSourcePathID + 1
Administration_DataSource.InsertCommand = "Insert Into di_source(ID, PATH, TBID) values('" & tbSourcePathID & "', '" & tBox.Text & "', '" & tBox.ID & "')"
Administration_DataSource.Insert()
End If
End Select
Next
End Sub
I tried this code
int tbSourcePathID = 0;
TextBox tBox = new TextBox();
PlaceHolder pSourcePath = new PlaceHolder();
tBox.Width = 400;
int tbID = tbSourcePathID + 1;
tBox.ID = "tbSourcePath_" + tbID.ToString();
pSourcePath.Controls.Add(tBox);
foreach (System.Web.UI.Control PControl in pSourcePath.Controls)
{
string Controltype = PControl.GetType().FullName;
switch (Controltype)
{
case "System.Web.UI.WebControls.TextBox":
bool here = true;
break;
}
}
and it works. Maybe the problem is in CheckExists(). Remember also the use of FindControl()

FindControl() not getting value from input textbox

I am working on a website that has input boxes which are capable of being edited. The boxes are populated from an SQL call on PageLoad() and each text box is assigned with the ID "txtz" and then the ID number from the SQL row.
I want the user to be able to edit the text box, and upon clicking a html button have the values from the input boxes stored in a variable that can be used to Update the SQL data.
My issue is that when I call CType(FindControl it is having difficulties locating the instance of the object... I would be very grateful for any and all help!
Note When I alert the variable created for the input ID it matches what is displayed as the inputs ID on the browser... leaving me to think the FindControl function is the issue.
I have attached two images from my code in hopes that it may assist!
Table Creation in PageLoad
Procedure on Button Click
Sub UpdatePostDB(sender As Object, e As EventArgs)
Dim ButtonID As String = sender.ID
Dim ButtonID2 As String() = ButtonID.Split("z")
Dim placeholder As String = ButtonID(0)
Dim realID As String = ButtonID(2) & ButtonID(3) & ButtonID(4)
Dim titleinput As String = "txtz" & realID
Dim messgeinput As String = "txtz" & realID
Dim Titl As String = CType(FindControl(titleinput), HtmlInputText).Value
Dim Messge As String = Request.QueryString("txt2z" & realID)
ClientScript.RegisterStartupScript(Me.GetType, "titlealert", "alert('Title: " & titleinput & "');", True)
End Sub
cm1.CommandText = "SELECT * FROM ManagerPosts WHERE Department = '" & DeptDDL2.SelectedItem.Text & "' And DateAdded >= DATEADD(DAY,-14,GETDATE()) ORDER BY DateAdded ASC"
dr1 = cm1.ExecuteReader
If dr1.HasRows Then
While dr1.Read
Dim viewannouncement As New TableCell() With {.HorizontalAlign=HorizontalAlign.Center}
Dim viewannounce As New CheckBox With {.ID = dr1.Item("ID"), .AutoPostBack = True}
AddHandler viewannounce.CheckedChanged, AddressOf viewannounce_CheckedChanged
Dim updateCell As New TableCell()
Dim updatebutt As New Button With {.ID = "bz" & dr1.Item("ID"), .Text = "Update Post", .Height = 25%, .Width = 85%}
AddHandler updatebutt.Click, AddressOf UpdatePostDB
Dim tr As New TableRow
If dr1("Visible") = "N" Then
tr.Cells.Add(New TableCell With {.Text = "<div style='background-color:white'><input runat='server' id=txtz" & dr1.Item("ID") & " type='text' style='width:100%;' value='" & dr1("Title") & "'/><br /><input id=txt2z" & dr1.Item("ID") & " type='text' style='width:100%;' value='" & dr1("Message") & "'/></div"})
viewannouncement.Controls.Add(viewannounce)
tr.Cells.Add(viewannouncement)
ManageTable.Rows.Add(tr)
updateCell.CssClass = "buttonup"
updateCell.Controls.Add(updatebutt)
tr.Cells.Add(updateCell)
ManageTable.Rows.Add(tr)
Else
tr.Cells.Add(New TableCell With {.Text = "<div style='background-color:white'><input id=txtz" & dr1.Item("ID") & " type='text' style='background-color: #ff66ff; width:100%;' value='" & dr1("Title") & "'/><br /><input id=txt2z" & dr1.Item("ID") & " type='text' style='background-color: #ff66ff; width:100%;' value='" & dr1("Message") & "'/></div"})
If dr1.Item("ID") = viewannounce.ID Then
viewannounce.Checked = True
End If
viewannouncement.Controls.Add(viewannounce)
tr.Cells.Add(viewannouncement)
ManageTable.Rows.Add(tr)
updateCell.CssClass = "buttonup"
updateCell.Controls.Add(updatebutt)
tr.Cells.Add(updateCell)
ManageTable.Rows.Add(tr)
End If
End While
End If
**Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 146: Dim messgeinput As String = "txtz" & realID
Line 147:
Line 148: Dim Titl As String = CType(FindControl(titleinput), HtmlInputText).Value
Line 149: Dim Messge As String = Request.QueryString("txt2z" & realID)
Line 150:**

ASP.NET onclick event renders HTML at the top of the page

This is my first time with ASP and I'm a little messed with it. I'm coding an interface that grabs information from a database and displays it in a table. For that I'm using an asp button that takes the text from an asp textbox once I click the button in the onClick event I do the connection to the database, execute the query and print the html to the main page.
The problem is that once I click the button the resultant html is rendered at the top of the page and the textbox and the button remains below, forcing me to scroll down to make another search.
This is my onClick code if it helps:
If Not SearchBox.Text Is Nothing Then
Dim conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" &
"Data Source= C:\Users\user\Documents\Visual Studio 2012\Projects\Aplicación UNED\Aplicación UNED\App_Data\Libros.mdb")
Dim query = New OleDbCommand("SELECT * FROM Libros WHERE Titulo LIKE '%" & SearchBox.Text & "%'", conn)
Dim adapter = New OleDbDataAdapter(query)
Dim res = New DataSet()
adapter.Fill(res, "Libros")
Response.Write("<table class='data_table'>")
Response.Write("<thead><tr class='data_table_info'><th scope='col'>Titulo</th><th scope='col'>Resumen</th><th scope='col'>Categoria</th><th scope='col'>ISBN</th><th scope='col'>PrecioConIVA</th><th scope='col'>EnlaceCompra</th></tr></thead>")
For Each Fila In res.Tables("Libros").Rows
Response.Write("<tr><td>" & Fila("Titulo") & "</td><td>" & Fila("Resumen") & "</td><td>" & Fila("Categoria") & "</td><td>" & Fila("ISBN") & "</td><td>" & Fila("PrecioConIVA") & "</td><td>" & Fila("EnlaceCompra") + "</td></tr>")
Next
Response.Write("</table>")
conn.Close()
End If
End Sub
Regards.
Instead of using Response.Write, introduce a Literal control below your button and assign the generated text to the Text property of your Literal. This will ensure that your content displays below your button.
Assuming you have an ASP.NET Textbox by the name SearchBox <asp:Textbox id="SearchBox" runat="server" /> place the literal below it
<asp:Textbox id="SearchBox" runat="server" />
<br />
<asp:Literal id="ResultHtml" runat="server" />
and in your code behind, replace the Response.Write calls with
Dim sb = new StringBuilder("<table class='data_table'>")
sb.Append("<thead><tr class='data_table_info'><th scope='col'>Titulo</th><th scope='col'>Resumen</th><th scope='col'>Categoria</th><th scope='col'>ISBN</th><th scope='col'>PrecioConIVA</th><th scope='col'>EnlaceCompra</th></tr></thead>")
For Each Fila In res.Tables("Libros").Rows
sb.Append("<tr><td>" & Fila("Titulo") & "</td><td>" & Fila("Resumen") & "</td><td>" & Fila("Categoria") & "</td><td>" & Fila("ISBN") & "</td><td>" & Fila("PrecioConIVA") & "</td><td>" & Fila("EnlaceCompra") + "</td></tr>")
Next
sb.Append("</table>")
ResultHtml.Text = sb.ToString()
You are writing html to the response before the page has rendered to the response.

Disabling button after click

Private Function StoreCouponCode() As String
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString)
Dim sql As New SqlCommand("", con)
Dim sqlGetSlNo As New SqlCommand("SELECT MAX(SlNo) FROM Coupons WHERE CustID=" & Convert.ToInt16(HFCustID.Value) & " AND BusiID=" & Convert.ToInt16(HFBusiID.Value) & " AND OfferID=" & Convert.ToInt16(HFOfferID.Value), con)
Dim sNo, UserID, couponCode, offerCheck As String
Dim slNo As Int16
Try
UserID = getCurrentUserID()
con.Open()
sNo = Convert.ToString(sqlGetSlNo.ExecuteScalar) 'fteches latest serial no for the coupon
If sNo = "" Then
slNo = 0
Else
slNo = Convert.ToInt16(sNo) + 1
End If
couponCode = MakeCouponCode(slNo)
offerCheck = couponCode.Substring(13, 3)
sql.CommandText = "INSERT INTO Coupons VALUES (" & _
HFCustID.Value & "," & HFBusiID.Value & "," & HFOfferID.Value & ",'" & _
Today.Date & "'," & Convert.ToString(slNo) & "," & offerCheck & ",'" & couponCode & "','" & _
UserID & "'," & LblPayInAdv.Text & "," & LblPayLater.Text & ",'" & Convert.ToString(Date.Now) & "')"
sql.ExecuteNonQuery()
con.Close()
Catch ex As Exception
couponCode = "N/A"
Finally
con.Dispose()
sql.Dispose()
End Try
Return couponCode
End Function
Protected Sub CmdGetCoupon_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles CmdGetCoupon.Click
If User.Identity.Name = "" Then
LblMessage.Text = "You need to login first!"
Exit Sub
End If
Dim couponCode As String = StoreCouponCode()
Response.Redirect("~/Coupon.aspx?CouponCode=" & couponCode, True)
End Sub
When the user clicks on the CmdGetCoupon it takes some time to redirect..so the user clicks on the CmdGetCoupon more than once,which results in multiple coupon generation from a single user account.
I want to display a message "Please Wait While Your Coupon Is Being Generated" and disable the CmdGetCoupon so that the user cannot click multiple times....Any Ideas??How to do this??
Do this in javascript - use the OnClientClick to call a function that disables the passed in form element.
// On button
... OnClientClick="DisableMe(this);" ...
// Javascript
function DisableMe(elem)
{
elem.disabled = true;
// more code to show message
}
In your html side you have to add it in the onClientClick property:
<asp:Button ID="CmdGetCoupon" runat="server" onClientClick="this.disabled=true;" />
Edit:
Since #Oded so accurately pointed out that my first answer does not address the display of a message, here are a couple of options.
<span id="PleaseWaitContainer"></span>
<asp:Button ID="CmdGetCoupon" runat="server" onClientClick="this.disabled=true;document.getElementById('PleaseWaitContainer').innerText='Please Wait While Your Coupon Is Being Generated';" />
Or
<asp:Button ID="CmdGetCoupon" runat="server" onClientClick="this.disabled=true;alert('Please wait while your coupon is being generated...';" />
You can disable the button in the click handler, because you're going to redirect to another page, so don't care about any other logic.
Otherwise, if you're performing any async operation, again, disable it as soon as you click it, and in the async callback handler depending on the logic either enable it back or handle it properly in other way.

Knowing what input radio is selected on ASP.NET (VB)

I'm creating input radio dynamicly on a ASP.NET page using PlacHolders.
While reader.Read
Dim ltr As New Literal()
Dim ltr1 As New Literal()
Dim ltr2 As New Literal()
Dim ltr3 As New Literal()
Dim ltr4 As New Literal()
ltr.Text = reader.GetString(2) & "<br />"
PlaceHolder2.Controls.Add(ltr)
ltr1.Text = "<form> <input type = radio name=groupe" & i & " value=1>" & reader.GetString(3) & "<br />"
PlaceHolder2.Controls.Add(ltr1)
ltr2.Text = "<input type = radio name=groupe" & i & " value=1>" & reader.GetString(4) & "<br />"
PlaceHolder2.Controls.Add(ltr2)
ltr3.Text = "<input type = radio name=groupe" & i & " value=1>" & reader.GetString(5) & "<br />"
PlaceHolder2.Controls.Add(ltr3)
ltr4.Text = "<input type = radio name=groupe" & i & " value=1>" & reader.GetString(6) & "</form><br /><br />"
PlaceHolder2.Controls.Add(ltr4)
i = i + 1
End While
My problem is : how can I get all the items selected on those input radio.
I am not sure how'd you do it with your html form.
If dynamically create a RadioButton control instead of Literals, then you could check the RadioButton.Checked property to see if ti's true. Or you could use a RadioButtonList instead.
If you REALLY had to follow the implementation you have above, you could add an ASP.NET HiddenField control and use javascript to dump the value of the selected radio button into the hidden input.

Resources