Execute ASP.nett codebehind method with Ajax Timer - ajax

I'm trying to get a timer to invoke a codebehind method after 5 seconds, but when the page is being loaded I get
Method 'Protected WithEvents timEndup As System.Web.UI.Timer' does not have a signature compatible with delegate 'Delegate Sub EventHandler(Of System.EventArgs)(sender As Object, e As System.EventArgs)'.
On the page I have
<asp:ScriptManager runat="server" />
<asp:Timer runat="server" id="timEndup" interval="5000" ontick="timEndup" />
and in the codebehind I have
Private Sub timEndup_Tick(sender As Object, e As System.EventArgs) Handles timEndup.Tick
Stop
End Sub
The signature looks correct to me, so what am I doing wrong?

You have already associated to an EventHandler your timEndup’s .Tick Event by: Handles timEndup.Tick in code behind.
So, just remove OnTick attribute by client side, OnTick="timEndup" must be removed
Or remove: Handles timEndup.Tick by code behind and put in the OnTick attribute the real name of EventHandler Sub as timEndup_Tick
So the code becomes:
Client Side:
<asp:ScriptManager runat="server" />
<asp:Timer runat="server" ID="timEndup" Interval="5000" OnTick="timEndup_Tick" />
Server Side:
Protected Sub timEndup_Tick(sender As Object, e As EventArgs)
Debug.WriteLine("Tick ")
End Sub
Or:
Client Side:
<asp:ScriptManager runat="server" />
<asp:Timer runat="server" ID="timEndup" Interval="5000" />
Server Side:
Protected Sub timEndup_Tick(sender As Object, e As EventArgs) Handles timEndup.Tick
Debug.WriteLine("Tick ")
End Sub
Another thing is that the EventHandler Sub must be declared NOT Private

Related

Issue with oncheckchanged in vb.net

I have a page with many gridviews. They are in 2 columns 5 on the left and 5 on the right. They are exactly the same.
The Gridview for Employment has a template for a checkbox on the far left. When this is checked, it fires the following:
Protected Sub ChckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast((CType(sender, Control)).Parent.NamingContainer, GridViewRow)
Dim key As String = GridView5.DataKeys(row.RowIndex).Value.ToString()
Dim Incomeadapter As New DataSet1TableAdapters.IncomeTableAdapter
Dim tblincome As New DataSet1.IncomeDataTable
tblincome = Incomeadapter.GetData(key)
GridView6.DataSource = tblincome
GridView6.DataBind()
Exit Sub
I have debugged this, and this code for some reason I don't understand is LOOPING around! Why does it does this? The first time around it gets the correct info, but then it goes back around and ruins the binding of the gridview to the tableadapter because the wrong ID was put into the call to the tableadapter funcion. Is it possible that OncheckChanged event does not work well with the onclick event? I'm using javascript to make sure that only 1 checkbox can be selected on the gridview on the same checkbox :
<div style="overflow-x: scroll; overflow-y: scroll; height: 100px; width: 444px">
<asp:GridView ID="GridView5" runat="server" DataSourceID="SqlDataSource5" AutoGenerateColumns="False" CellPadding="2" HorizontalAlign="Center" OnRowDataBound="Gridview5_rowdatabound" DataKeyNames="EmpID" RowStyle-Wrap="False" HeaderStyle-Wrap="False" ShowHeader="False" ShowFooter="True" FooterStyle-BackColor="Black">
<FooterStyle BackColor="Black"></FooterStyle>
<HeaderStyle Wrap="False"></HeaderStyle>
<RowStyle HorizontalAlign="Center" />
<Columns>
<asp:TemplateField ItemStyle-Width="25px" HeaderText="">
<ItemTemplate>
<asp:CheckBox ID="ChkSelect" runat="server" onclick="CheckOne(this)" AutoPostBack="True" OnCheckedChanged="ChckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<script type="text/javascript">
function CheckOne(obj) {
var grid = obj.parentNode.parentNode.parentNode;
var inputs = grid.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "checkbox") {
if (obj.checked && inputs[i] != obj && inputs[i].checked) {
inputs[i].checked = false;
}
}
}
}
</script>
Rather than doing a postback, with this checkbox with autopostback set to true, is there a way to bind the income gridview which is below the employment gridview without a postback? The income gridview has a separate sql table which has the ID of the employer table as a column. The idea is when you check a checkbox on gridview5 (employment) the income gridview below changes to show the income items which are reflected of the employer. There can be multiple types of income for each employer. Any advice greatly appreciated.
' CausesValidation="False" />
' />
I was able to fix the "looping" issue by rewriting the code as follows:
Dim key As String = ""
For Each row As GridViewRow In GridView5.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim chkrow As CheckBox = TryCast(row.Cells(0).FindControl("ChkSelect"), CheckBox)
If chkrow.Checked Then
key = GridView5.DataKeys(row.RowIndex).Value.ToString()
End If
End If
Next
'Dim row As GridViewRow = TryCast((CType(sender, Control)).Parent.NamingContainer, GridViewRow)
Dim Incomeadapter As New DataSet1TableAdapters.IncomeTableAdapter
Dim tblincome As New DataSet1.IncomeDataTable
tblincome = Incomeadapter.GetData(key)
GridView6.DataSource = tblincome
GridView6.DataBind()
It is working correctly, however on every postback the page goes up to the top and this is just not acceptable, so I would like to try it with Ajax and update panels. I'm reading up on this now.

using row filter to get specific names from table

There is a code for filtering fields from a query (fname, lname and location) into a DataGridView:
Dim DV As New DataView(dbdataset1)
DV.RowFilter = String.Format("fname like '%" & Me.tbSearch.Text.Trim & "%'")
DataGridView.DataSource = dbdataset1
The filter can find lname, location but not fname:
here is the screen shot of the populated fields
As for the DataGridView element I can tell you're on ASP.NET 2.0 or below. Anyway, I got it working on ASP.NET 4.7 like this:
ASPX file:
<%# 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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
<asp:TextBox ID="tbSearch" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" />
</form>
</body>
</html>
Code behind (VB.Net):
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Private Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load
End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim dbdataset1 As New DataTable
Dim dr As DataRow
dbdataset1.Columns.Add("fname")
For i As Integer = 1 To 3
dr = dbdataset1.NewRow()
dr("fname") = "picture" + i.ToString()
dbdataset1.Rows.Add(dr)
Next
For i As Integer = 1 To 3
dr = dbdataset1.NewRow()
dr("fname") = "document" + i.ToString()
dbdataset1.Rows.Add(dr)
Next
Dim DV As New DataView(dbdataset1)
DV.RowFilter = String.Format("fname like '%" & Me.tbSearch.Text.Trim & "%'")
Me.GridView1.DataSource = DV
Me.GridView1.DataBind()
End Sub
End Class
Just replace my GridView1 with your DataGrdiView.
UPDATE:
So the filter was fixed by the user deleting and adding the Texbox control again into the form.

Setting SelectedValue in a Telerik DropDownList in a RadGrid

I am trying to set the SelectedValue of a RadDropDownList in the EditTemplate of my RadGrid. The DataItemBound event appears to be throwing an error on compilation.
ASP.NET
<telerik:GridTemplateColumn DataField="givenAnswer" HeaderText="givenAnswer" UniqueName="givenAnswer">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "givenAnswer") %>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadDropDownList ID="ddlGivenAnswer" runat="server" OnItemDataBound="ddlGivenAnswer_DataBound">
<Items>
<telerik:DropDownListItem Text="Yes" Value="Yes" />
<telerik:DropDownListItem Text="No" Value="No" />
</Items>
</telerik:RadDropDownList>
</EditItemTemplate>
</telerik:GridTemplateColumn>
C#
protected void ddlGivenAnswer_DataBound(object sender, GridItemEventArgs e)
{
if ((e.Item.IsInEditMode))
{
GridEditFormItem item = (GridEditFormItem)e.Item;
RadDropDownList ddl = (RadDropDownList)item.FindControl("ddlgivenAnswer");
ddl.SelectedValue = (string)DataBinder.Eval(e.Item.DataItem, "givenAnswer").ToString();
}
}
Error
CS0123: No overload for 'ddlGivenAnswer_DataBound' matches delegate 'DropDownListItemEventHandler'
This error is being throw on the telerik:RadDropDownList open tag line in ASP.NET. What am I missing here?
Main Edit:
Error CS0123:
First typing CS0123 in Google show me that you were using wrong parameter for your event. Probably a copypast fail. Delete the even in the aspx and ask to intelisense to create a new one. Or copypast this one.
protected void ddlGivenAnswer_ItemDataBound(object sender, Telerik.Web.UI.DropDownListItemEventArgs e)
ItemDataBound:
ItemDataBound occure when a data is bound in a control.
I'am pretty sure that inline declaration are not going to fire this event.
Minor Misconception:
Why would someone change the Value of a selected element dynamically?
It's like changing the value of a vote without changing the vote him self or the name on the vote.
What you want is to check the rigth item.
To check Waldo in the drop down list :
ddlGivenAnswer.FindItemByValue("Waldo").Selected = true;
To check the right Item:
ddlGivenAnswer.FindItemByValue(
DataBinder.Eval(e.Item.DataItem, "givenAnswer").ToString()
).Selected = true;

Telerik Grid - Get master ID in details view

I have a master/detail grid where I do inserts on both the master and detail views. When I'm in detail mode, I need to obtain the master ID value but I can't seem to obtain the value.
I first obtain the inserteditem in the InsertCommand:
Dim inserteditem As GridDataInsertItem = DirectCast(e.Item, GridDataInsertItem)
And then I obtain an instance of the parent (master) view:
Dim parenttable As GridTableView = inserteditem.OwnerTableView.ParentItem.OwnerTableView
I just can't seem to get to the master key value, however. Anyone know what I should do?
Please try with the below code snippet.
.ASPX
<MasterTableView CommandItemDisplay="Top" DataKeyNames="ID" Name="parent">
<DetailTables>
<telerik:GridTableView Name="Child" DataKeyNames="Name">
</telerik:GridTableView>
</DetailTables>
<Columns>
........
........
</Columns>
</MasterTableView>
.ASPX.VB
Protected Sub RadGrid1_InsertCommand(sender As Object, e As GridCommandEventArgs)
If e.Item.OwnerTableView.Name = "Child" Then
Dim item As GridDataInsertItem = TryCast(e.Item, GridDataInsertItem)
Dim pitem As GridDataItem = TryCast(item.OwnerTableView.ParentItem, GridDataItem)
' Parent Item DataKey Here
Dim strID As String = pitem.GetDataKeyValue("ID").ToString()
End If
End Sub

Using Telerik RadComboBox within a repeater

I have a repeater that contains a Telerik RadComboBox:
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<telerik:RadComboBox ID="rcb" runat="server" EnableLoadOnDemand="true"
AllowCustomText="true" ItemRequestTimeout="1000"
NumberOfItems="10" MarkFirstMatch="false">
</telerik:RadComboBox>
</ItemTemplate>
</asp:Repeater>
In the ItemDataBound event of the Repeater, I am wiring up the ItemsRequested event like this:
private void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e) {
RadComboBox rcb = (RadComboBox)e.Item.FindControl("rcb");
rcb.ItemsRequested += rcb_ItemsRequested;
}
private void rcb_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e) {
// Database call to load items occurs here.
// As configured, this method is never called.
}
Currently, the server-side rcb_ItemsRequested method is never called. I suspect that wiring the ItemsRequested event in the ItemDataBound is problematic, but the problem may lie elsewhere.
Any ideas on how to use the Telerik RadComboBox within a repeater properly?
Have you tried putting the event handler wiring in the markup rather than adding it dynamically?
Also - you are probably aware, but just in case - ItemsRequested is an event that only fires under certain conditions. To quote the docs:
The ItemsRequested event occurs when the EnabledLoadOnDemand property is True and the user types text into the input field or clicks on the drop-down toggle image when the list is empty. - Reference
Does your scenario match the above?
EDIT:
I've tested some code. The following works (The ItemsRequested Event fires for the all ComboBoxes and adds the three test items to the dropdown on the fly..):
Markup:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_ItemDataBound">
<ItemTemplate>
<br />
<telerik:RadComboBox ID="rcb" runat="server" EnableLoadOnDemand="true" AllowCustomText="true"
ItemRequestTimeout="1000" NumberOfItems="10" MarkFirstMatch="false" />
</ItemTemplate>
</asp:Repeater>
</form>
code behind:
protected void Page_Load(object sender, EventArgs e)
{
List<string> data = new List<string>();
data.Add("Item 1");
data.Add("Item 2");
//add some items to the repeater to force it to bind and repeat..
rpt.DataSource = data;
rpt.DataBind();
}
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//wire the event
RadComboBox rcb = (RadComboBox)e.Item.FindControl("rcb");
rcb.ItemsRequested += rcb_ItemsRequested;
}
protected void rcb_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
//add the items when requested.
(sender as RadComboBox).Items.Add(new RadComboBoxItem("Item1", "1"));
(sender as RadComboBox).Items.Add(new RadComboBoxItem("Item2", "2"));
(sender as RadComboBox).Items.Add(new RadComboBoxItem("Item3", "3"));
}

Resources