I have read through many of the questions related to my situation but I have not found one that does. I currently have a repeater with 4 bound items to it, clicking on a button in the repeater causes my error, below is the markup and codebehind. Can someone explain to me why this is happening and the fix for it? (I have excluded the page decleration and just included the page_load event. let me know if i should cut and past full code in)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="rptFoo" runat="server">
<ItemTemplate>
<asp:Button ID="btnfoo" runat="server" />
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim items As New List(Of String)
items.Add("test1")
items.Add("test2")
items.Add("test3")
items.Add("test4")
rptFoo.DataSource = items
rptFoo.DataBind()
End Sub
"Invalid postback or callback argument" with Databound controls
answers my question incase anyone else needs this
That happens because the Repeater is getting datasource (binding) as you perform the item command operation. Just add if(not ispostback) to the page_load method.
I will repeat the answer here so you don't need to link to
The problem is loading the data for the control in the page Load event and calling the DataBind() method. However it appears that if the DataBind() method is called before the events are raised the above exception is generated as the control naming has changed.
The solution is to change this to if(!IsPostback) DataBind() and then call the DataBind() method at the end of the event handler. You would need to call it most of the time anyway at the end of the handler to affect the changes.
If this is not your problem, and you are modifying controls client side using JavaScript, check out this article.
Related
A question from a beginner without English as mother language.
I have a Login.aspx file, populated with one asp:HyperLink and one asp:Login. Inside the login control there are labels, textboxes, and validation procedures inside a layout template.
Inside the designer file, however, there are only hyperlink and login controls. The textbox UserName is absent as all other controls inside the Login control.
I want to populate UserName with a session variable at page load, i.e. UserName.text=Session("Fng").ToString but got error
'UserName' is not declared.
How to work around?
You could try to use Login.UserName property to access the textbox UserName.
I designed the following html.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Login runat="server" ID="log">
</asp:Login>
<asp:button id="btnshowusername" Text="showusername" runat="server" OnClick="btnshowusername_Click"></asp:button>
</div>
</form>
</body>
</html>
VB.NET code:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Session("UserName") = "user1"
End Sub
Protected Sub btnshowusername_Click(sender As Object, e As EventArgs)
log.UserName = Session("UserName")
End Sub
End Class
Tested result: When you click the button, the Session value is assigned to the textbox UserName.
I have seen various examples that use a onClientClick to avoid postback.
However, I don't want this. I want to contain the linkbutton's links within vb.net code and prevent the page from performing a postback when clicked. The reason for this is when my page loads it sets the default show() and hide() div displays again which loses the user's current spot on the page when the new window opens. So how can I do this without causing the main page from resetting?
I have tried OnClientClick="javascript:return false;" but prevented user from clicking linkbutton.
I have also tried AutoPostBack = "false" and it didn't work
Attempted other ways as well. ... no luck... including trying HyperLink but those gave me issues too.
<asp:linkbutton ID="aCapShip" runat="server" OnClick="aCapShip_Click" style="text-decoration:none;" >
<i class="fa glyphicon glyphicon-plane fa-lg"></i> Cap Shipping <!--<span class="arrow"></span>-->
</asp:linkbutton>
JavaScript
$(document).ready(function () {
$("#shipping").show();
$("#Ul2List").hide();
$("#manufacture").hide();
$("#Ul1List").hide();
$("#reList").hide();
.........
VB.net
Protected Sub aCapShip_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim url As String = "http://dummyinfo/dummyinfo/dummyinfo.aspx"
Process.Start(url)
End Sub
So my work around is the following:
VB.net
If Not Page.IsPostBack Then
startUp()
End If
Public Sub startUp()
Response.Write("<SCRIPT LANGUAGE=""JavaScript"">startSort()<" & "/" & "SCRIPT>")
End Sub
JavaScript
$(function startSort() {
$("#shipping").show();
$("#Ul2List").hide();
$("#manufacture").hide();
$("#Ul1List").hide();
$("#reworkList").hide();
});
I am trying to click the button highlighted in picture below. The code is from a web page:
I'm not to sure, but I believe the button is within an iFrame.
I have tried:
Dim wrapClick As HtmlElement = Contact.WebBrowser1.Document.GetElementById("Btn_WrapUp")
wrapClick.InvokeMember("Click")
And:
Dim elPoint As New Point(704, 340)
Dim wrapClick As HtmlElement = Contact.WebBrowser1.Document.GetElementFromPoint(elPoint)
wrapClick.InvokeMember("onClick")
And:
Contact.WebBrowser1.Document.GetElementById("Btn_WrapUp").InvokeMember("Click")
In all of the above, I have tried 'onClick' and 'Click'.
WebBrowser1 is on a different form.
Thanks!
Click button in WebBrowser control - working example:
Main file with frame - x.html:
<html>
<body>
<iframe width="400" height="300" src="y.html" id="frame1"> </iframe>
</body>
</html>
File placed in frame - y.html - with submit button:
<html>
<body>
<form action="...some_action...">
<input type="submit" id="btn"> Submit!
</form>
</body>
</html>
Button on VB form with OnClick event:
Dim Frame1 As HtmlWindow = WebBrowser1.Document.Window.Frames("frame1")
Frame1.Document.GetElementById("btn").InvokeMember("click")
VB2010Ex & .NET Framework 4 Client Profile.
I have tried code with action that load other site. Site was loaded in iframe.
Finally success :-)
I have following abc.aspx file:
<%# Page Language="vb" AutoEventWireup="false" Codefile="abc.aspx.vb" Inherits="abc" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<asp:textbox id="txtSearch" runat="server"></asp:textbox>
</body>
</html>
here is abc.aspx.vb file:
Partial Class abc
txtSearch.Text = "HELLO"
When i try to use this it gives error: txtSearch is not accesible?
Can anyone please tell me what am i doing wrong here?
This is not complete code just a snippet. But i think it gives an idea what am i trying to do.
Partial Class abc
txtSearch.Text = "HELLO"
If you're trying to set that value at the class level, that's not allowed. The functionality you'd be trying to achieve with that can be accomplished by setting it in the constructor:
Partial Class abc
Shared Sub New()
txtSearch.Text = "HELLO"
End Sub
I have a login page that stores a few values to localStorage (html5) then continues to a VB.Net page. I am looking for a method in VB that can read back those stored values and make them VB vars. Any ideas?
The VB.NET code-behind is running on the server and has no direct access to the local storage API of the browser.
You can, however, easily fill some hidden fields on the login page, using JavaScript, which will be posted on submit and can be read from the code-behind of the .NET page.
Something like this (not tested):
this.document.getElementById("HIDDEN_FIELD_ID").value = localStorage.STORED_VALUE;
...
<input type="hidden" id="HIDDEN_FIELD_ID" />
...
On the .NET page the value can be read like:
Request.Form("HIDDEN_FIELD_ID")
(There are other ways, but this one is easy to grasp.)
Be aware that login data in localStorage can be accessed (and modified) by the user, so make sure you are not creating a security risk.
This sample uses the above concept with VB Code:
Here is the html body element:
<body>
<form id="form1" runat="server">
<asp:HiddenField ID="hfLoaded" runat="server" />
<asp:HiddenField ID="hfLocalStorage" runat="server" />
</form>
<script type="text/javascript">
// Load LocalStorage
localStorage.setItem('strData', 'Local storage string to put into code behind');
function sendLocalStorageDataToServer()
{
// This function puts the localStorage value in the hidden field and submits the form to the server.
document.getElementById('<%=hfLocalStorage.ClientID%>').value = localStorage.getItem('strData');
document.getElementById('<%=form1.ClientID%>').submit();
}
// This checks to see if the code behind has received the value. If not, calls the function above.
if (document.getElementById('<%=hfLoaded.ClientID%>').value != 'Loaded')
sendLocalStorageDataToServer();
</script>
Here is the page load event:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim s As String
s = hfLocalStorage.Value
'This next line prevents the javascript from submitting the form again.
hfLoaded.Value = "Loaded"
End Sub
Now your code behind has the localStorage value available to it.