onserverclick not firing c# when properties added in runtime - c#

I'm adding a button in runtime in C# web forms. I need to call a function btnEdit_click when the button is called. Somehow the function isn't being called.
The codes are below. Please help
protected void btnEdit_Click(object sender, EventArgs e)
{
Response.Redirect("~/Setup.aspx");
}
HtmlGenericControl EditButton = new HtmlGenericControl("button");
EditButton.Attributes["class"] = "btn btn-default";
EditButton.Attributes["id"] = "editButton";
EditButton.Attributes["runat"] = "server";
EditButton.Attributes["OnServerClick"] = "btnEdit_Click";
EditButton.InnerText = "Edit";

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
HtmlButton EditButton = new HtmlButton();
EditButton.Attributes["class"] = "btn btn-default";
EditButton.Attributes["id"] = "editButton";
EditButton.Attributes["runat"] = "server";
EditButton.InnerText = "Edit";
EditButton.ServerClick += btnEdit_Click;
this.form1.Controls.Add(EditButton);
}
protected void btnEdit_Click(object sender, EventArgs e)
{
Response.Redirect("~/Setup.aspx");
}

Related

Programatically added HTML Button - OnServerClick doesn't work

Default.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html>
<head>
<title>Test Case</title>
</head>
<body>
<div id="testdiv" runat="server"></div>
</body>
</html>
Default.aspx.cs:
using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
testdiv.InnerHtml = "<input type=\"button\" runat=\"server\" OnServerClick=\"Test_Click\"/>";
}
protected void Test_Click(object sender, EventArgs e)
{
testdiv.InnerHtml = "REGISTERED CLICK!";
}
}
I want Test_Click to be run on the server when the button is clicked, but it doesn't.
I tried putting it in a server side form, but it still doesn't work.
try this way,
protected void Page_Load(object sender, EventArgs e)
{
var button = new Button {ID = "Button1", Text = "Test Button"};
button.Click += button_Click;
PlaceHolder1.Controls.Add(button);
}
private void button_Click(object sender, EventArgs e)
{
testdiv.InnerHtml = "Button1 is clicked";
}

Dynamic created radio buttons wont execute event on click

I have a button named "Add Radio Button" in my form, and a text box named TexBox1.
I've written code that when I click the "Add Radio Button", it generates a Radio button; it's own name:
c#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["Counter"] = 0;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i <= Convert.ToInt32(ViewState["Counter"]); i++)
{
HtmlGenericControl div = new HtmlGenericControl("div");
RadioButton rb = new RadioButton();
rb.ID = i.ToString();
rb.Text = "Button" + i.ToString();
rb.GroupName = "RB";
rb.CheckedChanged += radioButton_CheckedChanged;
div.Controls.Add(rb);
Panel1.Controls.Add(div);
}
ViewState["Counter"] = Convert.ToInt32(ViewState["Counter"]) + 1;
}
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton btn = sender as RadioButton;
TextBox1.Text = btn.Text;
}
}
ASP
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add RadioButton" />
</div>
</form>
</body>
</html>
The problem is that after several radio buttons being created, I want the program to work as if each of them is checked, the TextBox1.Text gets the radio button's text string (the name of button) but the function radioButton_CheckedChanged doesn't execute.
You missed this I think
rb.CheckedChanged +=new EventHandler(rb_CheckedChanged);
Check my comments below also
you should set as true to Autopostback property
rb.AutoPostBack = true;
this is how its should be imo:
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i <= Convert.ToInt32(ViewState["Counter"]); i++)
{
HtmlGenericControl div = new HtmlGenericControl("div");
RadioButton rb = new RadioButton();
rb.ID = i.ToString();
rb.Text = "Button" + i.ToString();
rb.GroupName = "RB";
rb.CheckedChanged += +=new EventHandler(radioButton_CheckedChanged);
div.Controls.Add(rb);
Panel1.Controls.Add(div);
}
ViewState["Counter"] = Convert.ToInt32(ViewState["Counter"]) + 1;
}
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton btn = sender as RadioButton;
TextBox1.Text = btn.Text;
}

Dynamically create TextBoxes, adding an extra one each time the user clicks a button

I am trying to dynamically create TextBoxes in ASP.NET, my code isn't working the way I expect it to...
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public int TextBoxCount
{
get
{
if (ViewState["tbCount"] == null)
{
ViewState["tbCount"] = 0;
}
return Convert.ToInt32(ViewState["tbCount"]);
}
set
{
int viewState = TextBoxCount;
if (Int32.TryParse(value.ToString(), out viewState))
{
ViewState["tbCount"] = value;
}
}
}
protected void Page_Init(object sender, EventArgs e)
{
if (TextBoxCount == 0)
{
AddTextBox();
}
else
{
RecreateTextBoxes();
}
}
private void AddTextBox()
{
TextBox tb = new TextBox();
tb.ID = "tb" + TextBoxCount++;
Panel1.Controls.Add(tb);
}
private void RecreateTextBoxes()
{
for (int i = 0; i < TextBoxCount; i++)
{
TextBox tb = new TextBox();
tb.ID = "tb" + i;
Panel1.Controls.Add(tb);
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
AddTextBox();
}
protected void btnDisplayText_Click(object sender, EventArgs e)
{
for (int i = 0; i < TextBoxCount; i++)
{
TextBox tb = (TextBox)Page.FindControl("Panel1").FindControl("tb" + i);
if (tb != null)
{
lblText.Text += "," + tb.Text;
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="lblText" runat="server" />
<div>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</div>
<asp:Button ID="btnDisplayText" runat="server" Text="Display Text" onclick="btnDisplayText_Click" />
<asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" />
</form>
</body>
</html>
What I am trying to do is dynamically create a new TextBox each time the user clicks the btnAdd button. The btnDisplayText button should then concatenate all of the text in each TextBox and display it in lblText.
Thanks for your help!
Use
TextBox tb = (TextBox)Panel1.FindControl("tb" + i);
instead of
TextBox tb = (TextBox)Page.FindControl("Panel1").FindControl("tb" + i);
in btnDisplayText_Click.
Also, remove all code except ViewState["tbCount"] = value; from TextBoxCount setter.
Update:
ViewState is not available in Page_Init. Move your Page_Init code to Page_Load.
int TextBoxID=0;
TextBox textBox = new TextBox();
TextBox.ID="TextBox"+TextBoxID.ToString();
btnDisplayText.Text +=textBox.Text;
lblText.Text=btnDisplayText.Text;
TextBoxID++;

asp.net redirect a page in NEW TAB on dropdown list onselectedindexchanged by passing query string

This is my code. Plz help me to redirect the page in new tab.
It can be either by script or c# code.
Thanks in advance.
<asp:DropDownList ID="ddlcomplaint" CssClass="list-item-width1" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlcomplaint_SelectedIndexChanged" onchange="aspnetForm.target ='_blank';">
protected void ddlcomplaint_SelectedIndexChanged(object sender, EventArgs e)
{
txtTitle.Text = "";
int complaint = Convert.ToInt16(ddlcomplaint.SelectedValue);
if (complaint == 100)
{
txtTitle.Enabled = true;
}
else
{
txtTitle.Enabled = true;
txtTitle.Text = ddlcomplaint.SelectedItem.Text.ToString();
}
string Title = ddlcomplaint.SelectedItem.Text;
Response.Redirect("/SearchComplaint.aspx?Title=" + Title);
}
JavaScript
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function Redirect() {
var ddl1 = document.getElementById('<%= ddlcomplaint.ClientID %>');
var selectedval = ddl1.options[ddl1.selectedIndex].value;
window.open("/SearchComplaint.aspx?Title=" + selectedval );
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddl1" runat="server" onchange="Redirect();">
</asp:DropDownList>
</div>
</form>
</body>
</html>
or
c#
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(),"OpenWindow","window.open('/SearchComplaint.aspx?Title=" + Title');",true);
}

How do you add a Gridview programmatically to an UpdatePanel

I can't figure out how to programmatically add a GridView with buttons to an UpdatePanel.
I can do it with simple controls such as buttons and labels, but when I try to add a GridView with buttons a full Postback() occurs.
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected override void OnInit(EventArgs e)
{
UpdatePanel up1 = new UpdatePanel();
up1.ID = "UpdatePanel1";
Button button1 = new Button();
button1.ID = "Button1";
button1.Text = "Submit";
button1.Click += new EventHandler(Button_Click);
Label label1 = new Label();
label1.ID = "Label1";
label1.Text = "A full page postback occurred.";
GridView gv1 = new GridView();
//Where the xml gets bonded to the data grind
XmlDataSource xds = new XmlDataSource();
xds.Data = xml;
xds.DataBind();
xds.EnableCaching = false;
gv1.DataSource = xds;
createButton(gv1, up1);
gv1.RowCommand += new GridViewCommandEventHandler(CustomersGridView_RowCommand);
gv1.DataBind();
up1.ChildrenAsTriggers = true;
up1.ContentTemplateContainer.Controls.Add(button1);
up1.ContentTemplateContainer.Controls.Add(label1);
up1.ContentTemplateContainer.Controls.Add(gv1);
Page.Form.Controls.Add(up1);
}
protected void Page_Load(object sender, EventArgs e)
{
}
public void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "buttonClicked")
{
int index = Convert.ToInt32(e.CommandArgument);
}
}
void createButton(GridView g)
{
ButtonField tea = new ButtonField();
tea.ButtonType = ButtonType.Image;
tea.ImageUrl = "~/checkdailyinventory.bmp";
tea.CommandName = "buttonClicked";
tea.HeaderText = "space";
g.Columns.Add(tea);
}
protected void Button_Click(object sender, EventArgs e)
{
((Label)Page.FindControl("Label1")).Text = "Panel refreshed at " +
DateTime.Now.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>UpdatePanel Constructor Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button2" runat="server" Text="Button" />
<asp:ScriptManager ID="ScriptManager1" runat="server" />
</div>
</form>
</body>
</html>
So how do you add a gridview with buttons programmatically to an UpdatePanel without causing a full PostBack() if the GridView is clicked?
Edit: Other things I have tried
void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
AsyncPostBackTrigger t = new AsyncPostBackTrigger();
t.ControlID = e.Row.Cells[0].ClientID;
t.EventName = "blah";
up1.Triggers.Add(t);
}
Well according to:
And I don't mind having the update panel created at the design time. I just need to be able to add stuff (like tables that contain gridviews that contain buttons into it) programmatically and then be able to do a partial postback
Basically I used your code with small changes:
Removed the binding from the Init event and I execute it in the Load event
The UpdatePanel is created at design time with a nested panel, and you simply add your dynamic controls to that panel
This code will do it (it works on my environment):
ASPX
<asp:ScriptManager runat="server" />
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Panel runat="server" ID="myPanel">
</asp:Panel>
<br />
<asp:Label runat="server" ID="lblMessage"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
Code behind
protected void Page_Init(object sender, EventArgs e)
{
Button button1 = new Button();
button1.ID = "Button1";
button1.Text = "Submit";
button1.Click += new EventHandler(Button_Click);
Label label1 = new Label();
label1.ID = "Label1";
label1.Text = "A full page postback occurred.";
var s1 = Builder<Product>.CreateListOfSize(15).Build();
GridView gv1 = new GridView();
gv1.DataSource = s1;
createButton(gv1);
gv1.RowCommand += new GridViewCommandEventHandler(CustomersGridView_RowCommand);
this.myPanel.Controls.Add(button1);
this.myPanel.Controls.Add(label1);
this.myPanel.Controls.Add(gv1);
}
void createButton(GridView g)
{
ButtonField tea = new ButtonField();
tea.ButtonType = ButtonType.Image;
tea.ImageUrl = "~/checkdailyinventory.bmp";
tea.CommandName = "buttonClicked";
tea.HeaderText = "space";
g.Columns.Add(tea);
}
protected void Button_Click(object sender, EventArgs e)
{
((Label)Page.FindControl("Label1")).Text = "Panel refreshed at " +
DateTime.Now.ToString();
}
protected void Page_Load(object sender, EventArgs e)
{
this.DataBind();
}
public void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "buttonClicked")
{
//int index = Convert.ToInt32(e.CommandArgument);
this.lblMessage.Text = DateTime.Now.ToString();
}
}
Output
Couldn't you just put the GridView in there at design time and just hide it by setting Visible=false?
If you don't know how many gridviews you need to repeat then you could wrap the GridView in a ListView. The concept is introduced here:
http://mattberseth.com/blog/2008/01/building_a_grouping_grid_with.html
This might not be the perfect solution I just thought I would offer it seeing as there is a bounty I assume you have hit a brick wall so far.

Categories

Resources