Button on usercontroll doesn't fire / raise event - asp.net

i have a usercontroll with two buttons and i want to get the raiseevent in the page where i call the usercontrol. Everything works fine except that i can't catch the event when i click the button on the user contol.
my markup for the uc:
<%# Control Language="VB" AutoEventWireup="false" CodeFile="MsgBox.ascx.vb" Inherits="MsgBox" ClassName="MsgBox" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="act" %>
<style type="text/css">
.Modal_trans {
opacity: 0.8;
filter: alpha(opacity=60);
}
</style>
<link rel="stylesheet" type="text/css" href="../Styles/StyleSheet.css" />"
<asp:Panel ID="PopPanel" runat="server"
Width="500px" Height="50px" DefaultButton="ok" Style="overflow:hidden;" CssClass="ModalGrid">
<table style="width: 100%; border-collapse: collapse; border: 0px;">
<tr id="boxheader" runat="server" style="cursor: move;">
<th colspan="2" style="padding-left: 10px; padding-top: 3px; padding-bottom: 3px;">
<asp:Label ID="lblTitle" runat="server" Text="Help"></asp:Label>
</th>
</tr>
<tr>
<td>
<asp:Image ID="Image" runat="server" ImageUrl="../images/info.png" />
</td>
<td style="padding: 10px; width: 220px;">
<div style="width: 295px; height:70px;"">
<asp:Label ID="lblMsg" runat="server" BackColor="#CCCCCC" />
</div>
</td>
</tr>
<tr>
<th colspan="2" style="padding-right: 20px; padding-top: 7px; padding-bottom: 7px;"" >
<asp:LinkButton ID="LinkButton1" runat="server" Style="display:none" ></asp:LinkButton>
<asp:Button ID="ok" runat="server" Text="Ok" Width="80px" />
<asp:Button ID="no" runat="server" Text="No / Cancel" Width="80px" Visible="False" />
</th>
</tr>
</table>
</asp:Panel>
<act:ModalPopupExtender ID="PopUp" runat="server" Enabled="True"
PopupControlID="PopPanel" TargetControlID="LinkButton1" BackgroundCssClass="Modal_trans"
OkControlID="ok" PopupDragHandleControlID="boxheader" Y="0" DropShadow="True">
<Animations>
<OnShown>
<Sequence AnimationTarget="PopPanel">
<Parallel Duration=".5" Fps="25">
<Move Horizontal="-175" Vertical="200" />
<Resize Width="350" Height="142"/>
<FadeIn />
</Parallel>
</Sequence>
</OnShown>
<OnHiding>
<Sequence AnimationTarget="PopPanel">
<%-- Scale the flyout down to 5% to make it disappear --%>
<Parallel Duration=".75" Fps="25">
<Scale ScaleFactor="0.05" Center="true" ScaleFont="true" FontUnit="px" />
<FadeOut />
</Parallel>
<%-- Reset the styles on the info box --%>
<StyleAction Attribute="display" Value="none"/>
<StyleAction Attribute="width" Value="350px"/>
<StyleAction Attribute="height" Value=""/>
<StyleAction Attribute="fontSize" Value="12px"/>
<%-- Re-enable the button --%>
<EnableAction Enabled="true" AnimationTarget="ok" />
</Sequence>
</OnHiding>
</Animations>
</act:ModalPopupExtender>
and the code behind
Partial Class MsgBox
Inherits System.Web.UI.UserControl
Public Shared IconInfo, IconExec, IconQues, IconError As Object
Public Property MsgText As String
Get
Return lblMsg.Text
End Get
Set(ByVal value As String)
lblMsg.Text = value
End Set
End Property
Public Event MsgButtonClick(ByVal buttonName As String)
Protected Sub MsgButtonOKClick(ByVal sender As Object, ByVal e As EventArgs) Handles ok.Click
RaiseEvent MsgButtonClick("ok")
End Sub
Protected Sub MsgButtonDenyClick(ByVal sender As Object, ByVal e As EventArgs) Handles no.Click
RaiseEvent MsgButtonClick("no")
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
IconInfo = "Show Information Icon"
IconExec = "Show Warning Icon"
IconQues = "Show Question Icon"
IconError = "Show Error Icon"
End Sub
Public Sub Pop(Optional ByVal Message As String = "", Optional ByVal Mode As Object = "IconInfo", Optional ByVal Title As String = "Info Message")
no.Visible = False
ok.Text = "Ok"
If Title <> "" Then
lblTitle.Text = Title
End If
Select Case Mode
Case "IconInfo"
Image.ImageUrl = "../images/info.png"
no.Visible = False
ok.Text = "Ok"
lblTitle.Text = "Info"
Case "IconExec"
Image.ImageUrl = "../images/exc.png"
no.Visible = False
ok.Text = "Ok"
lblTitle.Text = "Warning"
Case "IconQues"
Image.ImageUrl = "../images/ques.png"
no.Visible = True
ok.Text = "Yes" lblTitle.Text = "Question"
Case "IconError"
Image.ImageUrl = "../images/error.png"
no.Visible = False
ok.Text = "Ok"
lblTitle.Text = "Error"
End Select
PopUp.Show()
End Sub
Protected Sub No_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles no.Click
PopUp.Hide()
End Sub
Tested also with the variation
Public Event MsgButtonClick As EventHandler
Protected Sub MsgButtonOKClick(ByVal sender As Object, ByVal e As EventArgs) Handles ok.Click
RaiseEvent MsgButtonClick(sender, e)
End Sub
Protected Sub MsgButtonDenyClick(ByVal sender As Object, ByVal e As EventArgs) Handles no.Click
RaiseEvent MsgButtonClick(sender, e)
End Sub
In my page, it's a contetpage of a master page with update panel i try to catch the event with following in the code behind
Protected Sub test() Handles PopMsg.MsgButtonClick
Response.Write("Done")
End Sub
In the markup on the page i have placed the controldirectly behind the ContentTemplate of the UpdatePanel
<msg:PopInfoMsg ID="PopMsg" runat="server" />
And call the UC with a button click
Protected Sub HelpMsg()
PopMsg.MsgText = "My first Test<br/> Cheers<br/>"
PopMsg.Pop("", "IconInfo", "Info")
End Sub
The control open as expected, but when clicking the "OK" button the control close without an event.
In VS i tried to debug at the Button Click but the code doesn't run to this point.
Where i'am wrong? Confusing me the last days...
Cheers, Alex

Related

how to change label with button in vb?

so i want to have a button the changes the text of a label when i click it and it says page not available, but when i comment out the update panel it atleast shows up.
<form id="form1" runat="server">
<asp:ScriptManager ID="s1" runat = "server"></asp:ScriptManager>
<div>
<h1>UpDaTe PaNeL tEsT PaGe</h1>
start text : Hello <br />
updated text: World<br />
<hr />
<asp:UpdatePanel ID="p1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Update" OnClick="updateLabel" Height="29px" Width="110px"/> -->
Text: <asp:Label ID="Label1" runat="server" Text="hello"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
codebehind
Public Class aaaa
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Function updatelabel() As String
Label1.Text = "World"
End Function
End Class
The previous answer is correct. The handler for the Label Click event needs the sender as Object and the e as EventArgs parameters. If you do not put them, your updateLabeL() function will not be a valid match for the event handler.
You can remove the OnClick from the button and Try the classic method of handling events.
So your code will be... ( Sorry I'm posting it via app so it might not be in code view )
Protected Sub ChangeLabel(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.text = "TextGoesHere"
End Sub

Shopping Cart Implemented in Visual Studio Express

i implemented a shopping cart found here: http://www.beansoftware.com/ASP.NET-Tutorials/Developing-Shopping-Cart.aspx
Whenever I run the project separately it would work perfect but when i move the .aspx classes and items into my project, i get these errors:
Type lstcart is not declared, It may be inaccessible due to its protection level. Place:Cart.aspx.vb line 22
Type CartItem is not defined. line 23 and 26
Handles Clause requires a WithEvents Variable defined in the containing type or one of its base types. line 38
"Lblmassage is not declared. It may be inaccessible due to its protection level. line : 41
The code of the program is found in the link i posted above, would anyone help me fixing my issue?
This is my cart.aspx.vb code:
Partial Class Cart
Inherits System.Web.UI.Page
Private Cart As SortedList
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Cart = GetCart()
If Not IsPostBack Then
Me.DisplayCart()
End If
End Sub
Private Function GetCart() As SortedList
If Session("Cart") Is Nothing Then
Session.Add("Cart", New SortedList)
End If
Return CType(Session("Cart"), SortedList)
End Function
Private Sub DisplayCart()
lstCart.Items.Clear()
Dim CartItem As CartItem
Dim CartEntry As DictionaryEntry
For Each CartEntry In Cart
CartItem = CType(CartEntry.Value, CartItem)
lstCart.Items.Add(CartItem.Display)
Next
End Sub
Protected Sub btnRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemove.Click
If lstCart.SelectedIndex > -1 And Cart.Count > 0 Then
Cart.RemoveAt(lstCart.SelectedIndex)
Me.DisplayCart()
End If
End Sub
Protected Sub btnEmpty_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEmpty.Click
Cart.Clear()
lstCart.Items.Clear()
lblMessage.Text = ""
End Sub
Protected Sub btnCheckOut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCheckOut.Click
lblMessage.Text = "Please Wait Checking out..."
End Sub
End Class
This is my Cart.aspx:
<%# Page Title="Cart" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Cart.aspx.vb" Inherits="EliteHosting.Cart" %>
<!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>Shopping Cart</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<div style="font-size: xx-large; vertical-align: baseline; width: 614px; color: #ffffff;
direction: ltr; height: 112px; background-color: #660000; text-align: left; font-variant: normal">
<br />
My Web Shop</div><br /><br />
Your shopping cart:<br />
<table style="width: 500px" cellspacing="0"
cellpadding="0" border="0">
<tr>
<td style="width: 286px; height: 153px">
<asp:ListBox ID="lstCart" runat="server"
Width="267px" Height="135px">
</asp:ListBox>
</td>
<td style="height: 153px">
<asp:Button ID="btnRemove" runat="server"
Width="100px" Text="Remove Item" /><br /><br />
<asp:Button ID="btnEmpty" runat="server"
Width="100px" Text="Empty Cart" />
</td>
</tr>
</table>
<br />
<asp:Button ID="btnContinue" runat="server"
PostBackUrl="~/Order.aspx" Text="Continue Shopping" />
<asp:Button ID="btnCheckOut" runat="server" Text="Check Out" /><br />
<br />
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</div>
</form>
</body>
</html>

Accessing parent controls from a user control

I have a RadTabStrip on my parent.aspx page:
<telerik:RadTabStrip ID="rm_comparable" runat="server"
MultiPageID="Data"
CausesValidation="False" Enabled="false"
Height="100%"
ShowBaseLine="True">
<Tabs>
<telerik:RadTab runat="server" Text="Property" Selected="True" >
</telerik:RadTab>
<telerik:RadTab runat="server" Text="Sales" >
</telerik:RadTab>
</Tabs>
</telerik:RadTabStrip>
<telerik:RadMultiPage ID="Data" runat="server" SelectedIndex="0">
<telerik:RadPageView ID="mainData" runat="server">
<uc1:propertyData runat="server" ID="propertyData"/>
</telerik:RadPageView>
<telerik:RadPageView ID="salesData" runat="server">
<uc1:salesRecords ID="salesRecords" runat="server" />
</telerik:RadPageView>
</telerik:RadMultiPage>
and use this code to try to change the control.
Private Sub enableTabStrip()
Dim myTabControl As RadTabStrip = DirectCast(Me.Parent.FindControl("rm_comparable"), RadTabStrip)
myTabControl.Enabled = True
End Sub
When I call the code when the page loads the tab strip is then enabled.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
enableTabStrip()
End If
End Sub
When I move the code I am calling out of the page load event, my RadTabStrip doesn't become available to the users as it did when I called the event in the page load.
Protected Sub btn_addAppraisal_Click(sender As Object, e As EventArgs) Handles btn_addAppraisal.Click
enableTabStrip()
End Sub
I am wondering why this functionality will only work in the page load event.

Update panel automatically clears textboxes

I'm trying to combine an event of automatic printing of messages in case the passwords will not match. For that purpose I'm using an Update panel.
The Error message prints perfectly My problem is that both text-boxes automatically created after it. even thought I don't specify it in the code. I can't understand what have I done wrong.
This is the code for front end:
<asp:TextBox ID="NonPass1" runat="server" TextMode="Password"></asp:TextBox>
<asp:TextBox ID="NonPass2" runat="server" TextMode="Password" autopostback="True"></asp:TextBox>
<asp:UpdatePanel ID="UpdatePanel6" runat="server">
<ContentTemplate>
<asp:Panel ID="Panel6" runat="server">
<asp:Label ID="Label1" class="errorMess" runat="server" Text="The Passwords do not match!!!"></asp:Label>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="NonPass2" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
this is the back end code(I'm using VB):
Protected Sub NonPass2_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles NonPass2.TextChanged
If NonPass1.Text <> NonPass2.Text Then
Panel3.Visible = False
Panel6.Visible = True
Else
Panel3.Visible = False
Panel6.Visible = False
End If
End Sub
maybe you can use javascript functions.
<script>
var t1 = false; // textbox1 onfocus triggered = true;
var t2 = false; // textbox2 onfocus triggered = true;
function clearTBox() {
if (t1 && t2) {
if (document.getElementById("textbox1Name").value != document.getElementById("textbox1Name2").value) {
alert("Insert your code here");
}
}
}
</script>
Heres what i use in c# for this, maybe you can inherit the technique
private void ClearTextBoxes()
{
Action<Control.ControlCollection> func = null;
func = (controls) =>
{
foreach (Control control in controls)
if (control is TextBox)
(control as TextBox).Clear();
else
func(control.Controls);
};
func(Controls);
}
then call cleartextboxes();
Hope that helped :)
what do you mean :
My problem is that both text-boxes automatically created after it
please, make your question more clearance
Try not to use updatepanel,
try this
<asp:TextBox ID="NonPass1" runat="server" TextMode="Password"></asp:TextBox>
<asp:TextBox ID="NonPass2" runat="server" TextMode="Password" autopostback="True"></asp:TextBox>
<div id="Div_Error" runat="server" visible="false" style="width:100%">
<asp:Label ID="Label1" class="errorMess" runat="server" Text="The Passwords do not match!!!"></asp:Label>
and use this in the code behind:
Protected Sub NonPass2_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles NonPass2.TextChanged
If NonPass1.Text <> NonPass2.Text Then
Div_Error.visible=true;
Else
Div_Error.visible=false;
End If
End Sub
The only logical reason for the behavior as you described is perhaps, you put the above password boxes inside another UpdatePanel.
Therefore, the password boxes will be reloaded on postbacks (textchanged event), and TextBox of type Password do not retain its value after postbacks for security reason.
Though, if security is not a concern for you, there is a workaround to 'avoid' the password textboxes from being cleared on postbacks, by reassigning their values every time postbacks occur. Just include the following codes in the page Load event.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
NonPass1.Attributes.Add("value", NonPass1.Text)
NonPass2.Attributes.Add("value", NonPass2.Text)
End Sub
this is a simple example:
<table class="mytable" cellspacing="0" style="width: 100%">
<tr>
<td>
<asp:TextBox ID="Txt_Pass" runat="server" ></asp:TextBox>
</td>
<td>
<asp:TextBox ID="Txt_Re_Pass" runat="server" ></asp:TextBox>
</td>
<td width="66%" align="left">
<asp:Button ID="Btn_Filter" runat="server" Text="" Height="22px" />
</td>
</tr>
</table>
<br />
<div id="Div_Error" runat="server" visible="false" style="width:100%">
<asp:Label ID="lbl_Error" runat="server" class="msg">
</asp:Label>
</div>
and in the code behind , use this:
Protected Sub Btn_Filter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btn_Filter.Click
If Trim(Txt_Re_Pass.Text) <> "" Then
Div_Error.Visible = False
if Txt_Pass.Text <> Txt_Re_Pass.Text then
Div_Error.Visible = True
lbl_Error.text="The Passwords do not match!!!""
else
Div_Error.Visible = False
End if
Else
Div_Error.Visible = True
lbl_Error.text="Please re enter your password"
End If
End Sub

RadListView insertitemtemplate still showing after insert

I have a pretty simple scenario using manual data operations with rad list view. I insert an item into my collection in the ItemInserting event, the listview shows the new item but the insertitemtemplate is still showing. Is my setup wrong? Do I have to manually hide the thing?
Markup
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Test2.aspx.vb" Inherits="Test2" %>
<!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">
<telerik:RadScriptManager runat="server" ID="rsm"></telerik:RadScriptManager>
<div>
<telerik:RadListView ID="rlv" runat="server" DataKeyNames="UserID" ItemPlaceholderID="itemPlaceholder" Height="400px" AllowPaging="true">
<InsertItemTemplate>
<tr>
<td>
<div style="vertical-align: middle; white-space: nowrap;">
<asp:LinkButton ID="btnInsert2" runat="server" Text="Insert" CommandName='<%# Telerik.Web.UI.RadListView.PerformInsertCommandName %>'></asp:LinkButton>
<asp:LinkButton ID="btnCancel2" runat="server" Text="Cancel" CommandName='<%# Telerik.Web.UI.RadListView.CancelCommandName %>'></asp:LinkButton>
</div>
</td>
<td>
<div style="vertical-align: middle; white-space: nowrap;">
<telerik:RadComboBox id="RadComboBox1" runat="server" autopostback="True" causesvalidation="False" allowcustomtext="True" backcolor="White" emptymessage="Select a Person" enableloadondemand="True" showmoreresultsbox="True" width="150px" dropdownwidth="200px" zindex="9002" enablescreenboundarydetection="False" style="margin-bottom: 0px" datatextfield="UserName" datavaluefield="pkUserID" filter="Contains" onitemsrequested="RCB_ItemsRequested">
<CollapseAnimation Duration="200" Type="OutQuint" />
</telerik:RadComboBox>
</div>
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Literal ID="lName" runat="server" Text='<%# Eval("UserName") %>'></asp:Literal>
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<asp:LinkButton ID="btnInsert" runat="server" CausesValidation="False" CommandName='<%# Telerik.Web.UI.RadListView.InitInsertCommandName %>' Text="Add User" />
<table>
<tr>
<td style="border: ridge 1px lightgray">
<strong>Applicable Users</strong>
<table>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
</telerik:RadListView>
</div>
</form>
</body>
</html>
Code Behind
Partial Class Test2
Inherits System.Web.UI.Page
Public Property Users As IList(Of UserInfo)
Get
Return If(ViewState("Users"), New List(Of UserInfo))
End Get
Set(ByVal value As IList(Of UserInfo))
ViewState("Users") = value
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Me.Users = GetInitialUsers()
End If
rlv.DataSource = Users
rlv.DataBind()
Me.ViewState.SetItemDirty("Users", True)
End Sub
Private Function GetInitialUsers() As IList(Of UserInfo)
Dim Users As New List(Of UserInfo)
Users.Add(New UserInfo(1, "1"))
Users.Add(New UserInfo(2, "2"))
Users.Add(New UserInfo(3, "2"))
Users.Add(New UserInfo(4, "4"))
Return Users
End Function
Protected Sub RCB_ItemsRequested(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs)
Dim ComboBox As Telerik.Web.UI.RadComboBox = sender
Dim itemOffset As Integer = e.NumberOfItems
Dim NumberOfItems As Integer = 1000000000
Dim Take As Integer = 20
For i As Integer = 0 To Take
ComboBox.Items.Add(New Telerik.Web.UI.RadComboBoxItem(i + itemOffset, (i + itemOffset).ToString))
Next
Dim NumberOfItemsInComboBox As Integer = e.NumberOfItems + ComboBox.Items.Count
If NumberOfItems > 0 Then
e.Message = [String].Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", NumberOfItemsInComboBox, NumberOfItems)
Else
e.Message = "No matches"
End If
e.EndOfItems = (NumberOfItemsInComboBox >= NumberOfItems)
End Sub
Protected Sub rlv_ItemInserting(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadListViewCommandEventArgs) Handles rlv.ItemInserting
Dim ComboBox As Telerik.Web.UI.RadComboBox = e.ListViewItem.FindControl("RadComboBox1")
If Not String.IsNullOrWhiteSpace(ComboBox.SelectedValue) Then
Dim UserID As Integer = ComboBox.SelectedValue
Dim UserName As String = ComboBox.Text
Me.Users.Add(New UserInfo(UserID, UserName))
Else
e.Canceled = True
End If
End Sub
<Serializable()>
Public Class UserInfo
Private _UserID As Integer
Private _UserName As String
Public ReadOnly Property UserID As Integer
Get
Return _UserID
End Get
End Property
Public ReadOnly Property UserName As String
Get
Return _UserName
End Get
End Property
Public Sub New(ByVal UserID As Integer, ByVal UserName As String)
_UserID = UserID
_UserName = UserName
End Sub
End Class
End Class
After you have inserted the new item, set the InsertItemPosition to None:
RadListView1.InsertItemPosition = RadListViewInsertItemPosition.None
radlistview1.IsItemInserted=false;
or set e.canceled=false after save click

Resources