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"
Related
I have two dropdownlists:
First one:
<asp:DropDownList ID="Drddl" class="form-control form-control-sm" Height="30" runat="server"
Width="350" Enabled="False" AppendDataBoundItems="true" AutoPostBack="true" DataTextField="FullName" DataValueField="Name">
</asp:DropDownList>
and gets filled on page load :
If Not Page.IsPostBack Then
FillDrData()
End If
The second:
<asp:DropDownList ID="Drddl" class="form-control form-control-sm" Height="30" runat="server" Width="350" Enabled="False" AppendDataBoundItems="true" AutoPostBack="true" DataTextField="FullName" DataValueField="Name"></asp:DropDownList>
and gets filled on SelectedIndexChanged of first one:
Private Sub Drddl_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Drddl.SelectedIndexChanged
FillAgenceData()
End Sub
My question is:
Everything is working fine until I click submit button that read selected items from both dropdownlists and save it in SQL database. The Agenciesddl selecteditem always return to 1.
Be mindful of the postbacks of each dropdownlist. Quick and dirty way would be to set the selected values in ViewState or Session variables then get the variables on Submit. See below for rough example (pardon the c#)
Private void Drddl_SelectedIndexChanged(sender As Object, e As EventArgs)
{
ViewState["DrddlVal"] = Drddl.SelctedValue;
}
private void submit(sender As Object, e As EventArgs)
{
string dbparmval = ViewState["DrddlVal"].ToString();
}
As usual the answer is very simple.
the DataValueField property of DropDownList must be UNIQUE
when SelectedIndexChanged event triggered, the DropDownList goes to first value if he find more than one item has same value.
thanks for this article:
enter link description here
thanks everyone..
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;
}
I have the following situation:
.ascx:
<script type="text/javascript">
$(document).ready(function () {
$('.onoffswitch-checkbox').change(function () {
if ($(this).is(":checked"))
$('#hf').val(1);
else
$('#hf').val(0);
});
});
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="hf"/>
</Triggers>
<ContentTemplate>
<input type="checkbox" class="onoffswitch-checkbox">
<asp:HiddenField ID="hf" runat="server" Value="0"/>
<asp:Label ID="label" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
.vb:
Protected Sub hf_ValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles hf.ValueChanged
label.Text = "something..."
End Sub
JS function changes the value of HiddenField after the CheckBox is checked or unchecked, but the ValueChaneged-Event of the HiddenField doesn't fire. I tryed with asp:PostBackTrigger - also no effect, can you see the error?
EDIT:
i tried also to declare the method in the control:
.ascx:
<asp:HiddenField ID="hf" runat="server" Value="0" OnValueChanged="hf_ValueChanged"/>
.vb
Protected Sub hf_ValueChanged(ByVal sender As Object, ByVal e As EventArgs)
label.Text = "something"
End Sub
no effect.
UpdatePanel will not work when you have Response.Write("") on event method. to change value of elements in UpdatePanel, try to use something like this:
hf.Value="something";
dear you need to add the OnValueChanged attribute in Hidden Field and declare the method name, then it will fire click event.
<asp:HiddenField ID="hf" runat="server" Value="0" OnValueChanged="hf_ValueChanged"/>
void hf_ValueChanged(Object sender, EventArgs e)
{
// Display the value of the HiddenField control.
string _strHDFValue = hf.Value;
Response.Write("something...")
}
Good Luck.
So, after 2 days experimentation i found the following workaround. It is not very pretty, but it works like I want.
I am not sure, but i think the JS changes the value of the hidden field only on the client site and the server doesn't see the changes and so is the event not fired.
at first I called manually a postback from js:
function changeHFValue(element) {
if ($(element).is(":checked"))
$('#<%= hf.ClientID %>').val(1);
else
$('#<%= hf.ClientID %>').val(0);
__doPostBack('UpdatePanel1', ''); //postback
}
ascx:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always" EnableViewState="true">
<ContentTemplate>
<asp:HiddenField ID="hf" runat="server"/>
<input type='checkbox' onchange='changeHFValue(this);' id='cb'/>
<asp:Label ID="label" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
now is the event fired because of postback, it goes in the method in .vb:
Protected Sub hf_ValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles hf.ValueChanged
If hf.Value Then
label.Text = "something"
Else
label.Text = "something else"
End If
End Sub
the new problem was that the checkbox was always unchecked after postback, when the UpdatePanel was reloaded. The solution is to look at the HiddenField:
$(document).ready(function () {
// bind your events here initially
bind();
});
var prm = Sys.WebForms.PageRequestManager.getInstance(); // Page request manager
prm.add_endRequest(function () {
// re-bind your events here after postback
bind();
});
function bind() {
if ($('#<%= hf.ClientID %>').val() == 1)
$('#cb').prop('checked', 'checked');
else
$('#cb').prop('checked', '');
}
if not works after, add this attribute to UpdatePanel tag:
ChildrenAsTriggers="true"
See your Page_Load if you have any Response.*, if then, you should avoid it when load caused by UpdatePanel.
i have a User Control.
In this user control I have a Repeater.
In this repeater I have again a User Control.
If I try to catch the click event of an ImageButton in this user control, I'm gettin nothing because the event has not been fired.
Is there a way to catch this event?
some code:
first user control:
<asp:Repeater ID="Rpt" runat="server">
<ItemTemplate>
<uc1:myUserControl id="myUserControl1" runat="server" />
</ItemTemplate>
</asp:Repeater>
in myUserControl
<asp:LinkButton ID="myUserControlBtn" runat="server" OnClientClick="thisEventIsFiring();" OnClick="btn_Click" Text="btn" />
This event isn't firing:
Protected Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
' I am not firing
End Sub
Also the following does not work because it returns nothing:
If Request(btn.UniqueID) IsNot Nothing
'check if Button was clicked
End If
It should be
<asp:LinkButton ID="myUserControlBtn" runat="server" OnClick="btn_Click" Text="btn" />
Here the code behind... I'm trying to retrieve this control so I can add items to the drop down list (I'm retrieving the Role Groups to add to the drop down list in the code-behind)
Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim DDRoleGroups As DropDownList
DDRoleGroups = FormView1.FindControl("DDRoleGroup")
End Sub
Here's the FormView: (I took out most of the fields so it's easier to read)
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID"
DataSourceID="ObjectDataSource_Vendors"
DefaultMode="Insert" BorderColor="DarkGray"
BorderStyle="Solid" BorderWidth="1px" CellPadding="4" Visible="False">
<EditItemTemplate>
</EditItemTemplate>
<InsertItemTemplate>
<label class="form_label">Role Group:</label><br /><asp:DropDownList ID="DDRoleGroup"
runat="server" Width="175px"
EnableViewState="False">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
Could it possibly have to do with the fact that it's in the Page_Load sub and the control hasn't acctually loaded yet?
Thanks,
Matt
Your dropdown only exists in Insert mode. Try to implement the formview's ModeChanged event and retrieve the control if CurrentMode == Insert:
protected void FormView1_ModeChanged(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Insert)
{
DropDownList DDRoleGroups = FormView1.FindControl("DDRoleGroup");
// fill dropdown
}
}
You cannot handle this in Page_Load, as the form has not yet switched into Insert mode.
FindControl on a formview will only work for the template that the FormView's "CurrentMode" property is set to.
In your case, you can only do FindControl for "DDRoleGroups" if your FormView is set to "Insert", since that's the template that your control exists in.
Hope that helps.