Listbox Selection Change Event Selected Index - asp.net

I am new to web building. I have the following listbox on the page. The page is enabled view state.
<asp:ListBox ID="ExamsList_ListBox" runat="server" DataTextField="Namee" viewstate="enabled"
DataValueField="ID" AutoPostBack="true" Height="213px" Width="152px"
ViewStateMode="Enabled" />
The data is data bind at run time. I ma able to see the list, but the listbox.selectedindex always results in "-1" value only even though I click the 10th one in box. could you please tell me what is wrong.
Here is the page code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body >
<form id="form1" runat="server">
<div>
<div>
<asp:ListBox ID="ExamsList_ListBox" runat="server" DataTextField="Namee" viewstate="enabled"
DataValueField="ID" AutoPostBack="true" Height="213px" Width="152px"
ViewStateMode="Enabled" />
</div>
</div>
</form>
</body>
</html>
and the code for filling in data is :
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsNothing(CType(Session("Login"), TikoClasses.Login)) Then
Response.Redirect("~/default.aspx")
ElseIf (CType(Session("Login"), TikoClasses.Login)).Admin = False Then
Response.Redirect("~/Loggedin/Welcome.aspx")
End If
ExamsList_ListBox.DataSource = DataModule.Exams_listall((CType(Session("Login"), TikoClasses.Login)).Inst_ID)
ExamsList_ListBox.DataBind()
End Sub
and selection changed even is:
Try
Dim k As Integer = ExamsList_ListBox.SelectedIndex
Dim tt As List(Of Integer) = ExamsList_ListBox.GetSelectedIndices.ToList
Dim t As Object = ExamsList_ListBox.SelectedValue
If ExamsList_ListBox.SelectedIndex > -1 Then
DataModule.GetExam(CType(Session("Login"), TikoClasses.Login).Inst_ID, ExamsList_ListBox.SelectedValue)
End If
Catch ex As Exception
End Try
Looking for help. Thanks in advance.

You need to put your bind code under !IsPostBack
if(!IsPostBack)
ExamsList_ListBox.DataSource = DataModule.Exams_listall((CType(Session("Login"), TikoClasses.Login)).Inst_ID)
ExamsList_ListBox.DataBind()
Endif
Since whenever your selection is Changed, your page_load event fired first and your selection is lost.

Related

ASP VB Add to textbox

I have a asp vb page that holds store hours. I'm supposed to add a textbox that has a list of people's emails in it. The emails come from a database, someone else wrote the code for that. But I need to be able to fill a textbox with these emails and have the textbox editable so someone can change or add new emails. So how do you get it so the textbox automatically fills up with the emails? I have a textbox on the asp page. And I have some code on the vb page to try to fill it but nothing shows up in textbox. Please help, I'm very new at this!
Asp:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="label1" Text="List Of Emails" runat="server" /> <br />
<asp:TextBox ID="emailBox" TextMode="MultiLine" runat="server" />
<br/><br/>
<asp:Button Text="Save" runat="server" />
<asp:Button Text="Cancel" runat="server" />
</div>
</form>
</body>
</html>
VB:
Partial Class KioskEmailUpdate
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Dim tempstoreID As Integer = 101
Dim ds As DataSet
Dim emailList As String = ""
ds = DBUtility.GetKisokAlertEmails(tempstoreID)
If Not ds Is Nothing AndAlso ds.Tables.Count > 0 And ds.Tables(0).Rows.Count > 0 Then
For Each dr In ds.Tables(0).Rows
emailList = dr("EmailList")
Next
End If
Dim seperatedEmailList() As String = Split(emailList, ";")
Dim counter As Integer = 0
For Each email In seperatedEmailList
emailBox.Text = emailBox.Text + email
counter = counter + 1
Next
Catch ex As Exception
Dim tempstoreID As Integer = 101
LogUtility.LogMessage("Error in getting and parsing email list from database. " & ex.ToString, "I", tempstoreID)
End Try
End Sub
End Class
You keep overwriting the variable emailList. Therefore, when you attempt to split it, you will only get one email address.
Change:
emailList = dr("EmailList")
To:
emailList &= dr("EmailList").ToString & ";"

Print different document using different buttons

I am using the the Neodynamic SDK to print documents to the client side.
We will have 5 documents to print. I see how to print either 1 document or print all documents, but is there a way to print 1 document per button. I.e. button1 prints out doc1, button2 prints doc2, etc.
Here is what I've got so far
<script runat="server">
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
fileone()
filetwo()
End Sub
Public Sub fileone()
Dim fileToPrint As New PrintFile(Context.Server.MapPath("~/forms/xmlheader.txt"), "xmlheader.txt")
If (WebClientPrint.ProcessPrintJob(Request)) Then
'Create a ClientPrintJob
Dim cpj As New ClientPrintJob()
'set client printer, for multiple files use DefaultPrinter...
cpj.ClientPrinter = New DefaultPrinter()
'set files-printers group by using special formatting!!!
'Invoice.doc PRINT TO Printer1
cpj.PrintFile = fileToPrint
'send it...
cpj.SendToClient(Response)
End If
End Sub
Public Sub filetwo()
Dim fileToPrint As New PrintFile(Context.Server.MapPath("~/forms/ How To Recover Office Doc.pdf"), " How To Recover Office Doc.pdf")
If (WebClientPrint.ProcessPrintJob(Request)) Then
'Create a ClientPrintJob
Dim cpj As New ClientPrintJob()
'set client printer, for multiple files use DefaultPrinter...
cpj.ClientPrinter = New DefaultPrinter()
'set files-printers group by using special formatting!!!
'Invoice.doc PRINT TO Printer1
cpj.PrintFile = fileToPrint
'send it...
cpj.SendToClient(Response)
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
fileone()
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "printForm1", "printForm1();", True)
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
filetwo()
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "printForm2", "printForm2();", True)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to print multiple files to client printers from ASP.NET</title>
<script type="text/javascript">
function printForm1() {
jsWebClientPrint.print(fileone());
}
function printForm2() {
jsWebClientPrint.print(filetwo());
}
</script>
</head>
<body>
<%-- Store User's SessionId --%>
<input type="hidden" id="sid" name="sid" value="<%=Session.SessionID%>" />
<form id="form1" runat="server">
<h1>How to print multiple files to client printers from ASP.NET</h1>
Please change the source code to match your printer names and files to test it locally
<br /><br />
<%-- Add Reference to jQuery at Google CDN --%><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script><%-- Register the WebClientPrint script code --%><%=Neodynamic.SDK.Web.WebClientPrint.CreateScript()%>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button"
Height="156px" Width="156px" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" Height="156px" Width="156px"/>
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Button"
Height="156px" Width="156px"/>
<asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="Button"
Height="156px" Width="156px"/>
<asp:Button ID="Button5" runat="server" onclick="Button5_Click" Text="Button"
Height="156px" Width="156px"/>
</form>
</body>
</html>
I thought of changing filetwo() variable to clientprintjob cpj2...
What you need is to make a function called PrintDoc() that receives the path of the file to print as a parameter. The signature should look like this:
Private Sub PrintDoc(path as String)
End Sub
You only need this function once and no need for sub fileone() and sub filetwo() ... up to filefive(). You just need this one sub called PrintDoc(path as String)
Then you need 5 buttons, each with their own _Click() event and in that event handler you call the PrintDoc procedure by passing a different param everytime.
Hope this helps.
Good luck!

Programmatically set OnSelectedIndexChanged for a DropDownList in VB.NET

I have tried below code but it didn't work:
DropDownList1.SelectedIndexChanged += new EventHandler ( doSub );
It is in C# not VB
When I type DropDownList1. intellisense doesn't bring the SelectedIndexChanged method
And last it complains about SelectedIndexChanged is an event and must be called by RaiseEvent
I'm trying to make ddls dynamically based on SelectedValue of each other, so I need to set the OnSelectedIndexChanged values programmatically instead of declarativly like this:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DoSub">
</asp:DropDownList>
Thanks in advance.
Tip: Remove the AutoEventWireup="false" in #Page section to get solution to work.
Use AddHandler to attach an event handler.
Syntax:
AddHandler obj.EventName, AddressOf HandlerName
Example:
<script runat="server">
Protected Sub Page_Load(sender As Object, e As System.EventArgs)
AddHandler DropDownList1.SelectedIndexChanged, AddressOf DoSub
End Sub
Sub DoSub(sender As Object, e As System.EventArgs)
Response.Write(DropDownList1.SelectedValue)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem>Foo</asp:ListItem>
<asp:ListItem>Bar</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
AddHandler DropDownList1.SelectedIndexChanged, AddressOf FunctionName

i wanna do it without autopost back i create a toggle button using asp.net image button !

i want to do it without page refresh .....
Protected Sub s1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles s1.Click
If s1.ImageUrl = "~/selected.gif" Then
s1.ImageUrl = "~/available.gif"
TextBox1.Text = TextBox1.Text.Replace("1", "")
ElseIf s1.ImageUrl = "~/available.gif" Then
s1.ImageUrl = "~/selected.gif"
TextBox1.Text = TextBox1.Text.ToString() & "," & "1"
End If
End Sub
I think you can use UpadatePanel. Example (assuming your controls are not scattered):
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Image ID="imgToggleImage" runat="server" />
<asp:Button ID="s1" runat="server" Text="Button"/>
</ContentTemplate>
</asp:UpdatePanel>
And then can have the required code in code behind.
If I am not wrong, you want to change image of s1 button when s1 is clicked and change some textbox contents. If you want to do it without postback, use javascript.
See an example here to get you started:
http://www.toknowmore.net/e/1/javascript/change-image-onclick.php#

How do I apply AddHandler using OnCommand in ASP.NET

I've been racking my brain trying to get this to work. My event for my LinkButton isn't firing. I'm figuring that it has SOMETHING to do with ViewState and that the button is not there when it tries to fire the event after the Postback or something.
When I click the Add button,it adds the link to the page and then when I click the Diplay Time" linkbutton it should fire the event and display the CommandArgument data but it's not and i can't figure out why.
Here's my code:
<%# Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub
Protected Sub btnDelete_OnCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
Response.Write(e.CommandArgument)
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btn As New LinkButton()
btn.ID = "lbn"
btn.Text = "Display Time"
btn.ValidationGroup = "vgDeleteSigner"
AddHandler btn.Command, AddressOf btnDelete_OnCommand
btn.CommandArgument = Now.TimeOfDay.ToString()
btn.EnableViewState = True
Panel1.Controls.Add(btn)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</div>
</form>
</body>
</html>
The reason that's happening is that your dynamic button "lbn" needs to be drawn again on post back when it's clicked because the button doesn't exist after you click it.
Basically you just have to dynamically add the button to the page again on post back of click of that button.
I would recommend having the button already on the page but visible = false and then just showing it when you click the other button.

Resources