[edit]
calling the follows directly is not possible in my code:
void control_Changed(object sender, EventArgs e)
This function is loop through a Collection of DropDownListBox, and each DropDownListBox has different Select_Change function. Also they are not in the same page, they collection of DropDownListBox is come from different user control of the page.
I saw a lot of solution are simply calling the function that the event should trigger..
But this will not be working on my case.
I have a code that will mapping the data to a collection of dropdownlistbox and select the proper dropdownlistbox item for each dropdownlistbox.
So, is kind of like this:
foreach (Control aControl in aControlCollection){
if (aControl.GetType() == typeof(RadComboBox))
{
bool FoundItem = false;
RadComboBox aComboBox = (aControl as RadComboBox);
foreach (RadComboBoxItem aComboItem in aComboBox.Items)
{
Debug.WriteLine("aComboItem " + aComboItem.Text + " Value" + aComboItem.Value);
if (aComboItem.Value.ToLower() == _dataObject.ToString().ToLower())
{
//aComboBox.SelectedIndex = aComboBox.Items.IndexOf(aComboItem);
aComboItem.Selected = true;
FoundItem = true;
~~~FIRE EVENT HERE~~~~~
//break;
}
else {
aComboItem.Selected = false;
}
}
if (!FoundItem)
{
RadComboBoxItem aComboItem = new RadComboBoxItem();
aComboItem.Value = _dataObject.ToString();
aComboItem.Text = _dataObject.ToString();
aComboBox.Items.Add(aComboItem);
aComboBox.SelectedIndex = aComboBox.Items.IndexOf(aComboItem);
}
}
}
}
Normally in the page, when user select the a first dropdownbox, the 2nd dropdownbox that follows will generate the proper dropdownlist item according to the first dropdownbox (from the first dropdownbox selectindexchange event).
So I wonder if there is anyway I can fire the DropDownListBox programmatically?
Just to make it even more clear, the above function is call by iterates all DropDownListBox on the page, so they can be link into different function.
Combobox_SelectedItem(null, null);
You can forge any arguments you desire into the parameters, if needed.
If you were to use the traditional void control_Changed(object sender, EventArgs e) code...
if (aComboItem.Value.ToLower() == _dataObject.ToString().ToLower())
{
//aComboBox.SelectedIndex = aComboBox.Items.IndexOf(aComboItem);
aComboItem.Selected = true;
FoundItem = true;
control_Changed(aComboItem, new EventArgs());
}
void control_Changed(object sender, EventArgs e) {
// your code here
}
Related
I have an aspx page. On pageLoad a call a method that loads a user control
protected void Page_Load(object sender, EventArgs e)
{
LoadUC();
}
This loads the user control onto the page (into a placeholder) passing a few generic values.
private void LoadUC()
{
ucTS ctrl = (ucTS)Page.LoadControl(_ucTSPath);
ctrl.ParentId = 0;
ctrl.addNew = false;
phFG.Controls.Add(ctrl);
}
The user control (which contains a repeater) loads the initial placeholder as well as another placeholder on the initial page:
protected void Page_Load(object sender, EventArgs e)
{
LoadItems();
}
private void LoadBOMItems()
{
List<Item> dtItem;
if (ParentId == 0)
{
if (ViewState["ItemFG"] == null)
{
dtItem = //Gets list of items
}
else
{
dtItem = (List<Item>)ViewState["ItemFG"];
}
ViewState["ItemFG"] = dtItem;
}
else
{
if (ViewState["Items" + ParentId] == null)
{
dtItem = //get list of items
}
else
{
dtItem = (List<Item>)ViewState["Items" + ParentId];
}
ViewState["Items" + ParentId] = dtItem;
}
if (dtItem.Count > 0)
rptTSItem.DataSource = dtItem;
rptTSItem.DataBind();
}
}
in the binding, I bind the repeater, but I am also adding more of the same user control.
The problem comes when I click add a new item to the repeater. The initial click, everything saves fine and a new row is added. The second click the user control is not found on the initial page and so the save method is not fires. The 3rd click, everything is fine, the 4th, the user control is not found. This keeps happening. Why is my usercontrol not always found? I have tried doing a postback check in multiple places, but that doesn't seem to work.
I am still not 100% sure why the event would fire properly every OTHER time as opposed to just the first time, but the solution was simply adding an ID to the user control.
private void LoadUC()
{
ucTS ctrl = (ucTS)Page.LoadControl(_ucTSPath);
ctrl.ParentId = 0;
ctrl.addNew = false;
ctrl.ID = "someID"
phFG.Controls.Add(ctrl);
}
and whenever I re-added the user control from within the user control, I pass the same ID.
I'm trying to persist CSS user preference to my database according to which of six buttons is selected by the user.
On order to do this, I am trying to assign an integer value to each button click event; whichever is clicked will pass the corresponding integer as a parameter to my data access object to update the database able.
My method reads such:
protected void SetCSS(object sender, EventArgs e)
{
Users setCss = new Users();
if (IsPostBack)
{
if (sender.ToString() == "Blue")
{
setCss.StylePreference = 0;
}
else if (sender.ToString() == "Khaki")
{
setCss.StylePreference = 1;
}
else if (sender.ToString() == "Night")
{
setCss.StylePreference = 2;
}
else if (sender.ToString() == "Pink")
{
setCss.StylePreference = 3;
}
else if (sender.ToString() == "White")
{
setCss.StylePreference = 4;
}
else if (sender.ToString() == "Yellow")
{
setCss.StylePreference = 5;
}
setCss.UserLoginName = Session["eMail"].ToString(); // current user
setCss.SetStylePreference(setCss.UserLoginName, setCss.StylePreference);
}
In each button's click event:
protected void btnBlue_Click(object sender, EventArgs e)
{
SetCSS(btnBlue, null);
}
protected void btnKhaki_Click(object sender, EventArgs e)
{
SetCSS(btnKhaki, null);
}
etc...
I put a watch on the sender object and, when the Pink button is selected, the value assigned to the sender reads
{Text="Pink"}
However, as I step through the if statement in the SetCSS method, when I come to the
else if (sender.ToString() == "Pink")
the condition is not met and, rather than setting the style preference to 3 as it should, the program passes on to the end of the statement, finishing by always assigning a value of 0 to the property.
What am I doing wrong?
Would really appreciate help...
You need to use sender.Id.ToString()
Calling sender.ToString() on an ASP.NET button will return "System.Web.Ui.Button" or something similar.
Paste the code related to how you setup the button and I'll clarify my answer more, as you could need either Id or Text depending on how you're setting the the name on your button.
Realistically, you can refactor this code to be a lot simpler.
You should map the Click event on all of your buttons to SetCSS(). Having a lot of scattered methods to only wrap the call is useless.
Change the if / else block to check for sender.Text
if (sender.Text.ToString() == "Blue")
{
setCss.StylePreference = 0;
}
and do the same for the rest of the statements.
I have this event code of the listBox:
I tried ot do it this way and it's almost working good.
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (recentItems.Contains(listBox1.SelectedItem))
{
itemExist = true;
item = listBox1.SelectedItem.ToString();
this.f1.PlayLightnings();
f1.pdftoolsmenu();
}
else
{
itemExist = false;
item = listBox1.SelectedItem.ToString();
recentItems.Add(listBox1.SelectedItem.ToString());
this.f1.PlayLightnings();
f1.pdftoolsmenu();
}
}
Im using a new bool variable itemExist and check and if the List recentItems wich is don't contain the selectedItem add it.
And if it does exist set the flag to true.
Then in the other code in Form1 im doing:
if (Lightnings_Extractor.Lightnings_Mode.itemExist == true)
{
if (!pdf1.Lightnings.Contains(Lightnings_Extractor.Lightnings_Mode.item))
{
pdf1.Lightnings.Add(Lightnings_Extractor.Lightnings_Mode.item);
}
}
So it's working as i wanted but the problem is that each new item i select in the listBox click on it i have to click on it twice since first time it's not in the recentItems and only on the second click it does in the recentItems and only on the second click it's changing the flag to true.
So how can i solve this problem in the SelectedIndexChanged event ?
I saw now i don't need the code part in Form1 only this code:
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
item = listBox1.SelectedItem.ToString();
this.f1.PlayLightnings();
f1.pdftoolsmenu();
if (item != null && !pdf1.Lightnings.Contains(item.ToString()))
{
pdf1.Lightnings.Add(item.ToString());
}
}
I have 40 buttons in a application that I need custom hovers that will show in a status field. I have made a function for adding a certain message and one to remove, so upon a hover, it calls the function, and same with leaving the button.
I want 40 different messages and one way of doing that is to check which button is being hovered over by the mouse.
if(button1.hovered == true){
string message = "scenario1";
}
elseif(button2.hovered == true){
scenario2...etc
}
Is there a way to check if hovered? and check it in a if statement?
ive decided to add more info so it might be easier to get my point.
add message to listview when mouse hoover.
void messAdd(object sender, EventArgs e)
{
string now = DateTime.Now.ToString("M/d/yyyy") + " - " + DateTime.Now.ToString("HH:mm:ss tt");
string message = "message 1";
found = false;
ListViewItem item = new ListViewItem(message);
foreach (ListViewItem z in listView1.Items)
{
if (z.Text == message)
{ found = true; }
}
if (found == false)
{
item.SubItems.Add(now.ToString());
listView1.Items.Add(item);
listView1.EnsureVisible(item.Index);
}
else
{
DeleteIfNecessary(message);
}
}
delete message from listbox when mouse leave:
void messdel(object sender, EventArgs e)
{
string message = "message 1";
found = false;
ListViewItem item = new ListViewItem(message);
foreach (ListViewItem z in listView1.Items)
{
if (z.Text == message)
{ found = true; }
}
if (found == true)
{
DeleteIfNecessary(message);
}
}
I can make 4 of these functions for each buttons, but since i need 40 different messages, stupid yes, but there is no way to send a message argument through the function, so i need to use the if test and check witch button is hovered and then output the message to that specified button. and im using visual studio and windows forms, sorry for not mentioned.
There is a Control.MouseHover event. You can implement:
private void button_MouseHover(object sender, System.EventArgs e)
{
doSomething(sender);
}
and for all of your buttons, set event handler for MouseHover to button_MouseHover in IDE, or do it in code:
this.button1.MouseHover += new System.EventHandler(this.button_MouseHover);
By checking the sender parameter you can know which button is calling the event handler.
Update
According to your update in the question, I think you can just use messAdd as the event handler for MouseEnter event for all of your buttons, and use messdel as the event handler for MouseLeave. Again, you don't need to create a copy of these two methods for all of your buttons, you just need to assign the same event handler method for all the buttons, and check sender to know who is calling the event handler - then creating different messages.
The sender is your Button object. Just cast it to a Button and access what you want (text, tag, name, etc.) to know which Button is trying to add/remove message on the list view.
Update 2
Button button = sender as Button;
if (button == null) {
// not a button, do nothing
return;
}
string message = String.Empty;
if (sender.Equals(button1)) {
message = "message1";
} else if (sender.Equals(button2)) {
message = "message2";
} ...
I'm going to assume this is WinForms, since you didn't specify otherwise.
You can create an int hoveredId that represents which button is hovered (value -1 means nothing is hovered). When creating the buttons, set the Tag property to the button's id number.
Then register each button to both of these functions:
private void buttons_MouseEnter(object sender, System.EventArgs e)
{
Button btn = ((Button)sender);
hoveredId = (int)btn.Tag;
}
private void buttons_MouseLeave(object sender, System.EventArgs e)
{
Button btn = ((Button)sender);
if ((int)btn.Tag == hoveredId)
hoveredId = -1;
}
When checking which button is hovered, you can use a switch statement:
switch (hoveredId)
{
case 1:
string message = "scenario1";
break;
case 2:
scenario2...etc
break;
}
I am trying to assign a ViewState value in my application with a SelectedIndexChanged function. Once it's assigned the postback will use the value to change some data and then set the value to zero but I can't seem to get it to work correctly. The controls are all created dynamically on Page_Load.
Page Load
protected void Page_Load(object sender, EventArgs e)
{
CreateAttributeControls();
TempProductVariantId = 0;
}
Create Attribute Controls
public void CreateAttributeControls()
{
...
var ddlArtistArtworks = new DropDownList();
ddlArtistArtworks.ID = "ddlArtistArtworksTest";
divAttribute.Controls.Add(ddlArtistArtworks);
ddlArtistArtworks.Items.Clear();
ddlArtistArtworks.SelectedIndexChanged += new EventHandler(ArtistArtwork_SelectedIndexChange);
ddlArtistArtworks.AutoPostBack = true;
...
}
ArtistArtwork_SelectedIndexChange
protected void ArtistArtwork_SelectedIndexChange(object sender, EventArgs e)
{
DropDownList ddl = sender as DropDownList;
TempProductVariantId = int.Parse(ddl.SelectedValue);
}
TempProductVariantId ViewState Save
public int TempProductVariantId
{
get
{
if (ViewState["TempProductVariantId"] == null)
return 0;
else
return (int)ViewState["TempProductVariantId"];
}
set
{
ViewState["TempProductVariantId"] = value;
}
}
When I load the page everything is fine. I change the DropDownList's value, It posts back, and the value is not set. Change it again the value is set and continues to change as I change the value of the DropDownList.
Any guidance on this would be greatly appreciated.
Note: I have tried changing when CreateAttributeControls() is called. In OnPreRender for example. I was given this to understand the lifecycle of the page Life Cycle
That's because you are essentially recreating the dropdown on every postback..
try this
public void CreateAttributeControls()
{
...
DropDownList ddlArtistArtworks;
if (!IsPostBack)
{
ddlArtistArtworks = new DropDownList();
ddlArtistArtworks.ID = "ddlArtistArtworksTest";
divAttribute.Controls.Add(ddlArtistArtworks);
ddlArtistArtworks.Items.Clear();
ddlArtistArtworks.AutoPostBack = true;
}
else
{
ddlArtistArtworks = (DropDownLise)divAttribute.FindControl("ddlArtistArtworksTest");
}
ddlArtistArtworks.SelectedIndexChanged += new EventHandler(ArtistArtwork_SelectedIndexChange);
...
}
For dynamically added controls, the event handler has to be linked up everytime so that has to be done outside the if-block, unconditionally.