TextBox TextChange event is not working in vb.net - asp.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;
}

Related

how to change label with button in vb?

so i want to have a button the changes the text of a label when i click it and it says page not available, but when i comment out the update panel it atleast shows up.
<form id="form1" runat="server">
<asp:ScriptManager ID="s1" runat = "server"></asp:ScriptManager>
<div>
<h1>UpDaTe PaNeL tEsT PaGe</h1>
start text : Hello <br />
updated text: World<br />
<hr />
<asp:UpdatePanel ID="p1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Update" OnClick="updateLabel" Height="29px" Width="110px"/> -->
Text: <asp:Label ID="Label1" runat="server" Text="hello"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
codebehind
Public Class aaaa
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Function updatelabel() As String
Label1.Text = "World"
End Function
End Class
The previous answer is correct. The handler for the Label Click event needs the sender as Object and the e as EventArgs parameters. If you do not put them, your updateLabeL() function will not be a valid match for the event handler.
You can remove the OnClick from the button and Try the classic method of handling events.
So your code will be... ( Sorry I'm posting it via app so it might not be in code view )
Protected Sub ChangeLabel(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.text = "TextGoesHere"
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.

General Function

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.

textChanged event doesn´t fire and i have autopostback=true

I have a textbox event declarated but it doesn´t fire. I have seen in SO other answers but all of them say that autopostback property has been true and i have it
my aspx
<asp:ScriptManager ID="ScriptManager2" runat="server" />
<asp:TextBox runat="server" ID="txtDia" Width="120px" Height="20px"
AutoPostBack="True" CssClass="textbox" OnTextChanged="txtDia_TextChanged"/>
<Juice:Datepicker ID="Datepicker2" runat="server" TargetControlID="txtDia"
DateFormat="dd/mm/yy"
MonthNames="Enero,Febrero,Marzo,Abril,Mayo,Junio,Julio,Agosto,Septiembre,Octubre,Noviembre,Diciembre"
MonthNamesShort="Ene,Feb,Mar,Abr,May,Jun,Jul,Ago,Sep,Oct,Nov,Dic,"
AutoPostBack="True" /></td>
and my aspx.vb
Protected Sub txtDia_TextChanged(sender As Object, e As System.EventArgs) Handles txtDia.TextChanged
CargarDatos()
End Sub
You should define your textbox like this (see OnTextChanged="txtDia_TextChanged" being added):
<asp:TextBox OnTextChanged="txtDia_TextChanged"
runat="server" ID="txtDia" Width="120px" Height="20px"
AutoPostBack="True" CssClass="textbox"/>
And remember that this event will rise onblur (focus removed from that textbox) only.
In your aspx you should write
<asp:TextBox runat="server" ID="txtDia" Width="120px" Height="20px"
AutoPostBack="True" CssClass="textbox" OnTextChanged="txtDia_TextChanged"/>
The event is not triggered if you overwrite the text in codebehind. So for example if you databind the control where the TextBox sits in.
You should so that only If Not IsPostBack:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostback)
{
DataBindAllControls(); // including your textbox
}
}
Edit: Sorry, here VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
DataBindAllControls() ' including your textbox
Else
End Sub
What FAngel said is correct.
I've had the same issue when validating dates and using jquery datepicker. I used the below to fire the validation routines after the box had been populated. So technically they fired twice, once after the textbox had lost focus to the datepicker, then again when my code fired after the datepicker had populated the textbox. It would have been better to disable the onblur event and call directly from the below, but wasn't possible in my project.
$('.datePicker').each(function () {
$(this).datepicker({
onSelect: function () {
$(this).trigger('blur');
}
});
});
You can use a variation on this by disabling the auto-postback and manually triggering it via the onSelect event.
You need to specify the method to be called when the event is fired like so:
<asp:TextBox runat="server" ID="txtDia" Width="120px" Height="20px" OnTextChanged="txtDia_TextChanged" AutoPostBack="True" CssClass="textbox"/>
Note:
OnTextChanged="txtDia_TextChanged"

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

Resources