I have a page that queries a DB and displays information for a number of printers. I have several sections that I would like to update at different intervals client-side using AJAX (at least this is best solution my research has come up with). I can get AJAX itself to work with the common "Show the time" example without any issues.
My problem is that the IDs if the sections I want to update (I've tried span, asp:label, div...> are generated with the server side code and ends up something like "Label1", "Label2"...
Every time I try to update the text inside one of these generated labels I get "'Label1' is not declared. It may be inaccessible due to its protection level". Everything is within a form tag, everything is "runat="server"". I feel like I'm just missing a setting, or a reference to something. So far I haven't been able to narrow my searching enough to get a useful answer.
I've lurked around here for years and have always been able to find a solution. This has me stumped though.
---- Example -----
The end goal is to have better control of when and how specific elements are updated. This is using the Timer tutorial demo. In my application I would be assigning IDs for the labels based on a record ID from a query (i.e. ID = "Mach" & MID)
The original code that works as posted:
<form id="form1" runat="server">
<div>
<!-- Ajax button code start -->
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Ajax - Button" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<!-- Ajax button code end -->
<br /><br />
<!-- Non - Ajax button code start -->
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button2" runat="server" Text="Non - Ajax - Button" onclick="Button2_Click" />
<!-- Non - Ajax button code end -->
</div>
</form>
And the Code Behind
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = DateTime.Now.ToString()
Label2.Text = DateTime.Now.ToString()
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = DateTime.Now.ToString()
Label2.Text = DateTime.Now.ToString()
End Sub
It seems to me the follow should work the same way:
<form id="form1" runat="server">
<div>
<% Call Generated()%>
</div>
</form>
Code Behind:
Public Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = DateTime.Now.ToString()
Label2.Text = DateTime.Now.ToString()
End Sub
Public Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = DateTime.Now.ToString()
Label2.Text = DateTime.Now.ToString()
End Sub
Public Sub Generated()
Dim str As String
str = "<div>"
str = str & ("<asp:ScriptManager ID=""ScriptManager1"" runat=""server"" />")
str = str & ("<asp:UpdatePanel ID=""UpdatePanel1"" runat=""server"">")
str = str & ("<ContentTemplate>")
str = str & ("<asp:Label ID=""Label1"" runat=""server"" Text=""Label""></asp:Label>")
str = str & ("<asp:Button ID=""Button1"" runat=""server"" Text=""Ajax - Button"" onclick=""Button1_Click"" />")
str = str & ("</ContentTemplate>")
str = str & ("</asp:UpdatePanel>")
str = str & ("<br /><br />")
str = str & ("<asp:Label ID=""Label2"" runat=""server"" Text=""Label""></asp:Label>")
str = str & ("<asp:Button ID=""Button2"" runat=""server"" Text=""Non - Ajax - Button"" onclick=""Button2_Click"" />")
str = str & ("</div>")
End Sub
Related
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 & ";"
I can't seem to find anything related to the problem I'm encountering.
GOAL
I'm trying to get the value of a custom data attribute from the button (Web Control) that I created in my code-behind when the button is clicked. I'm referencing MSDN-Attribute.GetCustomAttribute Method but I'm not sure if this is appropriate for what I'm trying to do.
My current click sub is below but does not function.
Default.ascx
<asp:UpdatePanel ID="itemWrapper" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:PlaceHolder ID="itemsPlaceholder" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Default.ascx.vb (Code-behind)
Protected Sub Items_Load()
Dim dynamicID = "13"
Dim itemBtn = New Button()
itemBtn.Attributes.Add("data-custom-id", dynamicID) '<- Custom attribute I want to get
itemBtn.UseSubmitBehavior = False
itemBtn.Text = "select"
itemBtn.ID = "Button_" & dynamicID
AddHandler itemBtn.Click, AddressOf ItemSelectBtn_Click
itemsPlaceholder.Controls.Add(itemBtn)
End Sub
Public Sub ItemSelectBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim clickedBtn = DirectCast(sender, Button),
clickedBtnID As String = clickedBtn.Attributes("data-custom-id")
Console.WriteLine("this button's ID is " & clickedBtnID)
End Sub
HTML OUTPUT
<div id="Content_C001_itemWrapper">
<input type="button"
name="ctl00$Content$C001$1"
value="select"
onclick="target = '_blank';__doPostBack('ctl00$Content$C001$1','')"
data-custom-id="13">
</div>
UPDATE
This was the simplest answer. Not sure why this wasn't working but as #zgood suggested to get the attribute. VB.NET uses Attributes("attribute-name") and not Attributes["attribute-name"] like in C#. Just an FYI.
Thanks for the help!
I'm trying to get an alphabet pager working in GridView. It does work, but only one letter shows up in the repeater that I'm using to show the letters. I've read other posts and looked at my code and it seems like it should be working, but it's not.
Any help would be appreciated.
Here is my html
<asp:Repeater ID="rptAlphabets" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lnkBtn1" runat="server" Text='<%#Eval("Value")%>' Visible='<%# Convert.ToBoolean(Eval("Selected"))%>' OnClick="Alphabet_Click"/>
<asp:Label ID="lblAlpha" runat="server" Text='<%#Eval("Value")%>' Visible='<%# Convert.ToBoolean(Eval("Selected"))%>' />
</ItemTemplate>
</asp:Repeater>
Here is my code behind
Private Sub GenerateAlphabets()
Dim alphabets As New List(Of ListItem)()
Dim alphabet As New ListItem
alphabet.Value = "ALL"
alphabet.Selected = alphabet.Value.Equals(ViewState("CurrentAlphabet"))
alphabets.Add(alphabet)
For i As Integer = 65 To 90
alphabet = New ListItem()
alphabet.Value = [Char].ConvertFromUtf32(i)
alphabet.Selected = alphabet.Value.Equals(ViewState("CurrentAlphabet"))
alphabets.Add(alphabet)
Next
rptAlphabets.DataSource = alphabets
rptAlphabets.DataBind()
End Sub
I'm using most of the code from an aspsnippets method.
EDIT :
I'm calling the GenerateAlphabets from my Page_Load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
ClearMessages()
If Not IsPostBack Then
ViewState("CurrentAlphabet") = "ALL"
Me.GenerateAlphabets()
BindGrids()
BindDropDownListBoxes()
End If
Catch ex As Exception
Me.HandleError(ex)
End Try
End Sub
Change your LinkButton code like this.
<asp:LinkButton ID="lnkBtn1" runat="server"
Text='<%#Eval("Value")%>'
Visible='<%# Convert.ToBoolean(Eval("Selected")) = False %>'
OnClick="Alphabet_Click"/>
Reason: You are hiding all LinkButtons that are not currently selected. You should be showing them instead.
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!
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim lbl_Date As New Label
Dim i as int16
i = 20
While (i < i + 1)
lbl_Date = TryCast(Page.FindControl("lbl_Date" + i.ToString), Label)
lbl_Date = "Value"
i = i + 1
End While
End Sub
This is returning Nothing in lbl_Date variable. If i don't use this in Page_Load, it works just fine. Only in Page_Load it doesn't work. Any Explanation :::: Well what i am trying to do here is . i have 100 label name are lable1 to lable100. I can do like label1 =Value and label2 =Value and so on but i am trying to put in loop so i have less code. just for the Info... This code work but not in Page_Load , I am thinking every one looking to the code but this has to do with page life cycle
<asp:content runat="server" id="leftcontent" contentplaceholderid="LeftSidePanel" xmlns:asp="#unknown">
<div id="mydiv" runat="server">
<asp:linkbutton id="Lnk_Heading1" runat="server"> </asp:linkbutton>
<asp:label id="lbl_Date1" runat="server"> </asp:label>
<asp:label id="lbl_Detail1" runat="server"> </asp:label>
<asp:button id="Button1" runat="server" text="Button" />
</div>
</asp:content>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim lbl_Date As New Label
Dim i as int16 i = 20
While (i < i + 1) lbl_Date = TryCast(Mydiv.FindControl("lbl_Date" + i.ToString), Label)
lbl_Date = "Value"
i = i + 1
End While
End Sub
Problem was , I didn't Used DIV in my aspx page . When i put my code in div and use the reference of the div in codebehind then it work.