Multiple DropDownList but same action - c#

I have two DropDownList with OnSelectedIndexChanged="SelectedIndexChanged" but I need to know in the C# code withch one is the one I used.
How can I know that?
Answering the questions:
I'm using Web Forms and I'm trying to change some GridViews Source from a choseen option in a DDL but the web had the same DDL (with different IDs) in several places and I can't delete them...

The general form for an event handler is:
OnSomeEvent(object sender, EventArgs e)
sender is a reference to the object that is raising the event.
In your case, sender is a reference to the DropDownList whose selected index has changed. So you should use something like this:
private void SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList the_list_that_changed = (DropDownList)sender;
int ids = the_list_that_changed.SelectedIndex;
}

The first parameter sender represents the object raising the event. Hence the Sender reference to your DropDownList whose triggered the selected index changed.
private void SelectedIndexChanged(object sender, EventArgs e)
{
if (((DropDownList)sender).ID == "firstDropDownID")
{
//To Do for first dropdown
}
else
{
//To Do for second dropdown
}
}

Related

How do i get information that is object sender

I'm currently trying to get access to information that is passed in through an object sender.
The application I am working on is a winforms application with a list view. I want to get the number of the ListViewItem that the user has pressed on. The ListView item that I have pressed on is correct when I debug.
However I am unaware of how to get the information I want from the object sender. I want to access the ListViewItem number,
look at the posted image ListViewItem: {24919} in this case
so I can use this number as index, when I search in a database.
Does anyone have a fast tip so I can continue with my program ?
private void InvoiceListView_SelectedIndexChanged(object sender, EventArgs e)
{
//Connect to db and search based on the the listviewItemnumber.
}
Currently the object sender containsmethod;
You can type-check the sender and work with the result:
private void InvoiceListView_SelectedIndexChanged(object sender, EventArgs e)
{
if (!(sender is ListView listView)) return;
//work with the listView object from here:
listView.Items = ...
}
You can get the item selected by casting the sender to ListView and then get corresponding value as given below:
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
ListView lw = (ListView)sender;
foreach(ListViewItem lvi in lw.SelectedItems )
{
MessageBox.Show(lvi.SubItems[0].Text);
}
}

TextBox c# refer string

Is it possible to like refer a string as content for a TextBox in c#? I have a listbox with a bunch of objects in it. And each object contain a string. And when I select an object in the listbox I want its string to be the content in the TextBox, so that whatever I write gets saved to the string.
Like for example in Java you could have a PlainDocument in an object, and whenever you select a different object in a JList you could set the document in a JTextField to the objects PlainDocument.
You can either use Data Binding for an automated solution or you can manually listen for SelectedIndexChanged event of the list box and set the Text property in the event handler.
listBox1.SelectedIndexChanged += (o, e) => {
object selectedItem = listBox1.SelectedItem;
textBox1.Text = selectedItem != null ? selectedItem.ToString() : null;
};
The content of the textbox can be accessed using
myTextBox.Text
This property expects string, so your answer is Yes. I think simply assigning this property would do.
UPDATE
I think you need something like this(assuming you are using WinForms):
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listBox1.SelectedItem != null)
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
int index = listBox1.Items.IndexOf(listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);
listBox1.Items.Insert(index, textBox1.Text);
}
Though there is an action for TextChanged event of textbox in WinForms, but changing listbox from there is a bit tricky (ends up calling each other infinitely) as we are already changing textbox from the change event of listbox.
Adding a button to do this simplifies it a lot.

calling multiple dropdownlist index change methods within the another ddl index change

I am using visual studio 12, coding in asp.net using c#.
I have 3 dropdownlists in my code, all of which are bound by lists that I created.
I need some advise as to which method is better to call the postbackvalues of ddl's to perform a task.
Option 1
When a user selects an item from drop down list 3, the postbackvalue is sent from Dropdownlist3_SelectedIndexChanged to the dropdownlist2_selectedindexchanged by calling the method. Only after I have both the postbackvalues I would like to produce a chart. Regardless of what the chart holds and regardless of what the data is in the drop down list.
So something like
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
// I would like to have the postbackvalue of drop down list 3 here so i can use its value and dropdownlist2's postbackvalue to produce a chart.
}
and in the dropdownlist3
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
// I would like to call DropDownlist2_SelectedIndexChanged(...) method so I can send the postbackvalue of DDL3 for use in DDL2.
}
Option 2:
Define a global variable that stores the postbackvalue of Dropdownlist3 and use that value in Dropdownlist2_SelectedIndexChanged method for further use such as produces a chart.
I have read a lot about global variables but do not understand the con's about them.
I am not sure if this is what you are after, but perhaps having a third method which is called that handles the updating of the chart...
for example
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
BuildChart();
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
BuildChart();
}
private BuildChart()
{
var ddl3Value = DropDownList3.SelectedValue;
var ddl2Value = DropDownList2.SelectedValue;
if(ddl3Value != null && ddl2Value != null)
{
//build chart.
}
}

How to call a method that has parameters from a click event

I have a method that gets a column value from an output gridview. How do I call this method in a button click event so that I can use the column value? Below is the method and button click event.
string mailAdd;
public void get_value(object sender, GridRecordEventArgs e)
{
mailAdd = e.Record["emailAddress"].ToString();
}
protected void btnsendMail_Click(object sender, EventArgs e)
{
//call get_value here
}
you cant ' because this method expect the EventArgs from the Sender which is the Grid object in your case , what I'd recommend is in
Get_value set the e.record["emailAddress"].toString() to a session and call in button
public void get_value(object sender, GridRecordEventArgs e)
{
Session["emailAddress"]= e.Record["emailAddress"].ToString();
}
protected void btnsendMail_Click(object sender, EventArgs e)
{
//you can use this
string _myEmail=Session["emailAddress"];
}
regards
is the part of the grid row? if yes then you should be using ItemCommand, if the button is separated from grid, then you may need to ascertain (dynamically) the row from which you want to read the email address. If you have the info, then you can easily access grid's content by row index and columnname, to get the email address, then rest is obvious.

ASP .Net Gridview

I'm using a form to populate a gridview with x users. However I want whenever I call the function if there is only 1 user displayed inside the gridview to autoselect that first user.
So within my callback function I have
if (users.count == 1)
{
// Do something
}
I currently use the following function upon someone pushing "select" alongside the gridview.
Users_SelectedIndexChanged(object sender, EventArgs e)
It would be nice if I could reuse this function and do something like
if (users.count == 1)
{
Users_SelectedIndexChanged(object sender, EventArgs e);
}
Use the GridView.SelectedIndex property.
Setting this property to 0 after binding the data and checking for at least one item ought to do the trick. Technically you could call the event handler method, but to no end other than executing your own implementation.
I would use the GridView's OnLoad method and call your function there. Data has been bound when the OnLoad method is called.
public void GridView_OnLoad(object sender, EventArgs e)
{
//Assuming one row means 1 user and gv is your gridview object
if (gv.Rows.Count == 1) //(user.count == 1)
{
//call your selected function here
}
}

Categories

Resources