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";
}
Related
I have created 2 radwizard steps dynamically, i want to fire an ajax request on step change. How can i do this?
Thanks.
Markup:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WizardUCDemo.ascx.cs" Inherits="WebUIFrameWork.UITemplate.WizardUCDemo" %>
<div style="overflow:inherit">
<telerik:RadWizard runat="server" RenderMode="Lightweight" ID="wizardControl"
DisplayProgressBar="false" ProgressBarPosition="Right" NavigationBarPosition="Right"
NavigationButtonsPosition="Bottom" DisplayNavigationButtons="false">
</telerik:RadWizard>
</div>
userControl.ascx.cs:
public void GenerateSteps()
RadWizardStep step;
for (int i = 0; i < 2; i++)
{
step = new RadWizardStep();
step.ClientIDMode = ClientIDMode.Static;
step.ID = "step_1"+i.ToString();
wizardControl.WizardSteps.Add(step);
}
aspx.cs:
protected void rbtnAddControl_Click(object sender, EventArgs e)
{
WizardUCDemo.GenerateSteps();
}
protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
}
image
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");
}
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;
}
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.
I have the feeling that i'm missing something key here.
I've tried following guides on http://msdn.microsoft.com/en-us/magazine/cc300437.aspx
and on google, but i can't see what i haven't done.
I have some very basic code which i have written just trying to get this to work:
The Default.aspx code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableSessionState="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Demo Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="myLabel" runat="server" Text="foo"></asp:Label>
<asp:LinkButton ID="lnkClickButton" runat="server" OnClick="lnkClickButton_Click" CommandName="Clicky">Click Me</asp:LinkButton>
</div>
</form>
</body>
</html>
The Default.aspx.cs code:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session["clickcount"] = 0;
Cache["clickscount"] = 0;
}
protected void lnkClickButton_Click(object sender, EventArgs e)
{
Session["clickcount"] = (int)Session["clickcount"] + 1;
Cache["clickscount"] = (int)Cache["clickscount"] + 1;
Label myLabel = ((Label)this.FindControl("myLabel"));
if (myLabel != null)
{
myLabel.Text = "Session: " + Session["clickcount"] + "; Cache: " + Cache["clickscount"] + ";";
}
}
}
I've tried using both the session object and the cache object to increment the values, but to no avail. I just get 1 every time.
N.B. this is my first asp.net project, also i'm fairly new to c#.
Page_Load is ran every postback as well as the initial load. You need to specify no postback in your Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack){
Session["clickcount"] = 0;
Cache["clickscount"] = 0;
}
}
Better still, specify that it should only be set if it doesn't already have a value:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["clickcount"] == null){
Session["clickcount"] = 0;
}
}
Just to clarify, the reason that it is better to only set the value if it isn't already set is that Page.IsPostBack is false every time someone directly visits the page. Say for instance you have your page http://example.com/Demo/Default.aspx and at the top you have a logo which you wrap in logo here, the session would be reset every time somebody clicked on the logo, even though they didn't actually leave the page. Also happens if they refresh on their browser without re-posting the last post.
Read on MSDN : Page.IsPostBack Property - Gets a value that indicates whether the page is being rendered for the first time or is being loaded in response to a postback.
vlue of property is true if the page is being loaded in response to a client postback; otherwise, false.
code like this ...you need to put the code in !IsPostBack as like below
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Session["clickcount"] = 0;
Cache["clickscount"] = 0;
}
}
serverside control generate postback to page it self so the code that you dont wnat to execute on each postbck need to be put as above
this will resolve your issue easily...
further to this you can create static property for count like this
Check my post on : Programming Practice for Server Side State Maintenance Variable
private int ClickCount
{
get
{
if (Session["clickcount"] == null)
{ Session["clickcount"] = 0; return 0; }
else
return (int)Session["clickcount"] ;
}
set
{
Session["clickcount"] = value;
}
}
than in final code
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
ClickCount = 0;
}
}
protected void lnkClickButton_Click(object sender, EventArgs e)
{
int val = ClickCount ;
ClickCount = val + 1;
}
Writing:
Session["clickcount"] = 0;
In the Page_Load will cause the counter to be reset every time the user enters the page.
Seems to me you want something like this:
protected void lnkClickButton_Click(object sender, EventArgs e)
{
if (Session["clickcount"] == null)
{
Session["clickcount"] = 1;
}
else
{
Session["clickscount"] = (int)Session["clickscount"] + 1;
}
Label myLabel = ((Label)this.FindControl("myLabel"));
if (myLabel != null)
{
myLabel.Text = "Session: " + Session["clickcount"] + ";
}
}
You get 1 because every post back your session and cache variable equal to 0.
protected void Page_Load(object sender, EventArgs e)
{
Session["clickcount"] = 0;
Cache["clickscount"] = 0;
}
And button click occur after the page_load, So you should use IsPostback property.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Session["clickcount"] = 0;
Cache["clickscount"] = 0;
}
}
Now these variables initialize only when page is loaded.
You should go through following link. It is described Asp.net page life.
http://msdn.microsoft.com/en-us/library/ms178472.aspx