Command Argument in Runtime created imagebutton doesn't work? - c#

Hello i am creating new image button something like this ;
e.row.cells[0].text= string.empty; //clearing the cells
Imagebutton img1= new ImageButton();
img1.Id="imgInput";
img1.ImageUrl="~/sample.jpg";
img1.CommandArgument= varID; // fetching the ID
img1.click+= new ImageClickEventHandler(imgInput_Click);
e.row.controls.add(img1)
I have written the above code in gridview rowdatabound event which.
Now in the click event (imgButton_Click; handled in second last line) i wish to put the command argument value in session;
protected void imgInput_Click(object sender, EventArgs e)
{
Session["idvalue"]= imgInput.CommandArgument;
}
But the varID here is coming as null. Why so? What am i missing here?
Please guide! Thanks

You need to use the ImageButton.Command event.
Example:
img1.Command += ImageButton_OnCommand;
protected void ImageButton_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Sort" && e.CommandArgument == "Ascending")
Label1.Text = "You clicked the Sort Ascending Button";
else
Label1.Text = "You clicked the Sort Descending Button";
}

Related

Listbox: Event SelectedIndexChanged called when add one item

I have this code behind a winforms which simply has a listbox as its only control:
private void Form1_Load(object sender, EventArgs e)
{
listBox1.DataSource = dtv;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "IDName";
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet dss = UseDatabase.FillDataSet("Select * From Table Where IDName=" + listBox1.SelectedValue);
string st = dss.Tables[0].Rows[0][0].ToString();
MessageBox.Show(st);
}
Run it say: "Additional information: There is no row at position 0."
Debug I see Event SelectedIndexChanged called when add one item.
Why user have not select item, that event is called
And how to fix this?
As far as I know the only way for you to fix this is by checking that SelectedIndex != -1 before doing anything else on the event handler method.

Confused with telerik event handlers

I'm not sure if this is right for getting a value of a multi combo box
when user selects something i need to get it's first column value and pass it to my DB
i'm trying to get a value from the telerik Multicombo box and idon't know wich event handler to use?
here's my code
private void radMultiColumnComboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
if (radMultiColumnComboBox3.MultiColumnComboBoxElement.Rows.Count > 0)
{
radMultiColumnComboBox3.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
var item = radMultiColumnComboBox3.MultiColumnComboBoxElement.Rows[0];
radGridView1.Rows.Add(item);
}
else
{
MessageBox.Show("PLS select a row", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
This is how you get the value of the first column when the user selects a row in the Telerik's MultiColumnComboBox:
void radMultiColumnComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var value = radMultiColumnComboBox1.EditorControl.Rows[radMultiColumnComboBox1.SelectedIndex].Cells[0].Value;
}
You can find the documentation of the SelectedIndexChanged event here.

WinForms DatagridViewComboboxColumn "enter key"

I have a datagridview with an editable combobox column, but everytime I press "Enter" on the current combobox, the text I'm writing disappears.
private void dgView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (dgView1.CurrentCell.IsInEditMode)
{
if (dgView1.CurrentCell.GetType() == typeof(DataGridViewComboBoxCell))
{
if (!((DataGridViewComboBoxColumn)dgView1.Columns[e.ColumnIndex]).Items.Contains(e.FormattedValue))
{
((DataGridViewComboBoxColumn)dgView1.Columns[e.ColumnIndex]).Items.Add(e.FormattedValue);
}
}
}
}
private void dgView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
{
ComboBox cbo = (ComboBox)e.Control;
cbo.DropDownStyle = ComboBoxStyle.DropDown;
}
}
I also tried adding the event handlers: "on key press", "on key down" and "on key up" but same problem happens.
How can I keep the current text when I press "Enter" key?
The DataGridViewComboBoxColumn does not accept any value which is not contained in the Items collection. So when user types in a new value, the current cell does simply not store the value after editing. You have to find another way to get the last value right after cell being edited. We can get the actual DataGridViewComboBoxEditingControl in the EditingControlShowing event handler, that control is actually a ComboBox, we can handle the TextChanged event. The best DataGridView event to handle to submit the new value (add to Items and show in the current cell) is the CellEndEdit event. So here is the code you should do, I've tested it and looks like it works as what you expected:
//use some variable to store the last edited value
string editingValue;
//EditingControlShowing event handler
private void dataGridView1_EidtingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e) {
var combo = e.Control as ComboBox;
if(combo != null){
combo.DropDownStyle = ComboBoxStyle.DropDown;
combo.TextChanged += (s,ev) => {
editingValue = combo.Text;
};
}
}
//CellEndEdit event handler for your dataGridView1
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e){
var comboColumn = dataGridView1.Columns[e.ColumnIndex] as DataGridViewComboBoxColumn;
if(comboColumn != null && editingValue != "" &&
!comboColumn.Items.Contains(editingValue)){
comboColumn.Items.Add(editingValue);
dataGridView1[e.ColumnIndex, e.RowIndex].Value = editingValue;
}
}
Note that the cell value is supposed to be string which is why the editingValue is declared as string, otherwise, you may have to convert the Text of the editing comboBox to the correct type of editingValue.

Action immediately after selecting Item

I have this code:
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "blahblah")
{
processing ps = new processing();
pictureBox1.Image = ps.blahblah(bmp);
}
else
{...
}
}
So the action of the ComboBox is done by clicking on the button1.
It is possible to take action immediately after selecting Item? without button clicking?
Subscribe to the SelectedIndexChanged event
comboBox1.SelectedIndexChanged += OnSelectedIndexChanged;
private void OnSelectedIndexChanged(object sender, EventArgs e) {
// Handle combo box changing
}
Try using this event,
ComboBox1.SelectedIndexChanged
and do
AutoPostBack = "true"
in your mark up if you want to check the selected item immediately after selecting item.

Intercept button click on page load

I need to write a code that can intercept a click of some button (asp button) than execute some code, and if a method return true, call the original click.
So the points are:
1- I don´t know how to save the original click.
2- Identify the button that was clicked.
Ex:
protected void Page_Load(object sender, EventArgs e)
{
var b = getButtonThatWasClicked();
var originalClick = b.Click;
if(callSomeMethod(b))
originalClick(null,null);
}
EDIT:
Ok managed to get the button who made the click doing this...Now i need to prevent the original Click to get called. The method bellow didn't worked. Even overriding the original click to a new Handler the old Handler got called and executed. I thing ASP.NET read it and make something like a call stack of events to get called.Even if the handler change the old still in the stack.
public void ButtonsTestMethod()
{
var listOfButtons = listaDeBotes.Where(b => b.CommandName != "");
foreach (var button in listOfButtons)
{
if (Request.Form[button.UniqueID] != null)
{
var buttonFromRequest = Request.Form[button.UniqueID];
if (buttonFromRequest == null)
continue;
if (button.CommandName != "xxx")
{
//validate things
//if(TemPermissao(Tela,GetAcaoDoBotao(botao.CommandName)))
//if(!canexecuteSomething())
button.Click += new EventHandler(defaultClick);
}
}
}
}
void defaultClick(object sender, EventArgs e)
{
//show error
}
protected void Page_Load(object sender, EventArgs e)
{
//other code
ButtonsTestMethod();
}
I don´t know if its possible but would appreciate some help =/
Thanks.
To get the control name, you can try the following in the page load:
protected void Page_Load(object sender, EventArgs e)
{
if( IsPostBack )
{
string senderControl = Request.Params["__EVENTTARGET"].ToString();
//senderControl will contain the name of the button/control responsible for PostBack
}
}
The first argument in the button click event handler is the sender. You can cast that sender as a Button object and then you should be able to identify which button that was based on that object. That way, you can eliminate having that function to figure out which is clicked.
void GreetingBtn_Click(Object sender, EventArgs e)
{
Button clickedButton = (Button)sender;
if(clickedButton.Text == "bla")
{
//Do stuff
}
}

Categories