How to add asyncPostBackTrigger to Template column, Item Template, Button in DataGrid - asp.net

I need to add a trigger for two buttons in DataGrid Template columns. I have found a couple postings saying to put the code in the code-behind using the UniqueID.
Something isn't right with my logic (or maybe it isn't in the right place). I am getting "Object reference not set to an instance of an object" error when I run it.
I am getting this on my "gridSelectTrigger.ControlID = btnSessionSelect.UniqueID" statement.
Does this logic need to be in an "ItemDataBound" event? Or is my logic wrong?
<%# Page Title="Admin Session Folders" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="AdminAddEditReleaseAndFiles.aspx.vb" Inherits="AdminAddEditReleaseAndFiles" Theme="Standard" %>
<asp:Panel ID="pnlEditTopic" runat="server" CssClass="modalPopupEditTopic" Style="display: none;" >
<table cellspacing="0" class="borderTable0" width="100%" style="">
<tr style="height:4px">
<td colspan="6" align="center">
<asp:ImageButton ID="btnAddTopic" runat="server" AlternateText="Add Topic"
ImageUrl="~/App_Themes/Common/images/BtnApply.jpg" Height="28px">
</asp:ImageButton>
<asp:ImageButton ID="btnUpdateTopic" runat="server" AlternateText="Update Topic"
ImageUrl="~/App_Themes/Common/images/BtnApply.jpg" Height="28px">
</asp:ImageButton>
<asp:ImageButton ID="btnDeleteTopic" runat="server" AlternateText="Delete Topic"
ImageUrl="~/App_Themes/Common/images/BtnDelete.jpg" Height="28px">
</asp:ImageButton>
<asp:ImageButton ID="btnEditTopicClose" runat="server" AlternateText="Close Edit Topic Popup"
ImageUrl="~/App_Themes/Common/images/BtnCancel.jpg" Height="28px">
</asp:ImageButton>
</td>
</tr>
</table>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not (IsPostBack) Then
Dim MainContent As ContentPlaceHolder = TryCast(Page.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
Dim UpdatePanelSessions As UpdatePanel = TryCast(MainContent.FindControl("UpdatePanelSessions"), UpdatePanel)
Dim btnSessionSelect As Button = TryCast(UpdatePanelSessions.FindControl("btnSessionSelect"), Button)
Dim btnSessionDetail As Button = TryCast(UpdatePanelSessions.FindControl("btnSessionDetail"), Button)
Dim gridSelectTrigger As AsyncPostBackTrigger = New AsyncPostBackTrigger
Dim gridDetailTrigger As AsyncPostBackTrigger = New AsyncPostBackTrigger
gridSelectTrigger.ControlID = btnSessionSelect.UniqueID
gridSelectTrigger.EventName = "Click"
UpdatePanelSessions.Triggers.Add(gridSelectTrigger)
gridDetailTrigger.ControlID = btnSessionDetail.UniqueID
gridDetailTrigger.EventName = "Click"
UpdatePanelSessions.Triggers.Add(gridDetailTrigger)
End If
End Sub
Thank you,
James

Got it. The update panel needed children as triggers set to true.

Related

Pass Text From a List View Controller to a OnClick Sub VB.Net

Is there a way to pass text from a List View controller to a OnClick Subroutine? For example, there's a label in the List View and I've attached a Button in the List View and attached an OnClick Subroutine to it. Now I want to pass the text from the Label over to the OnClick Subroutine. I hope this make sense. Down below is my code:
VB.Net Code:
Protected Sub AmButtonClick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim url As String = CType(Me.AmListViewDetails.FindControl("merchantLink"), Label).Text
Dim sb As New StringBuilder()
sb.Append("<script type = 'text/javascript'>"_
sb.Append("window.open('")
sb.Append(url)
sb.Append("');")
sb.Append("</script>")
ClientScript.RegisterStartupScript(Me.GetType(), "script", sb.ToString())
End Sub
ASP.Net:
<asp:ListView runtat="server" ID="AmListViewDetails" ...>
<ItemTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<asp:Label ID="merchantLink" runat="server" Text='<%Eval("Link")%>' />
</td>
</tr>
<tr runat="server">
<td runat="server">
<asp:Button ID="AmBtn" runat="server" Text="Checkout" OnClick="AmButtonClick"/>
</td>
</tr>
</table>
</ItemTemplate>
</asp:ListView>
Add a CommandArgument property to the button. That's going to be the value passed to the event handler. You don't need an OnClick property.
<asp:Button ID="AmBtn" runat="server" Text="Checkout" CommandArgument="???" />
Your event handler would be
Sub AmListViewDetails_ItemCommand(object sender, ListViewCommandEventArgs e)
Dim valuePassedFromListView = Cstr(e.CommandArgument)
End Sub

ItemCommand of DataList in UpdatePanel is not fired on Postback

I have a popup panel which contains an UpdatePanel, which contains a DataList. Table rows are populated using ItemTemplate and there is a LinkButton generated on each row for deleting this row. I would like to delete this record in the DataList's ItemCommand event handler and rebind the DataList.
However, after I click a "delete" button in the DataList, ItemCommand is not fired. I've already checked if IsPostBack in my Page_Load and only do Datalist.Databind() if it's not a postback. Normally I would expect first Page_Load and then list_ItemCommand being called after I click a delete button in the DataList, but list_ItemCommand is not called as expected. And nothing is then displayed in DataList which is inside the UpdatePanel.
And stranger, if I remove the IsPostBack check in Page_Load, that being said, rebind the DataList in every Page_Load, ItemCommand will be caught and list_ItemCommand is called. This is against the answers in many other posts "ItemCommand event will be canceled if DataList is rebinded during PostBack".
Code behind:
Protected records As New List(Of Record)
Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Set some page properties...
If Not Page.IsPostBack Then
GetListOfRecordFromDatabase()
datalist.DataSource = records
datalist.DataBind()
End If
End Sub
Protected Sub datalist_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles datalist.ItemCommand
Select Case e.CommandName.ToLower
Case "delete"
For Each c As Record In records
If c.Id = e.CommandArgument Then
records.Remove(c)
Exit For
End If
Next
DeleteRecordFromDatabase(e.CommandArgument)
datalist.DataSource = records
datalist.DataBind()
End Select
End Sub
Controls:
<asp:Content ID="content1" runat="server" ContentPlaceHolderID="Content1PlaceHolder">
<asp:LinkButton ID="btnpopup" runat="server" OnClientClick="javascript:return popup()"></asp:LinkButton>
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server" EnableViewState="false" >
</asp:ScriptManagerProxy>
<asp:Panel ID="PanelPopup" runat="server" style="display:none;">
<asp:UpdatePanel ID="UPPopup" runat="server" UpdateMode="conditional" EnableViewState="false">
<ContentTemplate>
<div id="divPopup1" runat="server">
<table id="table1" cellpadding="2" cellspacing="1" width="500" border="0" >
<asp:DataList ID="datalist" runat="server" OnItemCommand="datalist_ItemCommand">
<ItemTemplate>
<tr align="center">
<td><%#Container.ItemIndex +1 %></td>
<td><asp:Label ID="Label1" runat="server" Text='<%# eval("Name") %>'></asp:Label></td>
<td><asp:Label ID="Label2" runat="server" Text='<%# eval("Color") %>'></asp:Label></td>
<td><asp:LinkButton ID="Delete" CommandName="Delete" runat="server" Text="Delete" CommandArgument='<%# eval("Id") %>' ></asp:LinkButton></td>
</tr>
</ItemTemplate>
</asp:DataList>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<div style="text-align:center;"><br />
<asp:Button ID="BtnSavePopup" runat="server" Text="Save and Close"/>
</div>
</asp:Panel>
<script type="text/javascript">
function popup() {
return mypagehelper.openAsModalPopup("<% =PanelPopup.ClientID%>");
}
</script>
</asp:Content>
Further more, I tried to grab the ControlID and the Control who raised the event during Postback using this code:
If IsPostBack Then
Dim CtrlID As String = String.Empty
If Request.Form("__EVENTTARGET") IsNot Nothing And
Request.Form("__EVENTTARGET") <> String.Empty Then
CtrlID = Request.Form("__EVENTTARGET")
Dim postbackControl As System.Web.UI.Control = Page.FindControl(CtrlID)
Else
End If
And I found that I can get my CtrlID as "ctl00$datalist$ctl08$Delete" but the postbackControl is Nothing. While on my other normal pages I can get both the controlID and actual control(which is a LinkButton) who raised the event.
Remove EnableViewState="false" from update panel.
it's late answer but i faced in soon, also may be it help some one else ...
the CommandName="Delete" is reserved for DataList "OnItemDeleting" Event
so just changing the CommandName to anything else (for example "Remove") will help or handle the OnItemDeleting Event.

TextBox TextChange event is not working in vb.net

I am using asp textbox control with TextChange event, trying to set the text to label as the textbox text get changed but it is working when i loose the focus from textbox not when text changes.
the problem with loosing focus is that if i use AutoPostBack="true" it refresh the page which affects my other values...
my source for textbox on aspx page
<asp:TextBox ID="txtSearchCust" runat="server" Width = "200" OnTextChanged="txtSearchCust_TextChanged" AutoPostBack="true" />
my code behind page source
Protected Sub txtSearchCust_TextChanged(sender As Object, e As EventArgs) Handles txtSearchCust.TextChanged
QtyChk.Text=txtSearchCust.Text
End Sub
here QtyChk is the label where I am trying to set the text
any solution...?
Based on your question.
<script type="text/javascript">
function doTextBoxChange() {
__doPostBack();
}
</script>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" onkeydown="return doTextBoxChange();"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack() Then
LabelChange()
End If
End Sub
Sub LabelChange()
Label1.Text = "1234"
End Sub
But I think use javascript is a better option.
The values will disappear after post, you need to request the value and setup.
otherwise, use viewstate or session.
Using the javascript, don't post form data.
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" onkeyup="return doTextBoxChange();"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</form>
function doTextBoxChange() {
var inputValue = document.getElementById("TextBox1").value;
document.getElementById('<%= Label1.ClientID %>').innerHTML = inputValue;
}

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

ASP / ASP.net postback problems With VB.net 2010

I'm working on a web app and i have the vb.net code working in the web form to only load setting in my controls once. but I'm using a google map control and every time my timer runs that control reloads it setting and wipes out any data i push in. i think i need some help in the asp.net/html side to stop on post back reloading the control.
here is my asp.net/html code.
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table style="width:100%;">
<tr>
<td style="width: 668px">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<artem:GoogleMap ID="GoogleMap1" runat="server"
DefaultAddress="999 main st, new york NY, 10541"
EnableMapTypeControl="True" EnableOverviewMapControl="False"
EnableReverseGeocoding="True" EnableStreetViewControl="False" MapType="Roadmap"
MaxZoom="25" MinZoom="12" ShowTraffic="False" Zoom="18" Height="380px"
Width="550px" Key="13123123132132132132132123" >
</artem:GoogleMap>
<br />
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Button" />
<br />
<br />
</td>
</tr>
<tr>
<td style="width: 668px">
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Timer ID="tmUpdateAlarms" runat="server" Enabled="False" Interval="30000">
</asp:Timer>
</td>
</tr>
</table>
My Timer Code
Protected Sub tmUpdateAlarms_Tick(sender As Object, e As System.EventArgs) Handles tmUpdateAlarms.Tick
tmUpdateAlarms.Enabled = False
Try
Dim lqFireFighterconnect As New lqFireFighterConnectDataContext
Dim lqGetAlarms As New lqAlarmAndGoDataContext
Dim getDeptGUID = From r In lqFireFighterconnect.tbDeptToPublics
Where r.PublicGUID = Request.QueryString("PUBGUID")
Select r
If getDeptGUID.Count = 0 Then
Exit Sub
End If
For Each foundGUID In getDeptGUID.Take(1)
gotDeptGUID = foundGUID.DeptGUID
Next
Dim GetAlarms = From r In lqGetAlarms.AlarmDrops
Where r.DeptGUID = gotDeptGUID
Order By r.TimeDate Descending
Select r
If GetAlarms.Count = 0 Then
Exit Sub
End If
Dim myCBTable As New DataTable()
With myCBTable.Columns
.Add("DateTime", GetType(String))
.Add("Address", GetType(String))
.Add("AlarmType", GetType(String))
.Add("CrossStreet", GetType(String))
.Add("Status", GetType(String)) '<<<< change the type of this column to what you actually need instead of integer.
End With
For Each FoundAlarm In GetAlarms.Take(1)
If Not GridView1.Rows(0).Cells(1).Text.ToString = FoundAlarm.AlarmAddress Then
For Each updateAlarm In GetAlarms.Take(3)
myCBTable.Rows.Add(updateAlarm.TimeDate.ToString, updateAlarm.AlarmAddress, updateAlarm.AlarmType, updateAlarm.AlarmCrossStreets, updateAlarm.AlarmStatus)
Next
With GridView1
.DataSource = myCBTable
.DataBind()
End With
Dim objmar As New Artem.Google.UI.Marker
objmar.Address = GridView1.Rows(0).Cells(1).Text.ToString
'objmar.Animation = Artem.Google.UI.MarkerAnimation.Drop
objmar.Visible = True
objmar.Title = GridView1.Rows(0).Cells(1).Text.ToString
objmar.Icon = "/Images/fire_c.png"
GoogleMap1.Markers.Add(objmar)
objmar = Nothing
GoogleMap1.Address = GridView1.Rows(0).Cells(1).Text.ToString
End If
Next
' LoadHydrants()
Catch ex As Exception
End Try
tmUpdateAlarms.Enabled = True
End Sub
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<artem:GoogleMap ID="GoogleMap1" runat="server"
DefaultAddress="999 main st, New York ny, 10541"
EnableMapTypeControl="True" EnableOverviewMapControl="False"
EnableReverseGeocoding="True" EnableStreetViewControl="False" MapType="Roadmap"
MaxZoom="25" MinZoom="12" ShowTraffic="False" Zoom="18" Height="380px"
Width="550px" Key="1231231231231231231231321231313213" >
</artem:GoogleMap>
</ContentTemplate>
</asp:UpdatePanel>

Resources