I have a simple ASP.NET Application. And On one of my pages all dropdownlist_SelectedIndexChanged events trigger only after I click save button.
This is part of my code behidn:
protected void ddlTimekeeperOffice_SelectedIndexChanged(object sender, EventArgs e)
{
CheckUniqueCombination();
}
protected void ddlTkprDepartment_SelectedIndexChanged(object sender, EventArgs e)
{
CheckUniqueCombination();
}
protected void ddlTkprSummaryTitle_SelectedIndexChanged(object sender, EventArgs e)
{
CheckUniqueCombination();
}
this is 3 dropdowns
protected void btnSave_Click(object sender, EventArgs e)
{
trTimekeeperRow.Visible = true;
}
and a save button.
When I select anything in dropdowns > Nothing happens
Then I click save button and before save_click it goes through every ddl_selectedindexchanged.
Why?
Make sure AutoPostBack property of the dropdown controls is true.
Related
Scenario: Only If the user follow the path Click on ListView > Click on Button the Button1 do something.
In other word I want to check in Button1_Click(object sender, EventArgs e) if the previous focus was on ListView.
So I tried this:
private void ListView_Test_Leave(object sender, EventArgs e)
{
_focusedControl = null;
}
I want raise previous event except when this event is raised:
private void Button1_Click(object sender, EventArgs e)
{
if(_focusedControl == listView_Test)
{
// ...
}
}
Edit: I have a variable that holds a reference to the currently focused control:
private Control _focusedControl;
and I update it in this way:
private void ListView_Test_GotFocus(object sender, EventArgs e)
{
_focusedControl = (Control)sender;
}
If the user follow the path Click on ListView > Click on Button I want raise only the Button1_Click event, in all other case I want normal raise.
You could use a helper variable.
bool wasRaised=false;
private void Button1_Click(object sender, EventArgs e) { wasRaised=true;}
Then you can check that variable in your event, and only run if it is false.
I need save login of my Login page in cookie for show every time when page is loaded
I have create for save login in cookie, in Button click event. but, because in the Post Back, Page_Load before button, so when page_load put cookie information in the TextBox, my cookie is empty
What can I do for resolve this?
protected void Page_Load(object sender, EventArgs e)
{
// This time, Cookies["login"].Value is empty
tbLogin.Text = Response.Cookies["login"].Value;
}
protected void Button1_Click1(object sender, EventArgs e)
{
Response.Cookies.Add(new HttpCookie("login", tbLogin.Text));
}
Try using Page.IsPostBack property.
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
tbLogin.Text = Response.Cookies["login"].Value;
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
Response.Cookies.Add(new HttpCookie("login", tbLogin.Text));
}
I have many controls in my form. for example 120 labels in one panel. and i want when user clicked on each label just call same function with same parameter.
Now i used like this :
private void label67_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
private void label66_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
private void label65_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
private void label64_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
now can i make easy way to call ChangeToTextbox function when user clicked in any label?
Add the same OnClick handler for all labels on the panel:
private void Form1_Load(object sender, EventArgs e)
{
panel1.Controls.OfType<Label>().ToList().ForEach(l => l.Click += label_Click);
}
private void label_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
Find all your controls then just add the handler via code;
List<Control> controls = GetAllMyControls();
foreach(Control control in controls)
{
control.OnClick += (o, e) => { ChangeToTextBox(o); }
}
The syntax should be very similar for both web and winform solutions.
It can be easily achieved by using following approach: Pls give a try,
1) Go to Windows Forms Designer and click the first Label control to select it. Then hold down the CTRL key while you click each of the other labels to select them. Be sure that every label is selected.
2) Then go to the Events page in the Properties window. Scroll down to the Click event, and type label_Click in the box
3) Press ENTER. The IDE adds a Click event handler called label_Click() to the code, and hooks it to each of the labels.
private void label_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
Reference: http://msdn.microsoft.com/en-us/library/dd553231.aspx
I have a label working as a button. I would like when I press a button the click event to this label to take action. for example
private void Label1_Click(object sender, EventArgs e)
{
MessageBox.Show("I pressed this label and showed me this messagebox");
}
Now I want when I press this button, the label1 click event to be performed
private void button1_Click(object sender, EventArgs e)
{
// I want when I press this button something like this happens
Label1.PerformClick();
}
private void button1_Click(object sender, EventArgs e)
{
Label1_Click(sender, e);
}
now if you want to show a message of which control was clicked all in one method do the following
private void label1_Click(object sender, EventArgs e)
{
Control control = (Control)sender;
var name = control.Name;
MessageBox.Show(string.Format("I pressed this {0} and showed me this messagebox",name));
}
Two ways to do this.
First:
private void button1_Click(object sender, EventArgs e)
{
Label1_Click(sender, e); // Just call the Label's click handler
}
Second:
// Bind the Label1_Click handler to the button1 Click event, as they both use the same delegate
button1.Click += new EventHandler(Label1_Click);
With the second approach, note that in C# delegates are multi-cast, so both the button1_Click handler and the Label1_Click handler will be called when the button is clicked, in the order they were bound.
private void button1_Click(object sender, EventArgs e)
{
//What the label click do:
MessageBox.Show("I pressed this label and showed me this messagebox");
}
Is that not easier?
Why do you want to do it ?
I think it would be easier for you to just include the lable click functionality with the button click. Maybe even separate each piece in their own method and call them from the button click. Here is how you'd call another click event.
private void button1_Click(object sender, EventArgs e)
{
label1_Click(sender, e);
}
public class MyLabel:Label
{
public void PerformClick()
{
OnClick(new EventArgs());//InvokeOnClick(this,new EventArgs());
}
}
I've got a form I've created with 4 buttons that will modify a listView, each with their own click event. I'm wondering as a way to avoid adding a method call in each of these buttons if it their is an event available for after a button has finished executing its click event?
The reason for this is so I can update the listView which will in turn update a webBrowser
Here's an example which results in "1" and then "all" being displayed to the user when button 1 is clicked, "2" and then "all" when button 2 is clicked etc etc.
Note: you don't want to do it like this - see comments
public Form1()
{
InitializeComponent();
foreach(var b in Controls.OfType<Button>())
b.Click += new EventHandler(AnyButton_Click);
}
void AnyButton_Click(object sender, EventArgs e)
{
MessageBox.Show("all");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("1");
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("3");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("2");
}