I'm writing a program where user has 3 drop boxes to input date, month and a year. after user selects the values I concatenate them and check for valid By default when a page loads I need to assign a current day, month and year to each drop box accordingly. Then I check validity of the date and pass value to the database.
My problem is that when I assign the values to the text of DropBoxes upon the loading of the page they are becoming permanent. even if the index is changed the values which are passed to the database are the ones which assigned to them when the page is loaded.
I can't actually understand what am I doing wrong:
these are the code samples which I've used:
I populate them with a current date values using folowing code(on the page Load event):
Dim CurYear As Integer = DatePart("yyyy", Now)
Dim CurDate As Integer = DatePart("d", Now)
Dim CurMonth As String = Format(Today.Date, "MMMM")
Dim CurDate2 As Integer = DatePart("d", Now)
Dim CurMonth2 As String = Format(Now, "MM")
Dates.Text = CurDate
Monthe.Text = CurMonth
years.Text = CurYear
Month2.Text = CurMonth2
Dates2.Text = CurDate2
Than I had to synchronize the selected index of 2 the dropboxes which contain numerical value of the month and 2 digit format of the day, to to form the proper string for checking of the date
Protected Sub Months_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Months.SelectedIndexChanged
Month2.SelectedIndex = Months.SelectedIndex
TextBox3.Text = years.Text & "-" & Monthes.Text & "-" & Dates.Text
End Sub
Protected Sub Dates_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Dates.SelectedIndexChanged
Dates2.SelectedIndex = Dates.SelectedIndex
TextBox3.Text = years.Text & "-" & Monthes.Text & "-" & Dates.Text
End Sub
And this is front end ASP code:
<asp:DropDownList ID="Dates" runat="server" autopostback="true">
<asp:ListItem></asp:ListItem>
<asp:ListItem>1</asp:ListItem>
...
<asp:ListItem>26</asp:ListItem>
<asp:ListItem>27</asp:ListItem>
<asp:ListItem>28</asp:ListItem>
<asp:ListItem>29</asp:ListItem>
<asp:ListItem>30</asp:ListItem>
<asp:ListItem>31</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="Months" runat="server" autopostback="true" >
<asp:ListItem></asp:ListItem>
<asp:ListItem>January</asp:ListItem>
...
<asp:ListItem>November</asp:ListItem>
<asp:ListItem>December</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="years" runat="server" autopostback="true" >
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="Dates2" runat="server" AutoPostBack="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem>01</asp:ListItem>
<asp:ListItem>02</asp:ListItem>
....
<asp:ListItem>29</asp:ListItem>
<asp:ListItem>30</asp:ListItem>
<asp:ListItem>31</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="Month2" runat="server" AutoPostBack="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem>01</asp:ListItem>
....
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
</asp:DropDownList>
Again, if I don't assign the default values to the boxes upon loading of the page it works perfectly. if i do, those values are fixed no mater what you choose
The comparevalidator:
<asp:UpdatePanel ID="UpdatePanel19" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox3" ValidationGroup="CompareValidatorDateTest" runat="server"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Dates" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="Monthes" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="years" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<asp:CompareValidator ID="CompareValidator3" Display="dynamic" ControlToValidate="TextBox3"
Type="Date" Operator="LessThanEqual" Text="Please enter a valid date" runat="server"
ValidationGroup="CompareValidatorDateTest"
I suppose you didnt use IsPostBack property while binding the dropdown with values on page load.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
// Bind your dropdown here here
End If
Related
Problem: When suburb drop down list value is changed - Page is posting back.
Desired result: Changing value in drop down list updates post code text box value without a page post back (post code text box is normally hidden)
Page code:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:DropDownList ID="Suburb" runat="server" DataTextField="LocalityPhrase"
DataValueField="Locality" AutoPostBack="true" class="DropDown" OnSelectedIndexChanged="Suburb_SelectedIndexChanged"/>
<asp:UpdatePanel runat="server" id="UpdatePanelPostCode" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="Postcode" runat="server" Visible="true"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Suburb" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
Code behind:
Protected Sub Suburb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Suburb.SelectedIndexChanged
'get postcode from suburb drop down
Dim pCode As String = ""
If Suburb.SelectedValue <> "" Then
pCode = Suburb.SelectedItem.Text.Substring(Len(Suburb.SelectedItem.Text) - 5, 4)
End If
Postcode.Text = pCode
End Sub
It appears there was something wrong with my install of the AJAX toolkit. Removed and reloaded it in Visual Studio (2008) and all is well.
in a project I am working on I have TabContainer (AJAX.NET) have many tabPanels all of them are doing the same function BUT each on on a different Table
let me give a sample :
<asp:TabContainer ID="TabContainer3" runat="server" ActiveTabIndex="0" BorderStyle="None"
BorderWidth="0" CssClass="MyTabStyle" Width="625px">
<asp:TabPanel ID="TabPanel1" runat="server">
<HeaderTemplate>
Tab_x
</HeaderTemplate>
<ContentTemplate>
<asp:TextBox ID="txt_x" runat="server"></asp:TextBox>
<asp:Button ID="btnx" runat="server" Text="Button" />
</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel ID="TabPanel2" runat="server">
<HeaderTemplate>
Tab_y
</HeaderTemplate>
<ContentTemplate>
<asp:TextBox ID="txt_y" runat="server"></asp:TextBox>
<asp:Button ID="btny" runat="server" Text="Button" />
</ContentTemplate>
</asp:TabPanel>
</asp:TabContainer>
Code behind (VB.NET)
Protected Sub btnx_Click(sender As Object, e As System.EventArgs) Handles btnx.Click
SaveText_x(txt_x.Text)
End Sub
Protected Sub btny_Click(sender As Object, e As System.EventArgs) Handles btny.Click
SaveText_y(txt_y.Text)
End Sub
is there a way to create general Sub or Function so if I clicked btnx function Save_x(txt_x.Text) be called
and when I click btny function Save_y(txt_y.Text) be called ?
You can assign multiple buttons to have the same click handler with the following code :
Protected Sub btn_Click(sender As Object, e As System.EventArgs) Handles btnx.Click, btny.Click
Dim btn As Button = CType(sender, Button)
If btn.ID = "btnx" Then
SaveText_x(txt_x.Text)
ElseIf btn.ID = "btny" Then
SaveText_y(txt_y.Text)
End If
End Sub
Both btnx and btny will both fire this Sub and it will check the button that sent it to see which method to call.
1)Made user control with public string property. use view state to store value of that property.
2)Add that user control into your tabs set that property with values like "X" or "Y" or any.
3)On button click check that property with if .. else if .. else or by switch statement and call your SaveText functions variants.
I have a little problem that bothers me presently. I have 5 dropboxes which I populate dynamically:
date
month(letter)
year
numeric-date 5 numeric-month.
I populate them with a current date values using folowing code:
Dim CurYear As Integer = DatePart("yyyy", Now)
Dim CurDate As Integer = DatePart("d", Now)
Dim CurMonth As String = Format(Today.Date, "MMMM")
Dim CurDate2 As Integer = DatePart("d", Now)
Dim CurMonth2 As String = Format(Now, "MM")
Dates.Text = CurDate
Monthe.Text = CurMonth
years.Text = CurYear
Month2.Text = CurMonth2
Dates2.Text = CurDate2
Than I have an update panel which handles boxes 4 and 5. They should change the Index if the boxes 1 and 2 are changed. But it doesn't work.
Protected Sub Months_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Months.SelectedIndexChanged
Month2.SelectedIndex = Months.SelectedIndex
End Sub
Protected Sub Dates_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Dates.SelectedIndexChanged
Dates2.SelectedIndex = Dates.SelectedIndex
End Sub
If I don't populate the drop boxes with a current date (code sample 1) it works. If I do, selectedIndexChanged doesn't react. What can be the possible mistake?
asp markup:
<asp:DropDownList ID="Dates" runat="server" autopostback="true">
<asp:ListItem></asp:ListItem>
<asp:ListItem>1</asp:ListItem>
...
<asp:ListItem>26</asp:ListItem>
<asp:ListItem>27</asp:ListItem>
<asp:ListItem>28</asp:ListItem>
<asp:ListItem>29</asp:ListItem>
<asp:ListItem>30</asp:ListItem>
<asp:ListItem>31</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="Months" runat="server" autopostback="true" >
<asp:ListItem></asp:ListItem>
<asp:ListItem>January</asp:ListItem>
...
<asp:ListItem>November</asp:ListItem>
<asp:ListItem>December</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="years" runat="server" autopostback="true" >
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="Dates2" runat="server" AutoPostBack="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem>01</asp:ListItem>
<asp:ListItem>02</asp:ListItem>
....
<asp:ListItem>29</asp:ListItem>
<asp:ListItem>30</asp:ListItem>
<asp:ListItem>31</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="Month2" runat="server" AutoPostBack="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem>01</asp:ListItem>
....
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
</asp:DropDownList>
I ave an asp page with 1 update panels with 2 panels which are controlled by 2 buttons if you click in first button first panel appears and second becomes hidden and if you click second button the same thing happens for the second panel. It's working ok and no problem about that.
The problem begins in the second panel where I have another update panel which is in charge of printing error messages. The messages which just check the length of input are working ok, but those messages which involve querying the database to check the validity of id are not being printed .
I thought the querying are the problem and I've tried them in a separated page without update panels and they are working ok. The problem lays somewhere with Update panels. And I really can't understand where. I would be most grateful if you can help me.
This is front end code(i omit some insignificant details):
<asp:Button ID="Button1" runat="server" Text="[Input your Id?]" />
<asp:Button ID="Button2" runat="server" Text="[Aditional info]" />
.....
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
< asp:Panel ID="Panel1" runat="server">
some code here
</asp:Panel>
<asp:Panel ID="Panel2" runat="server">
<b>*ID Number: </b>
<asp:TextBox ID="IdNo" runat="server" ></asp:TextBox><font size='2'>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" class="errorMess" ErrorMessage="Letters are not Allowed!!" ControlToValidate="IdNumb" ValidationExpression="\d+"
runat="server" />
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<asp:Label ID="errorEm" class="errorMess" runat="server" Text="Please fill in the required fields"></asp:Label>
<asp:Label ID="errorLenght" class="errorMess" runat="server" Text="Id is too long!!!"></asp:Label>
<asp:Label ID="errorUser" class="errorMess" runat="server" Text="Id is not valid!!!"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button5" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:Button ID="Button5" runat="server" Text="Generate" /></td></tr>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
and this is backend VB code:
Protected Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click
If IdNo.Text = "" Then
Panel1.Visible = False
Panel2.Visible = True
errorEm.Visible = True
ElseIf IdNo.Text.Length > 9 Then
Panel1.Visible = False
Panel2.Visible = True
errorLenght.Visible = True
Else
Try 'everything that comes here does not work'
myconn.Open()
Dim stquery As String = "SELECT * from account WHERE user_id= #id"
Dim smd = New MySqlCommand(stquery, myconn)
smd.Parameters.AddWithValue("#id", Convert.ToInt32(IdNo.Text))
Dim myreader = smd.ExecuteReader()
If Not myreader.HasRows Then
Panel1.Visible = False
Panel2.Visible = True
errorUser.Visible = True
myconn.Close()
Return
Else
Panel1.Visible = False
Panel2.Visible = True
myreader.Read()
Dim ErrorMessage As String = "alert('User has been found');"
Page.ClientScript.RegisterStartupScript(Me.GetType(), "ErrorAlert", ErrorMessage, True)
myconn.Close()
End If
myconn.Close()
Catch ex As Exception
Dim ErrorMessage As String = "alert('" & ex.Message.ToString() & "');"
Page.ClientScript.RegisterStartupScript(Me.GetType(), "ErrorAlert", ErrorMessage, True)
myconn.Close()
End Try
End If
End Sub
When you want to execute script from inside of Update panel - don't use
Page.ClientScript.RegisterStartupScript
Use
ClientScriptManager.RegisterStartupScript
instead.
Ref: http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript.aspx
I'm creating a chat application in asp.net. In this I'm creating tab panels in a tab container dynamically for each user. when you select a new user I'm generating a tab for him with two text boxes and send button to send messages. If he doesn't want to continue his chat I need to remove or delete that tab. Is there any way that i can remove or delete those tabs in asp.net using vb.net?
Here is a complete working sample, it could be broken down into this line(in RemoveTab):
Me.TabContainer1.Tabs.Remove(tab)
But have a look yourself ...
Page-ASPX:
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" CombineScripts="true">
</asp:ToolkitScriptManager>
<div>
<asp:UpdatePanel ID="UpdTabContainer" ChildrenAsTriggers="false" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:TabContainer ID="TabContainer1" runat="server" AutoPostBack="true">
<asp:TabPanel ID="TabUserList" runat="server" HeaderText="UserList">
<ContentTemplate>
<asp:UpdatePanel ID="UpdUserList" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:ListBox ID="ListBox1" SelectionMode="Single" AutoPostBack="true" OnSelectedIndexChanged="UserChanged" runat="server">
<asp:ListItem Text="User 1" Value="1"></asp:ListItem>
<asp:ListItem Text="User 2" Value="2"></asp:ListItem>
<asp:ListItem Text="User 3" Value="3"></asp:ListItem>
</asp:ListBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TabContainer1" EventName="ActiveTabChanged" />
</Triggers>
</asp:UpdatePanel>
</ContentTemplate>
</asp:TabPanel>
</asp:TabContainer>
</ContentTemplate>
</asp:UpdatePanel>
</div>
Codebehind:
Public Class TabContainerSample
Inherits System.Web.UI.Page
Const idSuffix = "_"c
Property CreatedTabIDs As List(Of String)
Get
If Session("CreatedTabIDs") Is Nothing Then
Session("CreatedTabIDs") = New List(Of String)
End If
Return DirectCast(Session("CreatedTabIDs"), List(Of String))
End Get
Set(value As List(Of String))
Session("CreatedTabIDs") = value
End Set
End Property
Private Sub TabContainerSample_Init(sender As Object, e As System.EventArgs) Handles Me.Init
For Each userID In CreatedTabIDs
AddTab(userID)
Next
End Sub
Private Sub AddTab(tabID As String)
Dim ucChat = DirectCast(Page.LoadControl("ChatControl.ascx"), ChatControl)
ucChat.UserName = tabID
AddHandler ucChat.ChatSubmitted, AddressOf chatSubmitted
AddHandler ucChat.ChatClosed, AddressOf chatClosed
Dim newTabPanel = New AjaxControlToolkit.TabPanel
newTabPanel.ID = String.Format("Tab{0}{1}", idSuffix, tabID)
newTabPanel.HeaderText = String.Format("TabPanel {0}", tabID)
newTabPanel.Controls.Add(ucChat)
TabContainer1.Tabs.Add(newTabPanel)
End Sub
Private Sub RemoveTab(tabID As String)
Dim tab = (From t In Me.TabContainer1.Tabs.Cast(Of AjaxControlToolkit.TabPanel)()
Where t.ID.Contains(idSuffix)
Let id = t.ID.Substring(t.ID.LastIndexOf(idSuffix) + 1)
Where id = tabID
Select t).First
Me.TabContainer1.Tabs.Remove(tab)
Me.CreatedTabIDs.Remove(tabID)
Me.UpdTabContainer.Update()
End Sub
Protected Sub UserChanged(sender As Object, e As EventArgs)
Dim userName = DirectCast(sender, ListBox).SelectedValue
If Not CreatedTabIDs.Contains(userName) Then
AddTab(userName)
CreatedTabIDs.Add(userName)
Me.UpdTabContainer.Update()
End If
End Sub
Private Sub chatSubmitted(chat As ChatControl)
' do something '
End Sub
Private Sub chatClosed(chat As ChatControl)
RemoveTab(chat.UserName)
End Sub
End Class
The Chat-UserControl:
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="ChatControl.ascx.vb" Inherits="WebApplication1.ChatControl" %>
<asp:UpdatePanel ID="UpdChatControl" runat="server" UpdateMode="Conditional">
<ContentTemplate>
Username: <asp:Label ID="LblUserName" runat="server" Text="0"></asp:Label>
<br /><asp:TextBox ID="TxtChat" runat="server" TextMode="MultiLine" Rows="4"></asp:TextBox>
<br /><asp:Button ID="BtnSend" runat="server" Text="Send" />
<br /><asp:Button ID="BtnClose" runat="server" Text="Close" />
</ContentTemplate>
</asp:UpdatePanel>
Codebehind:
Public Class ChatControl
Inherits System.Web.UI.UserControl
Public Event ChatSubmitted(sender As ChatControl)
Public Event ChatClosed(sender As ChatControl)
Public Property UserName As String
Get
Return Me.LblUserName.Text
End Get
Set(value As String)
Me.LblUserName.Text = value
End Set
End Property
Public Property ChatText As String
Get
Return Me.TxtChat.Text
End Get
Set(value As String)
Me.TxtChat.Text = value
End Set
End Property
Private Sub BtnSend_Click(sender As Object, e As System.EventArgs) Handles BtnSend.Click
RaiseEvent ChatSubmitted(Me)
Me.UpdChatControl.Update()
End Sub
Private Sub BtnClose_Click(sender As Object, e As System.EventArgs) Handles BtnClose.Click
RaiseEvent ChatClosed(Me)
Me.UpdChatControl.Update()
End Sub
End Class
If you have further question, ask.