Asp.net: Page won't refresh after clicking dynamically generated Button controls - c#

Clicking on any of the dynamically generated button controls does indeed call the b_Click method and delete the given user, however, upon deleting the page does not reload the 'new' list of users.
protected void Page_Load(object sender, EventArgs e)
{
DbDB db = new DbDB();
List<User> users = db.GetUsers().ExecuteTypedList<User>();
foreach (User u in users)
{
Button b = new Button();
b.Text = u.FirstName;
b.Click += new EventHandler(b_Click);
PlaceHolder1.Controls.Add(b);
}
}
}
void b_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
DbDB.User.Delete(x => x.FirstName == b.Text);
}

protected void Page_Load(object sender, EventArgs e) {
LoadUsers();
}
void b_Click(object sender, EventArgs e) {
Button button = (Button)sender;
string firstName = button.CommandArgument;
DbDB.User.Delete(x => x.FirstName == firstName);
PlaceHolder1.Controls.Remove(button);
}
void LoadUsers() {
DbDB db = new DbDB();
List<User> users = db.GetUsers().ExecuteTypedList<User>();
foreach (User user in Users) {
Button button = new Button();
button.CommandArgument = user.FirstName; // normally the user "id" to identify the user.
button.Text = user.FirstName;
button.Click += new EventHandler(b_Click);
PlaceHolder1.Controls.Add(button);
}
}

That's because the Page_Load event is called before the Click event so when you're retrieving the list of users from the database in Page_Load the user is still in there. As a quick solution you could move the code from Page_Load to PreRender event.
Have a look at this link for more info on the page life cycle: http://msdn.microsoft.com/en-us/library/ms178472.aspx

You don't need to select users with every post-back. Also you don't need to create controls at runtime for this.
Following is an alternative way.
<asp:Repeater runat="server" ID="myRepeater">
<ItemTemplate>
<asp:Button runat="server" OnClick="Button_Click" Text='<%# DataBinder.Eval(Container.DataItem, "FirstName") %>' />
</ItemTemplate>
</asp:Repeater>
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// load users
myRepeater.DataSource = users;
myRepeater.DataBind();
}
}
protected void Button_Click(object sender, EventArgs e)
{
// delete user
Button button = sender as Button;
button.Visible = false;
}

Write your page_load body inside
if(!IsPostBack)
{
....
}
That should work.

Related

User control click binding not working

I have a problem with my user control(SefAttributes.ascx). I have 2 textbox, 1 label and 1 button in my user control.In main page(SantiyeGiris2.aspx), I am data binding.I am adding this user control dynamically and I want to delete that row when user clicks delete button, but my click event not working.
My user control code behind like below:
public event EventHandler SefSilClicked;
protected void btnSefSil_Clicked(object sender, EventArgs e)
{
if (this.SefSilClicked != null)
{
this.SefSilClicked(this, e);
}
}
In SantiyeGiris2.aspx.cs I am binding like:
protected void rptSantifeSefDataBound(object sender, RepeaterItemEventArgs e)
{
SefAttributes sa = (SefAttributes)(e.Item.FindControl("sfDiv"));
SantiyePersonel sp = (SantiyePersonel)(e.Item.DataItem);
sa.txtSefBas.Text = sp.BaslangicFormatlanmis;
if (Convert.ToDateTime(sa.txtSefBas.Text) < System.DateTime.Now)
sa.txtSefBas.Enabled = false;
sa.txtSefBit.Text = sp.BitisFormatlanmis;
sa.lbl.Text = sp.PersonelTamAdi;
sa.SefSilClicked += new EventHandler(RptSefSilClicked);
}
and click function
protected void RptSefSilClicked(object sender, EventArgs e)
{
YP_SANTIYE.WindowScripts.Alert("click activated.");
}
I can't see the problem.I will grateful if anyone can help me.

How to change button text on page load based on listview value on Visual Studio

I want to change my button text on page load after retrieving the list view values.
For example,
<asp:Label ID="favouriteLabel" runat="server" Text='<%# Eval("favourite") %>' />
If this label value is 1, the button will change to Favourited.
I have retrieved the list view values by binding the listview
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label activity = (Label)e.Item.FindControl("favouriteLabel");
activityID = activity.Text;
}
}
then, I get the activityID and do a simple if-else check on the page load
protected void Page_Load(object sender, EventArgs e)
{
if (activityID == "1")
{
Button4.Text = "Favourited";
}
else
{
Button4.Text = "Favourite";
}
}
However it does not work. Anybody?
Do that inside a PostBack check in the load event, for instance:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (activityID == "1")
{
Button4.Text = "Favourited";
}
else
{
Button4.Text = "Favourite";
}
}
}
Read more about postback here
Page_Load happens before your ItemDataBound event so the activityId you are looking at in the Page_Load will never be 1.
Just put the code you have in the Page_Load into the ItemDataBoundEvent

Why does an ASP.NET TextBox.TextChanged not fire when the new text is blank?

I have a somewhat complicated setup for an ASP.NET TextBox. It is inside of a user control that's inside a repeater that's inside a repeater.
I am able to load text into the TextBox and also get TextChanged to fire when text is changed to anything EXCEPT FOR blank/empty. The event doesn't fire when the user clears out the TextBox.
Does anyone know why ASP.NET might discriminate between text and blank?
I made a simplified version of my problem to separate it from possible other factors. I get the same results.
Below is my aspx markup:
<div>
<asp:Repeater runat="server" ID="rptRepeat" EnableViewState="False">
<ItemTemplate>
<asp:repeater runat="server" ID="rptChild" EnableViewState="False">
<ItemTemplate>
<uc:Things runat="server" ID="ucChild"></uc:Things>
</ItemTemplate>
</asp:repeater>
</ItemTemplate>
</asp:Repeater>
</div>
<asp:Button runat="server" ID="btnBtn" Text="click"/>
Below is my user control markup
<asp:TextBox runat="server" ID="txtText"></asp:TextBox>
Below is the aspx code behind
private List<List<TestObj>> data = null;
protected void Page_Init(object sender, EventArgs e)
{
this.rptRepeat.ItemCreated += rptRepeat_ItemCreated;
this.rptRepeat.ItemDataBound += rptRepeat_ItemDataBound;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Session["data"] = data = new List<List<TestObj>>();
for (var x = 0; x < 5; x++)
{
data.Add(new List<TestObj>());
for (var y = 0; y < 5; y++)
{
data[0].Add(new TestObj
{
Text = x + "_" + y
});
}
}
}
else
{
data = (List<List<TestObj>>)Session["data"];
}
this.rptRepeat.DataSource = data;
this.rptRepeat.DataBind();
}
void rptRepeat_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var rptChild = (Repeater)e.Item.FindControl("rptChild");
rptChild.ItemDataBound += rptChild_ItemDataBound;
}
void rptRepeat_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var rptChild = (Repeater)e.Item.FindControl("rptChild");
var rowObj = (List<TestObj>)e.Item.DataItem;
rptChild.DataSource = rowObj;
rptChild.DataBind();
}
void rptChild_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var ucChild = (WebUserControl1)e.Item.FindControl("ucChild");
var rowObj = (TestObj)e.Item.DataItem;
ucChild.data = rowObj;
}
Below is the user control code behind
public TestObj data = null;
protected void Page_Init(object sender, EventArgs e)
{
this.txtText.TextChanged += txtText_TextChanged;
}
protected void Page_PreRender(object sender, EventArgs e)
{
this.txtText.Text = data.Text;
}
void txtText_TextChanged(object sender, EventArgs e)
{
data.Text = this.txtText.Text;
}
You can download the whole project at https://www.dropbox.com/s/p6zkqqsw71fvuyw/textchangetest.zip?dl=0 if you want to try tinkering with stuff to get me an answer.
The databinding of the repeater was intentionally put in Page_Load because otherwise child controls' events don't fire.

Troubles with dynamic c# asp.net controls update

I'm currently working on a website using c# and asp.net. For this purpose, I need to create dynamic controls but I enconter some issues. I already read official documentation and searched for lots of tutorial but unfortunately, no one allowed me to fix this problem.
Here is a very simplified example of what I'm trying to do;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
CreateControls();
else
UpdatePage();
}
protected void CreateControls()
{
Button button1 = new Button();
button1.ID = "_Button1";
button1.Text = "Button1";
button1.Click += new System.EventHandler(_ClickEvent);
_Panel.Controls.Add(button1);
Button button2 = new Button();
button2.ID = "_Button2";
button2.Text = "Button2";
button2.Click += new System.EventHandler(_ClickEvent);
_Panel.Controls.Add(button2);
}
protected void UpdatePage()
{
Button button1 = ((Button)_Panel.FindControl("_Button1"));
button1.Text = "I went through UpdatePage and changed";
Button button2 = ((Button)_Panel.FindControl("_Button2"));
button1.Text = "I went through UpdatePage and changed";
}
protected void _ClickEvent(object sender, EventArgs e)
{
}
The aim here would just be to change the buttons' text when clicking on one of them. "Page_Load" method is correctly called as well as the "UpdatePage" one, but in the latter, Button1 and Button2 controls have disappeared (they are not in the panel controls anymore) and an NullPointer exception is obviously raised.
Would anybody have an explanation ? I know I probably missed something about page life cycle but could not find any clear solution anywhere.
Thanks a lot !
You are creating the controls the first time the page is loaded, but the Page_Load event is too late to add controls to the page and have WebForms know about that.
On the initial page load, somewhere between the OnInit and Page_Load, WebForms makes a note of what controls are currently on the page and sets them up in the view state and all of that stuff, so that they next time you post back it knows those controls should be there. If you don't add your controls until Page_Load, WebForms isn't really paying attention any more to what you're adding to the page, so they next time you post back it doesn't know to put those controls on the page.
Move your CreateControls call into the OnInit method. This will tell WebForms at the appropriate time to create the controls (about the same time that any controls from the .aspx markup get added, although slightly later). Then WebForms will be aware of those controls and will apply any view state necessary (if it's a postback), and then finally on Page_Load you can muck with the control data with your UpdatePage call.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CreateControls();
}
Think of OnInit as "put all of the controls on the page and wire up event handlers".
Think of Page_Load as "put data into the controls that are already there".
The controls that you are creating dynamically will be lost on postback. Try this:
protected void Page_Load(object sender, EventArgs e)
{
CreateControls();
UpdatePage();
}
protected void CreateControls()
{
Button button1 = new Button();
button1.ID = "_Button1";
button1.Text = "Button1";
button1.Click += new System.EventHandler(_ClickEvent);
_Panel.Controls.Add(button1);
Button button2 = new Button();
button2.ID = "_Button2";
button2.Text = "Button2";
button2.Click += new System.EventHandler(_ClickEvent);
_Panel.Controls.Add(button2);
}
protected void UpdatePage()
{
Button button1 = ((Button)_Panel.FindControl("_Button1"));
button1.Text = "I went through UpdatePage and changed";
Button button2 = ((Button)_Panel.FindControl("_Button2"));
button1.Text = "I went through UpdatePage and changed";
}
protected void _ClickEvent(object sender, EventArgs e)
{
}
Will try it:
protected String TextButton1
{
get { return (String) ViewState["TextButton1"]; }
set { ViewState["TextButton1"] = value; }
}
protected String TextButton2
{
get { return (String)ViewState["TextButton2"]; }
set { ViewState["TextButton2"] = value; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CreateControls();
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
UpdatePage();
}
}
protected void CreateControls()
{
Button button1 = new Button();
button1.ID = "_Button1";
button1.Text = String.IsNullOrEmpty(TextButton1) ? "The First Value" : TextButton1;
button1.Click += new System.EventHandler(_ClickEvent1);
_Panel.Controls.Add(button1);
Button button2 = new Button();
button2.ID = "_Button2";
button2.Text = String.IsNullOrEmpty(TextButton2) ? "The First Value" : TextButton2;
button2.Click += new System.EventHandler(_ClickEvent2);
_Panel.Controls.Add(button2);
}
protected void UpdatePage()
{
Button button1 = ((Button)_Panel.FindControl("_Button1"));
button1.Text = String.IsNullOrEmpty(TextButton1) ? "The First Value" : TextButton1;
Button button2 = ((Button)_Panel.FindControl("_Button2"));
button2.Text = String.IsNullOrEmpty(TextButton2) ? "The First Value" : TextButton2;
}
protected void _ClickEvent1(object sender, EventArgs e)
{
TextButton1 = "test";
Button b = (Button) sender ;
b.Text = TextButton1;
}
protected void _ClickEvent2(object sender, EventArgs e)
{
TextButton2 = "test";
Button b = (Button)sender;
b.Text = TextButton2;
}

add a button click event dynamically in asp.net 4.5 c#

I have some questions to this post [1]: How can i create dynamic button click event on dynamic button?
The solution is not working for me, I created dynamically an Button, which is inside in an asp:table controller.
I have try to save my dynamic elements in an Session, and allocate the Session value to the object in the Page_Load, but this is not working.
Some ideas
edit:
...
Button button = new Button();
button.ID = "BtnTag";
button.Text = "Tag generieren";
button.Click += button_TagGenerieren;
tabellenZelle.Controls.Add(button);
Session["table"] = table;
}
public void button_TagGenerieren(object sender, EventArgs e)
{
TableRowCollection tabellenZeilen = qvTabelle.Rows;
for (int i = 0; i < tabellenZeilen.Count; i++)
{
...
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["table"] != null)
{
table = (Table) Session["table"];
Session["table"] = null;
}
}
}
It is not a good practice to store every control into Session state.
Only problem I found is you need to reload the controls with same Id, when the page is posted back to server. Otherwise, those controls will be null.
<asp:PlaceHolder runat="server" ID="PlaceHolder1" />
<asp:Label runat="server" ID="Label1"/>
protected void Page_Load(object sender, EventArgs e)
{
LoadControls();
}
private void LoadControls()
{
var button = new Button {ID = "BtnTag", Text = "Tag generieren"};
button.Click += button_Click;
PlaceHolder1.Controls.Add(button);
}
private void button_Click(object sender, EventArgs e)
{
Label1.Text = "BtnTag button is clicked";
}
Note: If you do not know the button's id (which is generated dynamically at run time), you want to save those ids in ViewState like this - https://stackoverflow.com/a/14449305/296861
The problem lies in the moment at which te button and it's event are created in the pagelifecycle. Try the page_init event for this.
Create Button in page load
Button btn = new Button();
btn.Text = "Dynamic";
btn.Click += new EventHandler(btnClick);
PlaceHolder1.Controls.Add(btn)
Button Click Event
protected void btnClick(object sender, EventArgs e)
{
// Coding to click event
}

Categories