i have created checkbox event maually.
chkCheckBox1.CheckedChanged += new EventHandler(chkCheckBox1_CheckedChanged);
this event is not triggered,in pageload i have put
(!page.ispostback)
{
}
so when i clik the check box it goes to the page load and not going to the evnt
protected void chkCheckBox1_CheckedChanged(object sender, EventArgs e)
{
..........
}
the checkbox event is not triggerd..
Have you enabled AutoPostBack property on your control?
By default this is set to False when you add a checkbox control to your page. Try setting it to true.
Set the Autopostback property to true.
chkCheckBox1.CheckedChanged += new EventHandler(chkCheckBox1_CheckedChanged);
You have to wire up this event on every call to the page so if you have put this inside of the if(!Page.IsPostBack) then put it outside.
Take a look at this article Adding a dynamic control to a placeholder control and wire up the event. It shows an extra step for making things totally dynamic but the principles stay the same for what you're after.
Grz, Kris.
To trigger the following event
protected void chkCheckBox1_CheckedChanged(object sender, EventArgs e)
{
..........
}
Set the checkbox autopostback property to TRUE
Related
I have a question about C# repeater. I have default width setting, and it will change base on some conditions in the Page_Load, I want change to be pass to my Image on OnItemDataBound. However, it seems that the OnItemDataBound is firing off before Page_Load because I changed the width to 700 in Page_Load, but when the image is loaded, it is always showing 380 instead. If OnItemDataBound is not the correct function to use, which function should I call so that I can change the image width after the Page_Load (where the custom width is set) is called? I tried OnPreLoad, OnLoad, and none of them worked.
protected int width = 380;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
width = 700;
}
}
protected void Test_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
{
Image Image = (Image)e.Item.FindControl("Image");
Image.ImageUrl = Utilities.generateImage();
Image.Width = width;
}
}
Databinding is done on PrerenderComplete event, which is fired on page lifecycle before PageLoad. For more info check https://msdn.microsoft.com/en-us/library/ms178472.aspx#lifecycle_events to see lifecycle events and their order.
If you declare the datasource in markup, it can render everything much earlier since you are not doing a manual databind. This can occur previous to Page_Load.
Try overloading an earlier event, such as OnLoad or OnPreLoad. Both of these occur prior to Page_Load.
If you are explicitly performing databinding and doing it in another event that occurs prior to Page_Load, then you'll have to ensure that the repeater is rebound if you want to change things. Once you call databind, it binds. If you need to change something either do it before or rebind.
You can use Page_Init for this. It fires before ItemDataBound:
protected void Page_Init(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// load
}
}
See also:
https://learn.microsoft.com/en-us/previous-versions/aspnet/ms178472(v=vs.100)
I am trying to add a basic switch to my site in order to switch between static and responsive layouts.
I have two linkbuttons at the bottom of my page:
<div id="toggleView">
<asp:linkbutton ID="lbtnMobile" runat="server" Visible="false">Switch to Mobile site</asp:linkbutton>
<asp:linkbutton ID="lbtnFull" runat="server" >Switch to Full site</asp:linkbutton>
</div>
They both have a very similar OnClick event.
protected void lbtnFull_Click(object sender, EventArgs e)
{
c.ViewChange = true;
Session["Customer"] = c;
}
protected void lbtnMobile_Click(object sender, EventArgs e)
{
c.ViewChange = false;
Session["Customer"] = c;
}
The events should set a boolean in a class file (User.vb) between true or false and then save the session, on postback the Page_Load event is supposed to read this boolean and use it to adjust the Viewport meta tag:
protected void Page_Load(object sender, System.EventArgs e)
{
//Other Stuff in here, irrelevant to current question
HtmlMeta view = new HtmlMeta();
view.Name = "viewport";
if (c.ViewChange = false)
{
view.Content = "width=device-width, initial-scale=1";
lbtnFull.Visible = true;
lbtnMobile.Visible = false;
}
else
{
view.Content = "width=1040px, initial-scale=1";
lbtnFull.Visible = false;
lbtnMobile.Visible = true;
}
MetaPlaceHolder.Controls.Add(view);
}
However, when I click on the "Switch to Full Site" linkbutton, the page will postback but nothing will have changed. Does the postback get triggered too early somehow?
The page load event will happen BEFORE your click event. Reference this here.
This means your check for the ViewChange will happen before you set it in the OnClick handler.
You should change
if (c.ViewChange = false)
to
if (c.ViewChange == false)
for something to happen. But I think it won't be what you expect. Because page_load is executed before click event. You may move some code from page_load to click event handlers.
When ever you postback the Page_Load always get called. So, the code mentioned inside Page_Load would always get executed.
protected void Page_Load(object sender, System.EventArgs e)
{
... All your mentioned code will be executed.
}
Therefore, you won't find any change in your HTML page currently viewed in a browser because at postback initial content also got executed. You need to wrap your content inside !IsPostBack to make it work properly.
Thus, modify you code in following way.
protected void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostback)
{
... All your mentioned code will be executed during normal load.
}
}
Also, you need to add some extra code in LinkButton click event i.e. what to show and what to hide.
Firstly your implementation in the Page_Load isn't very clear.
Nevertheless this is what I recommend, from what I've understod:
As the page load will get executed before the post-back event like the buton or link click, you need persist the value of the class object
Make a protected property of type of your class (where you store/manage the ViewChange atribute)
The property should internally (in the get & set), hold/persist the value in session/viewstate (similar to what you've written)
The setting and reading should only be by referring the property directly (and not how you've done the click-event)
On clicking of the button and post setting the new value, you will have to redirect to the same page, as only then the Page_Load event will get the new boolean value that you've just changed in the click-event; (Page_Load occurs befoer the any other post-back event)
An alternative to the fresh redirection is that, you could make a function that has the view changing logic (as depicted in your Page_Load code), and this function should be called on your button/link click event (post boolean value change) and also in the Page_Load event, but within the "!IsPostBack" block
Hope this helps you.
I'm filling a combobox with the datasource property in a c# winform app. In the other hand, I'm firing up an action with the SelectedIndexChanged of the same combo. The problem is that whenever the combo is filled with datasource the SelectedIndexChanged is called and I just want this event to be called when the user in fact does a selection.
Is there a way to avoid calling this event when filling the combo?
This is some of my code
//Filling the combo with some data
combo_cliente.DataSource = clientes;
combo_cliente.DisplayMember = "NomComp";
combo_cliente.ValueMember = "IDPersona";
private void combo_cliente_SelectedIndexChanged(object sender, EventArgs e)
{
// Here is the action to be triggered when user perfoms a selection
}
Thanks
Maybe unsubscribe and then subscribe again:
combo_cliente.SelectedIndexChanged -= combo_cliente_SelectedIndexChanged;
combo_cliente.DataSource = clientes;
combo_cliente.SelectedIndexChanged += combo_cliente_SelectedIndexChanged;
im assuming you assigned the event handler with the designer so they are bound when the control is instantiated. alternatively you could assign them in code after populating the controls.
Attach your event handler in your code behind instead of doing it on your aspx page and do it affer you have finished loading your control.
you need to add a blank record as the first of your combobox. Then in your code, you can write this;
private void combo_cliente_SelectedIndexChanged(object sender, EventArgs e)
{
if!(comboBox1.SelectedValue.ToString()== string.Empty)
{
//Here is the action to be triggered when user perfoms a selection
}
}
I have a checkboxlist and i have to call function which perforform db job while checked a particular checkbox and unchecked a particular box. Plz help me out !!!!
what about this, if it is a web application:
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (CheckBoxList1.SelectedValue == "your value")
{
//do logic
}
}
CheckBox.OnCheckedChanged Method (System.Web.UI.WebControls)
Raises the CheckedChanged event of the CheckBox control. This allows you to handle the event directly.
CheckBox.CheckedChanged Event (System.Windows.Forms)
CheckedChanged Event. Occurs when the value of the Checked property changes.
I have a formview control, and on the ItemCreated event, I am "priming" some of the fields with default values.
However, when I try to use the formview to insert, before the ItemInserting event gets called, for some reason it calls ItemCreated first. That results in the fields being over-written with the default values right before the insert happens.
How do I get it to not call the ItemCreated event before the ItemInserting event?
you need to use formview Databound event instead of formview ItemCreated event to set values, try like
protected void frm_DataBound(object sender, EventArgs e)
{
if (frm.CurrentMode == FormViewMode.Edit)//whatever your mode here is.
{
TextBox txtYourTextBox = (TextBox)frm.FindControl("txtYourTextBox");
txtYourTextBox.Text// you can set here your Default value
}
}
Also check this thread of similare issue
FormView_Load being overwritten C# ASP.NET
You cannot change the order in which the events fire. However, you should probably wrap the code that sets the default values inside !IsPostBack so that it doesn't reset your values for example:
protected void FormView_ItemCreated(Object sender, EventArgs e)
{
if(!IsPostBack)
{
//Set default values ...
}
}
Try checking the CurrentMode property of the form view.
void FormView_ItemCreated(object sender, EventArgs e)
{
if (FormView.CurrentMode != FormViewMode.Insert)
{
//Initialize your default values here
}
}