I have a TextBox and a postback Button.
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged1" EnableViewState="false"></asp:TextBox><span></span>
<asp:Button ID="Button1" runat="server" Text="Button" />
So, I need to fire TextChanged event only when text changed(what an irony), like it fires when EnableViewState is true. I can't unsubscribe event or subscribe it somwhere else or enable ViewState.
I've tried to save text from the TextBox in HiddenField and then check if it's changed. Here's the code
protected void Page_Load(object sender, EventArgs e) {
if (HiddenField1.Value != TextBox1.Text) {
HiddenField1.Value = TextBox1.Text;
TextBox1.Text = HiddenField1.Value;
}
}
But I have no idea what to do when text is not changed and not to fire the event.
The problem was in saving values from TextBox when ViewState disabled. Saving it in HiddenField is not a good idea, because we can take value from HiddenField on Page_Load, but we need to take it on TextBox init. However, if we save TextBox to Session variable, this problem will disappear. Here's the code:
protected void TextBox1_TextChanged1(object sender, EventArgs e) {
Session["text"] = TextBox1.Text;
}
protected void TextBox1_Init(object sender, EventArgs e) {
if(Session["text"]!=null)TextBox1.Text = Session["text"].ToString();
}
Save values in session variable on textchanged event and restore it on textbox init. Comparison run on framework level.
Related
I have a form and I am want to call a stored procedure on text change event of that text-box ,
Can you suggest any idea.
Thanks in advance.
You just have to handle the TextChanged-event:
<asp:TextBox ID="TextBox1" runat="server"
OnTextChanged="TextBox1_TextChanged">
</asp:TextBox>
Codebehind:
protected void TextBox1_TextChanged(Object sender, EventArgs e)
{
TextBox txt = (TextBox) sender;
// call your stored-procedure
}
Note that you should databind the controls only if(!IsPostBack), otherwise the events are not triggered. So for example in Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBindForm(); // which sets the `Text` property of this TextBox among others
}
}
I've got an image button in a Repeater, which is in a User Control, and I can't get the OnItemCommand event to fire when I click it. It always gives the error: "... Invalid postback or callback argument. Event validation is enabled using ..."
When I change the image button to a link button, it doesn't give me the error, but it still doesn't fire the OnItemCommand function.
I found some other relevant answers (such as How to process events from Buttons inside Repeaters? And what's this EnableEventValidation thing?) but they all say, "Make sure you're data-binding your repeater inside of a if(!Page.IsPostBack) block." I have done that, but it didn't make a difference.
Here's the markup for the repeater:
<asp:Repeater ID="rptExpenses" OnItemDataBound="rptExpenses_ItemDataBound" OnItemCommand="Button_Command" runat="server" >
<ItemTemplate>
<asp:ImageButton ID="ibDelete" ImageUrl="~/Images/delete.png" CommandName="Delete" runat="server" />
</ItemTemplate>
</asp:Repeater>
Here's some exerpts from the code-behind
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
rptExpenses.DataSource = ExpenseIds;
rptExpenses.DataBind();
}
}
protected void rptExpenses_ItemDataBound(object sender, RepeaterItemEventArgs e) {
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)) {
var ibDelete = (ImageButton)e.Item.FindControl("ibDelete");
ibDelete.CommandArgument = e.Item.DataItem.ToString();
}
}
protected void Button_Command(object sender, EventArgs e) {
var btn = (IButtonControl)sender;
switch (btn.CommandName) {
case "Delete":
//delete it
break;
}
}
Try using this code instead:
<asp:Repeater ID="rptExpenses" runat="server">
<ItemTemplate>
<asp:ImageButton ID="ibRemove" ImageUrl="~/Images/delete.png" runat="server"
CommandName="Remove"
CommandArgument='<%# Container.DataItem %>'
OnCommand="ibRemove_Click"/>
</ItemTemplate>
</asp:Repeater>
And this in the code behind:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
rptExpenses.DataSource = ExpenseIds;
rptExpenses.DataBind();
}
}
protected void ibRemove_Click(object sender, CommandEventArgs e)
{
var btn = (IButtonControl)sender;
switch (btn.CommandName)
{
case "Remove":
//delete it, use btn.CommandArgument to find id to remove
break;
}
}
If you still receive the ... Invalid postback or callback argument. Event validation is enabled using ... error, it most likely means that you are somewhere re-binding the repeater before the ibRemove_Click(...) event is raised.
I found the reason why this was happening in the answer to this question: Why ItemCommand doesn't fired on My Repeater
Turns out that for some reason the Page's DataBind function was invalidating the repeater's controls without passing through the repeater's OnItemDataBound function a second time.
I want to add add an Ajax CalendarExtender to my page. And then after selecting a date and clicking on a button I get the selected Day in a label.
I have a text box which is the target of the CalendarExtender
<asp:TextBox ID="DateText" runat="server" ReadOnly="true" ></asp:TextBox>
<ajaxToolkit:CalendarExtender
ID="Calendar1"
runat="server"
TargetControlID="DateText"
Format="MMMM d, yyyy"
PopupPosition="Right"
/>
<asp:Button runat="server" ID="Button1" onclick="Button1_Click" />
In the Code Behind:
On the first page load I set the date to Today.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Calendar1.SelectedDate = DateTime.Today;
}
}
In the Button1_Click Event
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = Calendar1.SelectedDate.Value.Day.ToString();
}
The problem is when I click the button (or after any post backs) the value selected get reset to Today's date.
And if I don't set it to DateTime.Today in the Page_Load It get reset to null and throw a null exception.
How can I solve this?
Thank you very much for any help.
I hope i was clear
Maybe this will work:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if(!Calendar1.SelectedDate)
Calendar1.SelectedDate = DateTime.Today;
}
}
The solution to the issue is making use of Request.Form collections. As this collection has values of all fields that are posted back to the server and also it has the values that are set using client side scripts like JavaScript.
Thus we need to do a small change in the way we are fetching the value server side.
C#
protected void Submit(object sender, EventArgs e)
{
string date = Request.Form[txtDate.UniqueID];
}
Suppose there is checkBox with id chkSelect and to perform ondatabinding event.How to do that?
You can add AutoPostBack="true" and OnCheckedChanged="chkSelect_CheckChanged" to your chkSelect CheckBox, then add this in your codebehind:
protected void chkSelect_CheckChanged(object sender, EventArgs e)
{
// your code here
}
if you want to perform actions when the CheckBox is checked/unchecked.
Or add OnDataBinding="chkSelect_DataBinding" to your CheckBox and the following in your codebehind:
protected void chkSelect_DataBinding(object sender, EventArgs e)
{
// your code here
}
if you want to use the OnDataBinding event.
I suppose you want to get ondatabinding event method called.
For this your check box should have event declared as:
<asp:CheckBox runat="server" ID="chkSelect" Text="CheckBox to bind" ondatabinding="chkSelect_DataBinding"/>
or
you can also do it Page_init or OnInit method as
protected override void OnInit(EventArgs e)
{
chkSelect.DataBinding += new EventHandler(chkSelect_DataBinding);
base.OnInit(e);
}
Now to let this event fire, you can call
chkSelect.DataBind(); in page_load. This will fire ondatabinding event.
Please note that the problem described below is almost the exact opposite to the common problem of "my control shows the same value every time the page loads. I understand the behavior expressed is generally not desirable.
I have a listbox which is being databound in the page load event even on postback.
When the event handler for the selectedindex changed is hit, the control somehow has the posted value even though it has already been bound again and should not have a selectedindex at this point.
Does anyone know how this could be possible.
EDIT:
To demonstrate that the SelectedIndex is indeed reset you can create a form with the following simple markup:
<label for="textbox1">Original Posted Value: </label>
<asp:TextBox runat="server" ID="textbox1" />
<asp:DropDownList runat="server" ID="dropdown" OnSelectedIndexChanged="dropdown_SelectedIndexChanged" AutoPostBack="true" />
<label for="textbox2">Value at point handler is hit: </label>
<asp:TextBox runat="server" ID="textbox2" />
With the following code in the .cs
protected void Page_Load(object sender, EventArgs e)
{
textbox1.Text = dropdown.SelectedIndex.ToString();
dropdown.DataSource = new string[] { "none", "A", "B", "C" };
dropdown.DataBind();
}
protected void dropdown_SelectedIndexChanged(object sender, EventArgs e)
{
textbox2.Text = dropdown.SelectedIndex.ToString();
}
Notice that the value in the second textbox will alway be 0.
The problem here is that the datasource is getting re-assigned and re-bound on each page load including postbacks. The selected index is changed and then changed back to 0.
Try cathcing the postbacks, and only set the datasource if it is not a postback (the initial load) such as this in the .cs
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
textbox1.Text = dropdown.SelectedIndex.ToString();
}
else
{
dropdown.DataSource = new string[] { "none", "A", "B", "C" };
dropdown.DataBind();
}
}
protected void dropdown_SelectedIndexChanged(object sender, EventArgs e)
{
textbox2.Text = dropdown.SelectedIndex.ToString();
}