I have created link button from code behind and binded with action like this
but this method Render is never invoked on click event. What I missed here ?
Panel pnlMain = new Panel();
Panel pnlContent = new Panel();
LinkButton lbContent = new LinkButton();
lbContent.Click += new EventHandler(Redirect);
pnlContent.Controls.Add(lbContent);
pnlMain.Controls.Add(pnlContent);
private void Redirect(object sender, EventArgs e)
{
}
Try this,
Panel pnlMain = new Panel();
Panel pnlContent = new Panel();
LinkButton lbContent = new LinkButton();
lbContent.Click += new EventHandler(Redirect);
Redirect(this, EventArgs.Empty);
private void Redirect(object sender, EventArgs e)
{
}
Are you missing runat="server" tag? however the compilator should complain about that.
Try this it will work.
In your aspx page
<form id="form1" runat="server">
<div>
</div>
</form>
And the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
Panel pnlMain = new Panel();
Panel pnlContent = new Panel();
LinkButton lbContent = new LinkButton();
lbContent.Text = "click";
lbContent.Click += new EventHandler(Redirect);
pnlContent.Controls.Add(lbContent);
pnlMain.Controls.Add(pnlContent);
this.form1.Controls.Add(pnlMain);
}
private void Redirect(object sender, EventArgs e)
{
}
Related
I'm trying to add a LinkButton dynamically
This is the html code:
<div id="resultDivText" runat="server">
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
</asp:PlaceHolder>
</div>
This is the c# code
LinkButton lb = new LinkButton();
lb.Text = songName + "</br>"; //LinkButton Text
lb.ID = song.Key.ToString(); // LinkButton ID’s
lb.CommandArgument = Convert.ToString(song.Key);
lb.CommandName = Convert.ToString(song.Key);
lb.Click += new EventHandler(test_Click);
this.form1.Controls.Add(lb);
PlaceHolder1.Controls.Add(lb);
And this is the "test_Click" function
protected void test_Click(object sender, EventArgs e)
{
showAllSong("let it be");
}
When i run the code it's show me the linkButton list but when i click on it nothing happens.
Try this code ..
static bool enable = false;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DynamicButton();
}
else if (enable)
{
DynamicButton();
}
}
protected void btnBindMapping_Click(object sender, EventArgs e)
{
enable = true;
DynamicButton();
}
protected void DynamicButton()
{
LinkButton lb = new LinkButton();
lb = new LinkButton();
lb.Text = songName + "</br>"; //LinkButton Text
lb.ID = song.Key.ToString(); // LinkButton ID’s
lb.CommandArgument = Convert.ToString(song.Key);
lb.CommandName = Convert.ToString(song.Key);
lb.Click += new EventHandler(test_Click);
this.form1.Controls.Add(lb);
PlaceHolder1.Controls.Add(lb);
}
protected void test_Click(object sender, EventArgs e)
{
Response.Write("<script>alert('done'); </script>");
}
You code is fine ..it works as expected ..But change this line
this.form1.Controls.Add(lb);
to
this.Controls.Add(lb);
Note:It postback to your server not a client sided event
Since you use CommandName and CommandArgument in the LinkButton, you have to delegate a Command not a Click.
lb.Command += new CommandEventHandler(test_Click);
protected void test_Click(object sender, CommandEventArgs e)
{
Response.Write(e.CommandArgument + "<br>" + e.CommandName);
}
And remove the line this.form1.Controls.Add(lb);
And this is not clear from your question, but you have to create that button every time the page is loaded, and that includes a PostBack.
In give detail you have not mention. when this dynamic button is created.
Because that may create a issue.
If you call your c# code in page load event then it works.
LinkButton lb = new LinkButton();
lb = new LinkButton();
lb.Text = songName + "</br>"; //LinkButton Text
lb.ID = song.Key.ToString(); // LinkButton ID’s
lb.CommandArgument = Convert.ToString(song.Key);
lb.CommandName = Convert.ToString(song.Key);
lb.Click += new EventHandler(test_Click);
this.form1.Controls.Add(lb);
PlaceHolder1.Controls.Add(lb);
if that button create dynamically on oninit or onload event then it works.
Button won't call it's event.
called in another button:
placeHolder.Controls.Add(CreateButton());
create button:
public Button CreateButton()
{
Button btn = new Button();
btn.ID = "id";
btn.Text = "some text";
btn.Attributes.Add("onclick", "return false;");
btn.Click += new EventHandler(btn_Click);
return btn;
}
Functionality:
private void btn_Click(object sender, EventArgs e)
{
// do something.
}
places debug lines to find the source, it's simply not calling btn_Click() when clicked. What's missing?
This code prevents the click event from firing:
btn.Attributes.Add("onclick", "return false;");
Remove this code, or change it to:
btn.Attributes.Add("onclick", "return true;");
EDIT
I am tried this code and it worked correctly. PlaceHolder is in form tag and runat attribute is server:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
placeHolder.Controls.Add(CreateButton());
}
public Button CreateButton()
{
Button btn = new Button();
btn.ID = "id";
btn.Text = "some text";
btn.Click += btn_Click;
return btn;
}
private void btn_Click(object sender, EventArgs e)
{
}
Here is my code
protected void Button1_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
Label newLabel = new Label();
newLabel.ID = "lbltest";
newLabel.Text = "my new label..";
panel1.Controls.Add(newLabel);
}
I cant see the my label text when i click the button
Any help appreciate
Thanks
You must add your panel inside of any control which exists on your page.
You have to add the Panel to some control in your web page or your top level form element if you don't have anywhere else to put it.
protected void Button1_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
Label newLabel = new Label();
newLabel.ID = "lbltest";
newLabel.Text = "my new label..";
panel1.Controls.Add(newLabel);
this.Form.Controls.Add(panel1); // YOU ARE MISSING THIS
}
You need to add the Panel to the page:
protected void Button1_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
Label newLabel = new Label();
newLabel.ID = "lbltest";
newLabel.Text = "my new label..";
panel1.Controls.Add(newLabel);
//Do this
SomeControlOnYourPage.Controls.Add(panel1);
}
Any help would be appreciated --
here is my aspx code -
<div id="div_Detail" class="div_det" runat="server" />
and in my code behind -
protected void Page_Load(object sender, EventArgs e) {
LoadDetail();
}
private void LoadDetail() {
HtmlTable tbl = new HtmlTable();
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
Button bUpdate = new Button();
bUpdate.Text = "Update";
bUpdate.Click += this.Update_Click;
cell.Controls.Add(bUpdate);
row.Cells.Add(cell);
tbl.Rows.Add(row);
div_Detail.Controls.Add(tbl);
}
private void Update_Click(object sender, EventArgs e) {
//Do something
}
I see the button on the page with the correct text. But on clicking that button, Update_Click never gets called.
Am I missing something?
Try giving the button an id. The .NET engine needs IDs on controls during postback to associate the control with a given event delegate.
Edit:
This code works fine:
protected void Page_Load(object sender, EventArgs e) {
LoadDetail();
}
private void LoadDetail() {
HtmlTable tbl = new HtmlTable();
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
Button bUpdate = new Button();
bUpdate.Text = "Update";
bUpdate.Click += this.Update_Click;
bUpdate.ID = "btnID";
cell.Controls.Add(bUpdate);
row.Cells.Add(cell);
tbl.Rows.Add(row);
div_Detail.Controls.Add(tbl);
}
private void Update_Click(object sender, EventArgs e) {
//Do something
}
Give the button an Id and everything will work fine.
bUpdate.Id = "btnUpdate";
Try This one may help u
bUpdate.Click += new System.EventHandler(this.bUpdate_OnClick);
protected void bUpdate_OnClick(object sender, EventArgs e) {
}
protected void Button1_Click(object sender, EventArgs e)
{
TableRow tb = new TableRow();
TableCell tc = new TableCell();
DropDownList db = new DropDownList();
db.Items.Add("Bangalore");
db.Items.Add("Mandya");
db.Items.Add( "Hassan");
tc.Controls.Add(db);
tb.Controls.Add(tc);
Table1.Controls.Add(tb);
db.SelectedIndexChanged += db_SelectedIndexChanged;
db.AutoPostBack = true;
}
private void db_SelectedIndexChanged(object sender, EventArgs e)
{
label.text = "welcome";
}
When this code executes in the Button1_Click event, db_SelectedIndexChanged doesn't execute. However, when I place the same Button1_Click code block in the Page_Load event, db_SelectedIndexChanged executes.
What may be the reason behind this?
Try to put
db.SelectedIndexChanged += db_SelectedIndexChanged;
db.AutoPostBack = true;
In the Page_Load event.
Don't wrap db.SelectedIndexChanged += db_SelectedIndexChanged; in !Page.IsPostBack as the events need to be wired up on each load
You are creating a dynamic control. The event will not fire unless you create the control in the PreInit method of the page.
protected void Page_PreInit(object sender, EventArgs e)
{
DropDownList db = new DropDownList();
db.Items.Add("Bangalore");
db.Items.Add("Mandya");
db.Items.Add( "Hassan");
db.SelectedIndexChanged += db_SelectedIndexChanged;
db.AutoPostBack = true;
tc.Controls.Add(db);
}
Check Page Life cycle for more info.
protected void Button1_Click(object sender, EventArgs e)
{
TableRow tb = new TableRow();
TableCell tc = new TableCell();
DropDownList db = new DropDownList();
db.Items.Add("Bangalore");`
db.Items.Add("Mandya");
db.Items.Add( "Hassan");
tc.Controls.Add(db);
tb.Controls.Add(tc);
Table1.Controls.Add(tb);
db.SelectedIndexChanged += db_SelectedIndexChanged;
db.AutoPostBack = true;
db_SelectedIndexChanged(null,null); // use this line, i hope it will work now.
}
private void db_SelectedIndexChanged(object sender, EventArgs e)
{
label.text = "welcome";
}
You can try this one.