How to prevent auto firing RadioButtonList indexchange event? - c#

I have a radioButton list and I want to set 0 as default index.
But,when page is posted by other button ,radiobuttonListChanged event is firing automaticly.
What should I do ?
protected void Page_Load (object sender, EventArgs e)
{
rblList.DataSource = MyDataSource();
rblList.DataBind ();
if (!IsPostBack)
{
rblAccounts.SelectedIndex = 0;
}
}
protected void rbl_Changed (object sender, EventArgs e)
{
Response.Write("Change Event fired");
}

In rbl_Changed event Write following code probably will help you out.
rbl.changed-= rbl_Changed;
rbl.changed = false;
rbl.changed+= rbl_Changed;

Related

1 button, 2 webpages, but one webpage at a time?

Here's what H have so far:
private void button1_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.ai_yori_aoshi_5370;
}
private void button2_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.AiYoriAoshi_feature;
}
private void button3_Click(object sender, EventArgs e)
{
audio.Stop();
if (button1.Enabled == true)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide");
if (button2.Enabled == true)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide");
}
}
}
this is only my test so far but what i want to do is change what button 3 does, i.e. if button 1 is clicked button three will open webpage 1, if button2 is clicked button 3 will open webpage 2, button 3's image will change depending, but what im finding with what i have done so far is that it opens BOTH pages AT THE SAME TIME ... how to i prevent this? i have tried if, else and else if, same result every time.
Both of your buttons are enabled, you are checking to see if the buttons are enabled or disabled (clickable or not), not which one has been clicked.
also:if (button2.Enabled == true)
is nested in the first conditional, I'm not sure if that's what you want.
You can: disable buttons 1 and 2 after their clicked so that, for instance button2.Enabled will now = false; (but then you will not be able to reclick that button)
More sophisticated, but better, is to use a delegate for the button3, and assign them in your button1_Click and button2_Click events. Something like this:
private void button1_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.ai_yori_aoshi_5370;
button3.Click += new EventHandler(this.Button3_Click_First);
}
private void button2_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.AiYoriAoshi_feature;
button3.Click += new EventHandler(this.Button3_Click_Second);
}
void Button3_Click_First(Object sender,
EventArgs e)
{
// When the button is clicked,
// change the button text, and disable it.
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide");
}
void Button3_Click_Second(Object sender,
EventArgs e)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide");
}
You may also have to check and make sure an event handler was not previously assigned, in calse someone clicks button1, then button2, then button1 ect. This is described here: Removing event handlers
You can handle your problem by storing the URL of the webpage in a private field, setting it when buttons 1 or 2 are clicked and reading from it after clicking button 3.
private string _address = null;
private void button1_Click(object sender, EventArgs e)
{
// do other stuff
_address = "http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide";
}
private void button2_Click(object sender, EventArgs e)
{
// do other stuff
_address = "http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide";
}
private void button3_Click(object sender, EventArgs e)
{
if (_address != null)
{
audio.Stop();
if (button1.Enabled || button2.Enabled)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start(_address);
}
}
}
I wasn't sure if all the code in button3_Click is necessary, so I cleared it up a little. I might be a bit off, though.
button.Enabled is always true for all buttons by default unless you set it to false. So you cannot use button1.Enabled property to check which button is pressed. try below approach.
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["Button1Clicked"] = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
ViewState["Button1Clicked"] = false;
}
protected void Button3_Click(object sender, EventArgs e)
{
if ((bool)ViewState["Button1Clicked"])
{
//open webpage2 code comes here
}
else
{
//open webpage2 code comes here
}
}

c# remove items when uncheck check box

When I check the checkbox in the checkboxlist, the checkbox.text will add into the listbox.
But when I uncheck the checkbox, the checkbox.text will be removed from the listbox.
But the problem is I do not know how to remove the selected items from the listbox.
For example, when i check the checkbox1, checkbox2, checkbox3, the listbox will display
checkbox1
checkbox2
checkbox3
However, when i uncheck the checkbox, Still Same
I stuck at here. Help !!
here is my code:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked) listBox1.Items.Add(checkBox1.Text);
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked) listBox1.Items.Add(checkBox2.Text);
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (checkBox3.Checked) listBox1.Items.Add(checkBox3.Text);
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
if (checkBox4.Checked) listBox1.Items.Add(checkBox4.Text);
}
if (checkBox3.Checked)
listBox1.Items.Add(checkBox3.Text);
else
listBox1.Items.Remove(checkBox3.Text);
Note that this will always remove whatever is in the Text property. This means that, if I check the box, change the text in textBoxX, and then uncheck, it will remove a different item.
The Items collection on a ListBox has a Remove method. Put an else in each of your CheckedChanged events and use the Remove method.
if (checkBox4.Checked) listBox1.Items.Add(checkBox4.Text);
else listBox1.Items.Remove(checkBox4.Text);
Make a common function and call. For e.g.
private void addRemove(CheckBox chk)
{
if (chk.Checked)
listBox1.Items.Add(chk.Text);
else
listBox1.Items.Remove(chk.Text);
}
Call
addRemove(checkbox1);
You can do one funciton
private void checkBoxCheckedChanged(object sender, EventArgs e)
{
CheckBox cb = sender as CheckBox;
if (cb != null)
if (cb.Checked) listBox1.Items.Add(cb.Text); else listBox1.Items.Remove(cb.Text);
}
And then add it for all your checkboxes as CheckedChanged event.
Call Remove() method. It accepts one argument that specifies the item to remove.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
listBox1.Items.Add(checkBox1.Text);
else
listBox1.Items.Remove(checkBox1.Text);
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked)
listBox1.Items.Add(checkBox2.Text);
else
listBox1.Items.Remove(checkBox2.Text);
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (checkBox3.Checked)
listBox1.Items.Add(checkBox3.Text);
else
listBox1.Items.Remove(checkBox3.Text);
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
if (checkBox4.Checked)
listBox1.Items.Add(checkBox4.Text);
else
listBox1.Items.Remove(checkBox4.Text);
}
If thats all you want to do why not assign the same event to every checkbox and do the below:-
CheckBox chkBox=(CheckBox)sender;
if (chkBox.Checked)
listBox1.Items.Add(chkBox.Text);
else
listBox1.Items.Remove(chkBox.Text);

Changing the BackColor of a textbox upon entry

I have the following code for my form:
private void txt1_Enter(object sender, EventArgs e)
{
txt1.SelectAll();
txt1.BackColor = Color.LightBlue;
}
private void txt2_Enter(object sender, EventArgs e)
{
txt2.SelectAll();
txt2.BackColor = Color.LightBlue;
}
private void txt1_Leave(object sender, EventArgs e)
{
txtThermalConductivity.BackColor = Color.White;
}
private void txt2_Leave(object sender, EventArgs e)
{
txtThermalConductivity.BackColor = Color.White;
}
There are another 20 textboxes on my form that I would like to do the same for. Is it possible to combine all of the enter events and all of the leave events so I have two events in total rather than 44 individual events?
In your Designer view, select each textbox and set the Enter and Leave events to point to a single implementation of each.
Then you can do this:
private void txt_enter(object sender, EventArgs e) {
((TextBox)sender).BackColor = Color.LightBlue;
}
private void txt_leave(object sender, EventArgs e) {
((TextBox)sender).BackColor = Color.White;
}
Also, SelectAll isn't required because you're setting the entire textbox's background color.. not the SelectionColor of a RichTextBox.
You could either add manually or iterate over all textboxes in form (extension method found here GetChildControls.
foreach (TextBox textBox in this.GetChildControls<TextBox>())
{
textBox.Enter += new EventHandler(TextBox_Enter);
textBox.Leave += new EventHandler(TextBox_Leave);
}
The above can be called from the Form's Load event.
The event listener now can look like the following by casting the sender to TextBox.
private void TextBox_Enter(object sender, EventArgs e)
{
TextBox txtBox = (TextBox)sender;
txtBox .SelectAll();
txtBox .BackColor = Color.LightBlue;
}
private void TextBox_Leave(object sender, EventArgs e)
{
TextBox txtBox = (TextBox)sender;
txtBox.BackColor = Color.White;
}
It is, just use something like the following:
private void tbLeave(object sender, EventArgs e) {
((TextBox) sender).BackColor = Color.White;
}
The set the controls event declaration to point to this function.
You can also do the same for the Leave() event.
(Just a little note to say, I much prefer to handle this kind of thing client side where possible.)

How to define hyperlink click event on the fly?

I am stuck on something: I am creating a hyperlink at runtime that has a navigation URL. I need to define its click event so that I can save a few values to the database. I did something like below but without success.
Could you please suggest an alternative?
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) {
if (e.Item is GridDataItem) {
HyperLink link = (HyperLink)gridDataItem["ContentTitle"].Controls[0];
link.ForeColor = System.Drawing.Color.Navy;
link.ToolTip = Common.grdTextCell(gridDataItem["ContentSummaryDescr"].Text);
link.NavigateUrl = "~/SomePath/" + gridDataItem["ContentName"].Text;
link.Target = "_blank";
link.Attributes.Add("onclick", "document.getElementById('" +
dummyBtn.ClientID + "').click();");
}
}
protected void dummyBtn_Click(object sender, EventArgs e) {
}
But the button click event is not firing, it simply navigates to the URL. What to do please?
For a server side event to fire you would need a LinkButton and not a HyperLink
LinkButton has a Click event handler which you can use.
HyperLink only redirects and has no corresponding Click event handler associated for server side code
You want a LinkButton, not a HyperLink.
Here's some sample code to get you started (not tested)
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
LinkButton link = (LinkButton)gridDataItem["ContentTitle"].Controls[0];
link.Click += dummyBtn_Click;
}
}
protected void dummyBtn_Click(object sender, EventArgs e)
{
Response.Write("dummyBtn_Click");
}
You should be using Link Button. Just replace your Hyperlink with LinkButton in your code.It should work.
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) {
if (e.Item is GridDataItem) {
LinkButton link = (LinkButton )gridDataItem["ContentTitle"].Controls[0];
link.ForeColor = System.Drawing.Color.Navy;
link.ToolTip = Common.grdTextCell(gridDataItem["ContentSummaryDescr"].Text);
link.NavigateUrl = "~/SomePath/" + gridDataItem["ContentName"].Text;
link.Target = "_blank";
link.Click += dummyBtn_Click;
}
}
protected void dummyBtn_Click(object sender, EventArgs e) {
}

TextBox validating doesn't work

I have two textboxes. I need to validate them before taking any other action.
private ErrorProvider _errorProviderEmail = new ErrorProvider();
private ErrorProvider _errorProviderPass = new ErrorProvider();
public FormLogin()
{
InitializeComponent();
textBoxEmail.Validating += TextBoxEmailValidating;
textBoxPass.Validating += TextBoxPassValidating;
textBoxEmail.Validated += TextBoxEmailValidated;
textBoxPass.Validated += TextBoxPassValidated;
textBoxEmail.Text = "";
textBoxPass.Text = "";
}
void TextBoxPassValidated(object sender, EventArgs e)
{
_errorProviderPass.SetError(textBoxPass, "");
}
void TextBoxEmailValidated(object sender, EventArgs e)
{
_errorProviderEmail.SetError(textBoxEmail, "");
}
void TextBoxPassValidating(object sender, System.ComponentModel.CancelEventArgs e)
{
if (!string.IsNullOrEmpty(textBoxPass.Text)) return;
e.Cancel = true;
_errorProviderPass.SetError(textBoxPass,"Password is required!");
}
void TextBoxEmailValidating(object sender, System.ComponentModel.CancelEventArgs e)
{
if (!string.IsNullOrEmpty(textBoxEmail.Text)) return;
e.Cancel = true;
_errorProviderEmail.SetError(textBoxEmail, "Email address is required!");
}
The problem is that only validating event for textBoxEmail is triggered, what could be wrong here, and why the validating event for textBoxPass never fires?
The individual TextBox controls only validate when they lose their focus.
Try calling the form's ValidateChildren() function to force each control to call their validation handlers:
private void button1_Click(object sender, EventArgs e) {
if (this.ValidateChildren()) {
this.Close();
}
}
Also, you only need one ErrrorProvider component.
The Validating event is raised only when the control that receives the focus has the CausesValidation property set to true.
For example, if you have written code in TextBox1's Validating event, and you click the OK button (CausesValidation = true) then the Validating event is raised, but if you click the Cancel button (CausesValidation = false) then the Validating event is not raised.
Source on CodeProject

Categories

Resources