I am trying to add a radio button click event at runtime.
Radiobutton button = new RadioButton();
button.GroupName = "buttonGroup";
button.OnCheckedChanged = "buttonGroup_OnCheckedChanged"; //I can't do this?
I know I can do this from the markup, but when I try to do this from the code behine, I cant find OnCheckedChanged.
Thanks for your help.
button.CheckedChanged += new EventHandler(buttonGroup_OnCheckedChanged);
button.CheckedChanged += buttonGroup_OnCheckedChanged;
I believe that is what you want.
Then you would obviously define buttonGroup_OnCheckedChanged and so on.
The handler should be created on page Init, the other answers are otherwise correct.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
button.CheckedChanged += new EventHandler(buttonGroup_OnCheckedChanged);
}
Related
I have a Button1 and a textbox. When i put a value in textbox and click this button then a new button is created with the value filled in textbox but i want that when again i click Button1 and there should be one more button or we can say how to move previous button's position?
This is code for Button1 in .aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
Button btnNew = new Button();
btnNew.ID = "btnNew";
btnNew.Text = textBox1.Text;
form1.Controls.Add(btnNew);
}
Please help me to solve this.
Thanks in advance.
You could probably do something like this with JavaScript/jQuery:
$('#button1').click(function() {
$('#button2').css('position', 'absolute');
$('#button2').css('top', '50px');
});
etc. You should provide more info as suggested by Matt.
I am trying to create a dynamic ModalPopupExtender from code behind that when i click on a button it pops a panel with a button in side.
i created a panel (named panel) with a button in side called ButtonOk (button.id ="ButtonOk")
but when i click the event handler of the first button (Button_Click) nothing happens please help me my code is:
protected void Button_Click(object sender, EventArgs e)
{
HiddenField hf = new HiddenField();
hf.ID = "hdnField";
AjaxControlToolkit.ModalPopupExtender modalPop = new AjaxControlToolkit.ModalPopupExtender();
modalPop.ID = "ModalPopupExtenderSelectFilds";
modalPop.PopupControlID =Convert.ToString(Page.FindControl(Convert.ToString(panel.ClientID))); //panel.ID;
modalPop.TargetControlID = Convert.ToString(Page.FindControl(Convert.ToString(hf.ClientID))); //"hdnField";
modalPop.OkControlID = "ButtonOk";
modalPop.BackgroundCssClass = "modalBackground";
modalPop.BehaviorID = "modalPopupExtenderSelectFilds";
modalPop.Show();
}
For help of other users, you will just have to add modalPop to area where control will be added to page
For example, this should be added at end of code
panel1.Controls.Add(modalPop);
check this code to add controls to panel:-
this.panel1.Controls.Add(modalPop);
In code behind, you can make this:
if (true)
{
var script = #"Sys.Application.add_load(function() { $find('behavoirIDModal').show(); });";
ScriptManager.RegisterStartupScript(this, GetType(), "ShowPopup", script, true);
}
edit this: behavoirIDModal
I would like to add and Image along with the HeaderText in RadGrid. I can able to do this in ItemBound event. But is there any possible ways to do the same in page prerender event?
I got it worked, here is the code to add the image in pre render event.
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
GridHeaderItem headerItem = (GridHeaderItem)RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0];
Image img = new Image();
img.ImageUrl = "~/Images/Refresh.gif";
headerItem["FirstName"].Controls.AddAt(1, img);
}
is possible, but do not know if it's a good idea.
((GridHeaderItem)((GridTHead)grid.MasterTableView.Controls[0].Controls[0]).Controls[1]).Cells[2].Text= "Test!"
it is good to check the types of controls.
I have a ListView that has a Button in the item template. Is there a way for me to identify which Item had it's button clicked from my OnClick event?
I was able to do it with the code below but it seemed crufty. Is there a better way to do this?
((ListViewDataItem)((Button)sender).Parent.Parent)
UPDATE:
Was able to implement using the NamingContainer method that one user suggested and then mysteriously removed his answer. Seems like a safer way to do than my original method:
((ListViewDataItem)((Button)sender).NamingContainer)
Yes, give the button a command name, then attach to ListView.ItemCommand; clicking the button fires this event, and it has some more specifics about the list item, such as a reference to it via e.Item.
HTH.
protected void RemoveButton_Click(object sender, EventArgs e)
{
ListViewDataItem item = ((ListViewDataItem)((Button)sender).NamingContainer);
//ListViewDataItem item = (ListViewDataItem)((LinkButton)sender).Parent;
int i = item.DisplayIndex;
DataTable dt = (DataTable)Session["cart"];
dt.Rows[i].Delete();
Listcart.DataSource = dt;
Listcart.DataBind();
Label Lblcart = (Label)Page.Master.FindControl("Lbitem");
Lblcart.Text = Listcart.Controls.Count.ToString();
Session["quantity"] = Lblcart.Text;
Session["cart"] = dt;
GrandTotal();
Session["amount"] = LbGrandTotal.Text;
}
I am adding controls dynamically during runtime using a conrolplace holder. i want to add buttons and handle their event. they will do the same thing but with different parameter. here is a sample of the code:
while (dataReader.Read())
{
Button edit = new Button();
PlaceHolderQuestions.Controls.Add(edit);
}
i need to handle the event of the buttons. Thanks
A couple of things:
Firstly you need to make sure the new Controls are added in the Page.OnInit event, so that they are added before the raised events are processed.
They also need to be added again on a postback!
They also need to have a unique ID set.
Finally you can handle the event just like you would in any C# app:
edit.Click += new EventHander(EditButton_Click);
and later in the code:
protected void EditButton_Click(object sender, EventArgs e)
{
// Do Something
}
You can just create a method, then add:
edit.Click += YourMethodName;
As long as the same button is created on postback before the eventhandler is raised, the event will fire.