How to move a dynamically created button? - asp.net

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.

Related

how I find a control in datalist

I have taken a datalist in my asp.net project which contains some picture and picture name. Now I want to show each picture in another page after. It means if any user will click on the picture then picture will open with it's details in another page.So I want to know that how I find a control in datalist.
In following picture of datalist I want to click on picture name like Jai Ho and It will open in another page.
You can use the OnClientClick property of Link Button.
In <asp:LinkButton> tag add following code
OnClientClick="javascript:window.open('your_url');"
try this
protected void up-movie-name_Click(object sender, EventArgs e)
{
Label4.Text = (Datalist1.SelectedItem.FindControl("Label1").ToString());
}
or
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
LinkButton linkButton = e.Item.FindControl("LinkButton1") as LinkButton;
string commandText = linkButton.Text;
}
}

Get asp.net button html

I have mostly worked with MVC but now doung a project in asp.net. I can't figure out how to render an anchor button, link button or whatever from code behind in a block of text. This button will do postback.
Example: The following errors occured, Click Here.
How do you get the html of that button and append it to the text?
You can add a button to placeholder, like this:
In markup, add this:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
In code, add this:
protected void Page_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.ID = "myButton";
btn.Text = "Dynamic Button";
btn.UseSubmitBehavior = true;
btn.Click += new EventHandler(myButton_Click);
PlaceHolder1.Controls.Add(btn);
}
And access the button's click event :
protected void myButton_Click(object sender, EventArgs e)
{
// Do whatever you need
}
NOTE: You have to recreate all dynamically created controls after postback! In this example code to create the button is placed in Page_Load so that it will be recreated at each postback.

create a dynamic ModalPopupExtender from code behind does not work

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

Row Editing event not firing on Edit button click

I have a gridview with edit and delete buttons on each row.
I am trying to change the back color or the fore color of the row in the gridview on which the edit button is clicked.
I am using the Row Editing event .Below is my code, the row editing event is not getting fired when I click the edit button.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
ClearBackColor();
GridView1.SelectedIndex = e.NewEditIndex;
GridView1.SelectedRow.BackColor = System.Drawing.Color.Red;
//GridViewRow row = GridView1.Rows[e.NewEditIndex];
//row.ForeColor = Color.DarkGreen;
}
What could be wrong? Please let me know.
Check http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx. You are probably missing CommandName="Edit" on button, so RowEditing will never fire.
remove this line : GridView1.SelectedIndex = e.NewEditIndex;
right click on gridview, and click properties. Check for "EditRowStyle",expand it, there will be BackColor, choose the color you want, and its done.

Adding radio button event handler at runtime

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);
}

Resources