C# - Event handler for dynamic buttons - asp.net

I have a textbox and submit button created using the design mode.
When the submit button is pressed, it will retrieve the user input from the textbox and then make a query to my database.
It will then display a list of dynamic buttons according to the information retrieved from my database.
However, the event handler for the buttons does not fire when clicked. I guess my problem is the postback but I cannot create those buttons in page_load etc. because I need to get the user input (from the textbox when submit button is pressed) before i can load the buttons.
How can i solve this problem?
Thank you.
Edit (codes):
protected void subBtn_Click(object sender, EventArgs e)
{
//database setup codes
.......
while (reader.Read())
{
Button detailsBtn = new Button();
detailsBtn.Text = reader["fName"].ToString();
//doesn't fire
detailsBtn.Click += new EventHandler(detailsBtn_Click);
memPanel.Controls.Add(detailsBtn);
}
}

Main problem is Postback regenerate dynamic controls on each postback if those controls does not exists.
For quick demo see this code
ASPX CODE
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Panel ID="pnl" runat="server"></asp:Panel>
</form>
ASPX.CS CODE
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
generate();
}
}
public void generate()
{
if (!pnl.HasControls())
{
for (int i = 0; i < 4; i++)
{
Button detailsBtn = new Button();
detailsBtn.Text = "fName" + i.ToString();
detailsBtn.ID = i.ToString();
detailsBtn.Click += new EventHandler(detailsBtn_Click);
pnl.Controls.Add(detailsBtn);
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
generate();
}
protected void detailsBtn_Click(object sender, EventArgs e)
{
}

Sound to me like you could easily refactor your page to use a simple <asp:Repeater runat="server" ..></asp:Repeater> instead of dynamically adding controls to a Panel.
Here is a very simple complete sample:
RepeaterTest.aspx
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<asp:TextBox ID="theTextBox" runat="server"></asp:TextBox>
<asp:Button ID="theButton" runat="server" OnClick="theButton_Click" Text="Click me" />
<asp:Repeater ID="test" runat="server">
<ItemTemplate>
<asp:Button ID="theRepeaterButton" runat="server" Text='<%# Eval("fName") %>' OnClick="theRepeaterButton_Click" />
</ItemTemplate>
</asp:Repeater>
</asp:Content>
RepeaterTest.aspx.cs
public partial class RepeaterTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void theButton_Click(object sender, EventArgs e)
{
string filter = theTextBox.Text;
// below row simulates fetching data using the filter text in the text box
var data = Enumerable.Range(0, 20).Select(i => new { fName = filter + " " + i });
test.DataSource = data;
test.DataBind();
}
protected void theRepeaterButton_Click(object sender, EventArgs e)
{
var button = (Button)sender;
// do something here based on text/commandname/commandargument etc of the button
}
}

Related

Disable postback of a dynamically created button asp.net

This is my Page1.aspx code:
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:Panel ID="Panel1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</asp:Panel>
</asp:Content>
This is my Page1.aspx.cs code
public partial class Page1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == true)
{
btn_Click(sender, e);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn.Text = "Dynamic button";
btn.Click += btn_Click;
Panel1.Controls.Add(btn);
}
void btn_Click(object sender, EventArgs e)
{
Response.Redirect("../Page2");
}
}
From this code, when "Button1" is clicked it should create a new button "btn" and when "btn" is clicked, it should go to the url in its click function (i.e. btn_Click()). But when I click "Button1" it does not execute "Button1_Click()" but "btn_Click()". What should I do??
if (IsPostBack == true)
can become
if (IsPostBack)
Then
Response.Redirect("../Page2");
can become
Response.Redirect("~/Page2");
In the aspx check you have:
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click"></asp:Button>
Moreover you can bind click in code behind (eg OnLoad):
Button1.Click += Button1_Click;
and also unbind:
Button1.Click -= Button1_Click;

Why does my button control not display in the code behind?

I have the following code in my aspx page:
<asp:Literal ID="ltPost" runat="server"></asp:Literal>
and this in my code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
BindData();
}
}
protected void BindData() {
str.Append(#"<asp:Button ID=*btnEditReply* runat=*server* CssClass=*button-action m-r-5* Text=*Sửa phản hồi* ValidationGroup=*AddSave*/>");
ltPost.Text = str.ToString().TrimEnd(',').Replace('*', '"');
}
I don't understand this; Why won't the button btnEditReply display?
To add a server control, you have to use Controls.Add(). Also, you can't add a button to a literal, so it's better to use a Placeholder.
C#:
Button editButton = new Button();
editButton.ID = "btnEditReply";
editbutton.Text = "Sửa phản hồi";
// .. etc
placeHolder.Controls.Add(editButton);
HTML:
<asp:Placeholder id="placeHolder" runat="server" />
Reference: http://msdn.microsoft.com/en-us/library/kyt0fzt1(v=vs.100).aspx

Radio button doesn't get selected after a post back

I have an item template within repeater:
<ItemTemplate>
<li>
<input type="radio"
value="<%# GetAssetId((Guid) (Container.DataItem)) %>"
name="AssetId"
<%# SelectAsset((Guid) Container.DataItem) %> />
</li>
</ItemTemplate>
I have a method that compares ids and decides whether to check the radio button.
protected string SelectAsset(Guid uniqueId)
{
if (uniqueId == GetSomeId())
return "checked=\"checked\"";
return string.Empty;
}
SelectAsset gets hit, but it doesn't select a radio button on a post back, but it does work if I just refresh the page. What am I doing wrong here?
Answer here: How to display "selected radio button" after refresh? says that it's not possible to achieve, is this really the case?
Thank you
Update
It appears that view state isn't available for simple controls if they don't have a runat attribute. I have solved this by using a custom GroupRadioButton control. Thank you for your help.
I'd suggest using a RadioButtonList:
Page Code
<asp:RadioButtonList RepeatLayout="UnorderedList" OnSelectedIndexChanged="IndexChanged" AutoPostBack="true" ID="RadioRepeater" runat="server" />
<asp:Label ID="SelectedRadioLabel" runat="server" />
Code Behind
if (!Page.IsPostBack)
{
/* example adds items manually
- you could iterate your datasource here as well */
this.RadioRepeater.Items.Add(new ListItem("Foo"));
this.RadioRepeater.Items.Add(new ListItem("Bar"));
this.RadioRepeater.Items.Add(new ListItem("Baz"));
this.RadioRepeater.SelectedIndex = this.RadioRepeater.Items.IndexOf(new ListItem("Bar"));
this.RadioRepeater.DataBind();
}
protected void IndexChanged(object sender, EventArgs e)
{
this.SelectedRadioLabel.Text = string.Format("Selected Item Text: {0}", this.RadioRepeater.SelectedItem.Text);
}
I assume you only need to select one item.
As described in the comments, it even works to access the SelectedItem in the Page_Loadevent handler:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// previous code omitted
}
else
{
string foo = this.RadioRepeater.SelectedItem.Text;
}
}
If you are creating all your controls dynamically at run-time (directly from code), then things are a little different. Here is the code that I used:
Page Code
<form id="form1" runat="server">
</form>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
RadioButtonList rbl = new RadioButtonList();
rbl.AutoPostBack = true;
rbl.SelectedIndexChanged += rbl_SelectedIndexChanged;
rbl.Items.Add("All");
// generate your dynamic radio buttons here
for (int i = 0; i<5; i++)
{
rbl.Items.Add(string.Format("Dynamic{0}", i));
}
form1.Controls.Add(rbl);
if (!Page.IsPostBack)
{
rbl.SelectedValue = "All";
PopulateTextBox(rbl.SelectedValue);
}
}
void rbl_SelectedIndexChanged(object sender, EventArgs e)
{
RadioButtonList foo = (RadioButtonList)sender;
PopulateTextBox(foo.SelectedValue);
}
void PopulateTextBox(string selection)
{
TextBox box = new TextBox();
box.Text = selection;
form1.Controls.Add(box);
}

Information disappears on button click

I have a ListView that has a FileUpload control and a button in each ListViewItem.
I have an OnClick event on my button where i try and pull information from the FileUpload control, but when I try to access the control all of the values that were set are gone (FileName etc).
What do I need to do differently here to access the information I just entered?
<asp:ListView ID="lv_Uploads" runat="server" OnItemDataBound="GetThumbs" EnableViewState="true" >
<LayoutTemplate>
<div id="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<div style="width:500px;>
<asp:FileUpload runat="server" ID="fu_Upload" />
<asp:Button ID="btn_Save" runat="server" Text="Save File" OnClick="SaveFile" />
<br />
</div>
</ItemTemplate>
</asp:ListView>
Code behind:
protected void SaveFile(object sender, EventArgs e)
{
//This always evaluates to an empty string...
string myFile = ((FileUpload)((Button)sender).Parent.FindControl("fu_Upload")).FileName;
}
I tested the code you provided for the aspx and the following as the code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
lv_Uploads.DataSource = data;
lv_Uploads.DataBind();
}
}
protected void SaveFile(object sender, EventArgs e)
{
//This always evaluates to an empty string...
string myFile = ((FileUpload)((Button)sender).Parent.FindControl("fu_Upload")).FileName;
}
protected void GetThumbs(object sender, ListViewItemEventArgs e)
{
}
protected IEnumerable<string> data = new string[] { "test1", "test2", "test3" };
The FileUpload control had data for me on PostBack.
Are you using an UpdatePanel around the ListView? FileUpload controls are not compatible with UpdatePanels.
See:
FileUpload control inside an UpdatePanel without refreshing the whole page?
and
http://msdn.microsoft.com/en-us/library/bb386454.aspx#UpdatePanelCompatibleControls
Is the ListView control being rebound before SaveFile is fired on PostBack? If so, it would wipe out any values the user entered.

Setting viewstate on postback

I am trying to set a ViewState-variable when a button is pressed, but it only works the second time I click the button. Here is the code-behind:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
lblInfo.InnerText = String.Format("Hello {0} at {1}!", YourName, DateTime.Now.ToLongTimeString());
}
}
private string YourName
{
get { return (string)ViewState["YourName"]; }
set { ViewState["YourName"] = value; }
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
YourName = txtName.Text;
}
Is there something I am missing? Here is the form-part of the design-file, very basic just as a POC:
<form id="form1" runat="server">
<div>
Enter your name: <asp:TextBox runat="server" ID="txtName"></asp:TextBox>
<asp:Button runat="server" ID="btnSubmit" Text="OK" onclick="btnSubmit_Click" />
<hr />
<label id="lblInfo" runat="server"></label>
</div>
</form>
PS: The sample is very simplified, "use txtName.Text instead of ViewState" is not the correct answer, I need the info to be in ViewState.
Page_Load fires before btnSubmit_Click.
If you want to do something after your postback events have fired use Page_PreRender.
//this will work because YourName has now been set by the click event
protected void Page_PreRender(object sender, EventArgs e)
{
if (Page.IsPostBack)
lblInfo.InnerText = String.Format("Hello {0} at {1}!", YourName, DateTime.Now.ToLongTimeString());
}
The basic order goes:
Page init fires (init cannot access ViewState)
ViewState is read
Page load fires
Any events fire
PreRender fires
Page renders

Resources