ASP.NET form using label - asp.net

I have to create an asp.net form using label. I found an example for this task and tried to run it but nothing from the expected result appeared. Do you have any idea what else should be done to run it? (I'm still at the very beginnig of asp.net.)
Here is the code:
<%# Page Language="vb" %>
<script runat="server">
Sub submit(Sender As Object, e As EventArgs)
label1.Text=txt1.Text
End Sub
</script>
<html>
<body>
<form runat="server">
Write some text:
<asp:TextBox id="txt1" Width="200" runat="server"/>
<asp:Button id="b1" Text="Copy to Label" OnClick="submit" runat="server"/>
<p><asp:Label id="label1" runat="server"/></p>
</form>
</body>
</html>

Please check the default access modifier for your function(SUB).
In c# we use protected and from MSDN
Class members, including nested classes and structs, can be public,
protected internal, protected, internal, or private. The access level
for class members and struct members, including nested classes and
structs, is private by default. Private nested types are not
accessible from outside the containing type.
So try with Friend access modifier for VB.Net.

Related

intercept __doPostBack function

According to this post you can intercept the post by overriding this function __doPostBack but it wont work. If I view the generated source I am not able to see the function that is meant to be auto generated and causes asp.net to make the postback.
where is this function? and how do I intercept it?
I'm using asp.net 4 and vs 2012
Default.aspx
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
//breaks here as it cant find __doPostBack
var __original = __doPostBack;
__doPostBack = myFunction();
function myFunction() {
alert('intercept');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
Default.aspx.vb
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim txt As String = TextBox1.Text
End Sub
End Class
An ASP.NET Button renders an <input type="submit" /> to postback the form, and will never use this method. The __doPostBack method is only used for elements that are not input buttons that do not normally cause a POST operation to the server. This is a function that's inside one of the Microsoft's JavaScript file, and only gets rendered out on the client. Common uses for __doPostBack are LinkButton controls, controls that may have AutoPostBack="true", and others.
Also, there may be a call to WebForms_DoPostBack... something named like that that also posts back, but internally calls __doPostBack.
If you are trying to prevent postback on click of your button, attach to the submit event of the form, and cancel the postback operation that way.

Datalist name not declared when using Code Behind. aspx.vb

I'm a totally newbie to .NET programming, and trying to get some stuff off the ground.
I've run some code which extracts data from a database and presents it into a Datalist. It works fine when the script is on the aspx file, but when I transfer it to a code behind I get the following error
Compiler Error Message: BC30451: Name 'showIt' is not declared.
(showIt is the ID of the Datalist)
Obviously, the Datalist markup is in my aspx page, and the script is in the code behind .vb file.
As I say, it all works when the scripts are all on the one .aspx file.
The code in the code behind is: (I've skipped the SQL and connection strings to keep this post concise)
Partial Public Class Data
Inherits System.Web.UI.Page
Sub Page_Load(sender As Object, e As EventArgs)
Dim objDataReader As OdbcDataReader
objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
showIt.DataSource = objDataReader
showIt.DataBind()
objDataReader.Close()
end sub
end class
The aspx file looks like this: (again simplified, basic HTML markup etc removed)
<%# Page Language="VB" AutoEventWireup="true" CodeFile="dataOut.aspx.vb" Inherits="_Default"%>
<form id="form1" runat="server">
<asp:DataList ID="showIt" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal">
<ItemTemplate>
<div style="width:300px; display:inline-block; height: 200px; overflow:hidden">
<div style="width:100%; background:#880000; border-bottom: solid 1px black"><%# DataBinder.Eval(Container.DataItem, "item") %></div>
<br />
<%# Data.stockDisplay(Eval("shopstock"))%>
<div style="clear: both"><%# Left(DataBinder.Eval(Container.DataItem, "description"),150) %></div>
</div>
</ItemTemplate>
Ah ha.... I've fixed it!
needed to declare the DataList before the function so it became publicly available... or at least I think that was the issue, it works now anyway
Protected WithEvents showIt As System.Web.UI.WebControls.DataList
I now have other issues... that can go in another question though!

UserControl causes full postback in UpdatePanel

I'm fighting this problem for few hours now and I'm not getting any closer to solution.
The thing is: I'm having few UpdatePanels on my master page. And then, I have RadTreeViewcontrol on the page, that should cause partial postback each time node is clicked. Everything works fine there.
Since I'm using the same tree on some other pages, I moved this functionality to UserControl. Stuff I've done before, so no problem. Moved some code to ascx, created some events. Everything always worked for me well. But not now.
RadTreeView is nested inside UserControl, and this control on master page with update panels, see below:
<asp:Panel ID="pnlContentWrapper" runat="server" CssClass="ContentWrapper">
<div id="HeaderBreadCrumb">
<asp:ContentPlaceHolder ID="HeaderBreadCrumbContent" runat="server" />
</div>
<div id="HeaderMenu">
<asp:UpdatePanel ID="upnlTreeMenu" runat="server">
<ContentTemplate>
<asp:ContentPlaceHolder ID="HeaderMenuContent" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="treeProductTree" />
</Triggers>
</asp:UpdatePanel>
</div>
<div id="TreeDiv">
<fp:ProductTree ID="treeProductTree" runat="server" />
</div>
<asp:Panel ID="ContentDiv" ClientIDMode="Static" runat="server">
<asp:UpdatePanel ID="upnlTreeContent" runat="server">
<ContentTemplate>
<asp:ContentPlaceHolder ID="TreePageContent" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="treeProductTree" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>
</asp:Panel>
And the user control is really simple:
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="ProductTree.ascx.vb"
Inherits="ProductTree" %>
<div>
<telerik:RadTreeView ID="treeProductTree" ClientIDMode="Static" runat="server" EnableDragAndDrop="false"
SkinID="CustomSkin" CssClass="MasterProductTree" DataFieldID="NodeId" DataFieldParentID="NodeParentId"
DataTextField="NodeName" DataValueField="NodeId" />
</div>
And some code behind:
Imports Telerik.Web.UI
Public Class ProductTree
Inherits System.Web.UI.UserControl
Public Event NodeExpand As EventHandler(Of ProductTreeNodeExpandEventArgs)
Public Event SelectedNodeChange As EventHandler
Protected Sub ProductTree_NodeExpand(ByVal sender As Object, ByVal e As RadTreeNodeEventArgs) _
Handles treeProductTree.NodeExpand
Dim nodeId As Integer = CInt(e.Node.Value)
Dim evetArgs = New ProductTreeNodeExpandEventArgs(nodeId)
RaiseEvent NodeExpand(Me, evetArgs)
//'some logic
End Sub
Protected Sub ProductTree_OnNodeClick(ByVal sender As Object, ByVal e As RadTreeNodeEventArgs) _
Handles treeProductTree.NodeClick
RaiseEvent SelectedNodeChange(Me, New System.EventArgs())
End Sub
End Class
What I don't know is: why is this causing full postback instead of partial? I suspect that it may have something to do with raising my for SelectedNodeChange, but I don't know how to deal with it other way. I need to let other components know, that node selection changed. How can I improve this to make it work with UpdatePanels?
Suspect that registering a UserControl as an AsyncPostbackTrigger is not going to get you very far. The UC itself is not a postback control, rather, it contains one. I would expose the tree control as a public property of the usercontrol, and then in code behind in your containing page, register the tree control itself as the trigger, rather than the UserControl.
Check this for a similar situation... it's not a great Q&A thread but it shows the basic gist.
Well, I have the similar situation but it's a little more complicated.
User control causing postback is actually added dynamically depending on the request parameter value.
I was so glad the ClietIDMode paramter was added, I didn't think a problem like tat would be overlooked by Microsoft.
The same problem that I am facing right now... I thing that could be fixed by wrapping up the UC with an iFrame.

Using the AutoCompleteExtender with ASP.Net

How is everyone today?
The Problem
Basically, I'm delving into the world of the AjaxControlToolkit today, with the main aim of fulfilling my AutoComplete requirements. I've set everything up as tutorialised and am a little confused as to why things aren't working (well I have an inkling as to what may be holding me back).
I've added the AjaxControlToolkit dll to my project and in my Markup I have the following :
at the top
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxControlToolkit" %>
then within my content
<ajaxControlToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></ajaxControlToolkit:ToolkitScriptManager>
<asp:TextBox ID="txtSearch" runat="server" CssClass="search"></asp:TextBox>
<ajaxControlToolkit:AutoCompleteExtender ID="autoCompleteSearchExtender" runat="server" TargetControlID="txtSearch" ServiceMethod="GetCompletionList"></ajaxControlToolkit:AutoCompleteExtender>
Then in the code behind, I have my nice little function (which the breakpoint within is never reached)
<System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()> _
Public Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As String()
' Get current list
Dim myList As List(Of MyClass) = GetSearchResultList()
Return (From s In myList Select s.Name).ToArray()
End Function
The function is never being called, for a reason I am unsure of.
Potential Issues
There are a couple of potential issues where things may be going wrong:
I've tried adding the AjaxControlToolkit dll to the Toolbar in VS (2010), but all the controls are greyed out...
The GetCompletionList function (WebMethod) I have written is in the code behind the page. Could this not be called because it has to be in a Web Service perhaps?
I've also just realised that my function in the code behind isn't Shared, is this required? Because the list associated with the auto complete is dynamic.
Any help would be appreciated.
Thanks in advance.
Try putting your GetCompletionList function in a web-service (asmx is easiest). Make sure the the web-service class has a [ScriptService] attribute and that your function has a [ScriptMethod] attribute.
You'll also need to supply the path to the web-service in your AutoCompleteExtender's "ServicePath" property (i.e. ServicePath="~/MyService.asmx")
Also, you don't need the "contextKey" parameter in your function unless you are passing a context key from your AutoCompleteExtender control.
hth
Update: Add these attributes and try it. I have implemented and it works with page behind web methods.
ServiceMethod="yourGetfunction"
MinimumPrefixLength="2"
CompletionInterval="100"
EnableCaching="FALSE"
CompletionSetCount="20"
You can check this solution: http://suggester.codeplex.com/
Demo to test: http://show-demos.net/suggester/
Its not from ALAX Toolkit but it uses ASP.NET AJAX and jQuery and has more rich functionality
I kind of made this working:
<WebMethod()> _
Public Shared Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer) As List(Of String)
Dim listData As New List(Of String)
listData.Add("A")
listData.Add("B")
listData.Add("C")
Return listData
End Function
My html:
<form id="maincontent" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods = "true">
</asp:ScriptManager>
<div>
<table style="margin-top:40px;color:White">
<tr>
<td>
Type in your search:
</td>
<td>
<asp:TextBox ID="searchBox" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender ServiceMethod="GetCompletionList" MinimumPrefixLength="1"
CompletionInterval="10" EnableCaching="false" CompletionSetCount="1" TargetControlID="searchBox"
ID="AutoCompleteExtenderPersonSearch" runat="server" FirstRowSelected="false">
</asp:AutoCompleteExtender>
</td>
</tr>
</table>
</div>
</form>

Webpart Connections asp.net VB

Im having the following problem with vb.net asp.net webparts. Im trying to create a static connection between webparts but im running into a problem, namely:
Could not find the connection provider Web Part with ID 'Ucl_Diary_Summary1'
I have the following defined as my iterface:
Public Interface IDiaryPartsProvider
function Test as String
End Interface
I have the following as my Consumer (UserControl):
Partial Class UsrCtrls_Diary_ucl_DiaryAwaitingReview
Inherits System.Web.UI.UserControl
<ConnectionConsumer("Test", "myID")> _
Public Sub GetTextTransferInterface(ByVal provider As IDiaryPartsProvider)
Dim a As String = provider.Test()
UserMsgBox(a.ToString, Me.Page)
End Sub
End Class
I have the following defined as my Provider (UserControl):
Partial Class UsrCtrls_Diary_Diary_Summary
Inherits System.Web.UI.UserControl
Implements IWebPart, IDiaryPartsProvider
<ConnectionProvider("myID")> _
Public Function Test() As String Implements IDiaryPartsProvider.Test
Return "this is a test"
End Function
End Class
I have my default.aspx as follows:
<%# Register Src="UsrCtrls/Diary/ucl_Diary_Summary.ascx" TagName="ucl_Diary_Summary"
TagPrefix="uc4" %>
<%# Register Src="UsrCtrls/Diary/ucl_DiaryAwaitingReview.ascx" TagName="ucl_DiaryAwaitingReview"
TagPrefix="uc5" %>
<asp:WebPartManager ID="WebPartManager1" runat="server">
<StaticConnections>
<asp:WebPartConnection ID="cnn"
ConsumerID="Ucl_DiaryAwaitingReview1"
ProviderID="Ucl_Diary_Summary1"
/>
</StaticConnections>
</asp:WebPartManager>
<asp:WebPartZone ID="zoneDiaryTopLeft" runat="server" EmptyZoneText="Add WebPart Here" DragHighlightColor="#454777" HeaderText=" ">
<ZoneTemplate>
<asp:Panel ID="pnl1" runat="server" title="Claims Awaiting Review">
<asp:UpdatePanel ID="udp_TopLeft" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
<uc5:ucl_DiaryAwaitingReview ID="Ucl_DiaryAwaitingReview1" runat="server" title="Claims Awaiting Review" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</ZoneTemplate>
</asp:WebPartZone>
<asp:WebPartZone ID="zoneDiaryTopRight" runat="server" EmptyZoneText="Add WebPart Here" DragHighlightColor="#454777" HeaderText=" ">
<ZoneTemplate>
<asp:Panel ID="PNL2" runat="server" title="Diary Summary">
<asp:UpdatePanel ID="udp_TopRight" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
<uc4:ucl_Diary_Summary ID="Ucl_Diary_Summary1" runat="server" Title="Diary Summary" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</ZoneTemplate>
</asp:WebPartZone>
I can only assume its because I have my webparts - usercontrol wrapped in a panel (used for scrolling) and also an updatepanel which I use to refresh, so how do I get it to see the usercontrol?
Thanks in advance.
James.
I didn't have a chance to look at your message in detail but the issue appears to be with your provider. It should be returning an object that implements the interface used for communication to the consumer (usually a reference to itself).
Check out the following resource for more info:
Introducing ASP.NET Web Part Connections
I found out my problem/solution in the end.
Rob you are correct I needed to pass back an instance of the object implementing the interface but also, you cant reference a User Control for a static connection which is a child of two other controls, namely the panel and update panel. I was doing this all wrong. I changed it so that the update panel is inside the user control rather than on the default page. That way all the specific (web part) components are self contained.
Also, referring back to the original item of returning the instance, I replaced the following:
Public Function Test() As String Implements IDiaryPartsProvider.Test
Return "this is a test"
End Function
with:
<ConnectionProvider("myID")> _
Public Function Test() As IDiaryPartsProvider
Return me
End Function
Public ReadOnly Property Test() As String Implements IDiaryPartsProvider.Test
Get
Return "This is a test"
End Get
End Property
Hope this can help someone!
James.

Resources