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.
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 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.
Is it possible to create Event (for example Button click event) from Source View? I mean without going to design view ?
In your code, you create your method:
protected void Button1_Click(object sender, EventArgs e)
{
}
And in your markup (not the design view), you add your button:
<asp:Button runat="server" OnClick="Button1_Click" />
That should be it.
Additionally you can add an event handler dynamically in Codebehind f.e.(VB.Net):
AddHandler Me.Button1.Click, AddressOf Me.Button1_Click
You need to create an eventhandler and assign a method to it.
ButtonId.click += new EventHandler(Button1_Click);
protected void Button1_Click(object sender, EventArgs e)
{
}
I'm trying to disable a label control from the CheckedChanged event handler of a checkbox. Should I be able to do this?
At the moment if I set Enabled to false nothing changes when the page reloads. If I do the same thing in Page_Load then I see the change.
To clarify:
This doesn't work:
protected void chkNeverExpires_CheckedChanged(object sender, EventArgs e)
{
this.lblMessage.Enabled = false
}
But this does:
protected void Page_Load(object sender, System.EventArgs e)
{
this.lblMessage.Enabled = false
}
Are you sure you're events are firing in the order you expect? Put breakpoints on all your postback methods and watch what happens, are you resetting the enabled state anywhere? do you have enableviewstate=false on anything?
Edit: You realise CheckedChanged doesnt fire until you postback from another control, or you have AutoPostBack=true on the checbkbox?
This works fine:
<asp:Label runat="server" ID="lblTest">test</asp:Label>
<asp:CheckBox runat="server" ID="chkCheck" AutoPostBack="true" />Check
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
chkCheck.CheckedChanged += chkCheck_CheckedChanged;
}
private void chkCheck_CheckedChanged(object sender, EventArgs e)
{
lblTest.Enabled = false;
}