I’ve tried several solutions for this problem but none of them worked.
Basically, I have a table of employees and the user have the choice of adding an employee dynamically thru an update panel. Each employee is being added as LinkButton and this button will fire ajaxToolkit:modalpopupextender window through OnClick event, and this window will show the employee details.
The problem is when I click on the employee name the popup window will show up BUT the details wont.
Here is the code in which I’m creating the buttons and putting it in the table:
LinkButton lbtn = new LinkButton();
lbtn.ID = employee_arry[i] + "_lbtn" + i;
lbtn.Text = employee_arry[i];
lbtn.Click += new EventHandler(this.employee_info);
lbtn.CausesValidation = false;
lbtn.Attributes.Add("runat", "server");
cell.Controls.Add(lbtn);
and here is the employee_info method:
//the info will be pulled from the database…
public void employee_info(object sender, EventArgs e)
{
name.Text = "employee name";
dept.Text = "employee department";
jobt.Text = "employee job title";
email.Text = "employee email";
tel.Text = "employee telephone";
ModalPopupExtender1.Show();
}
Check this answer
https://stackoverflow.com/a/11127064/1268570
This explains the behavior of dynamic controls
You need to consider:
Dynamic controls should be created in the PreInit event when you are not working with a master page, if you are, then create the controls in the Init event
Avoid setting properties that can be changed in each post in these events because when the view state is applied (in a post event) the properties will be overridden
Dynamic controls must be created every time the page is posted, avoid this if(!this.IsPostBack) this.CreatemyDynamicControls();
When you create the controls in the PreInit or Init events, their states will be automatically set in a post event, which means in the LoadComplete event your controls will contain their state back even when you create them again in each post and even when you did not explicitly set their state. Note this behavior is different when you are dealing with controls created at design time, in that case, the event where the state has been set is the Load event
Event subscription should occur before the PageLoadComplete or they will not be raised
Edit 1
In case you have not found a solution, this is a way to do it (full working example):
ASPX
<asp:ScriptManager runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ViewStateMode="Enabled">
<ContentTemplate>
<asp:Panel runat="server" ID="myPanel">
</asp:Panel><br />
<asp:Button ID="Button1" Text="add control" runat="server" OnClick="addControl_Click" /><br />
<asp:Label ID="lblMessage" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Code Behind
protected int NumberOfControls
{
get
{
if (ViewState["c"] == null)
{
return 0;
}
return int.Parse(ViewState["c"].ToString());
}
set
{
ViewState["c"] = value;
}
}
protected void addControl_Click(object sender, EventArgs e)
{
this.NumberOfControls++;
this.myPanel.Controls.Add(new Literal { Text = "<br />" });
this.myPanel.Controls.Add(this.CreateLinkButton(this.NumberOfControls));
}
protected void Page_PreLoad(object sender, EventArgs e)
{
this.CreateDynamicLinkButtons();
}
private void CreateDynamicLinkButtons()
{
for (int i = 0; i < this.NumberOfControls; i++)
{
this.myPanel.Controls.Add(new Literal { Text = "<br />" });
this.myPanel.Controls.Add(this.CreateLinkButton(i + 1));
}
}
private LinkButton CreateLinkButton(int index)
{
var l = new LinkButton { Text = "MyLink" + index.ToString(), ID = "myLinkID" + index.ToString() };
l.Click += (x, y) =>
{
this.lblMessage.Text += "<br/>ID: " + (x as LinkButton).ID;
};
return l;
}
Output
Related
I have a problem with PagedDataSource and UpdatePanel.When I use AsyncPostBackTrigger in my buttons, paging in my page disappear.
Here's asp code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DataList ID="DataListGallery" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" OnItemDataBound="DataListGallery_ItemDataBound" >
<ItemTemplate>
<asp:HiddenField ID="FieldPhoneId" Value='<%# Eval("Phone_InfoID") %>' runat="server" />
<asp:ImageButton ID="btnShop" OnClick="btnShop_Click" ImageUrl="images/cart.gif" CssClass="left_bt_item" title="header=[خريد] body=[ ] fade=[on]" runat="server" />
<asp:ImageButton CssClass="left_bt_item" title="header=[مورد علاقه] body=[ ] fade=[on]" OnClick="btnFavourite_Click" ID="btnFavourite" ImageUrl="images/unfav.png" runat="server" />
.
.
.
Here's C# code(load event):
protected void Page_Load(object sender, EventArgs e)
{
//http://www.aspdotnetfaq.com/Faq/how-to-determine-whether-an-asynchronous-partial-postback-has-occurred-on-page.aspx
// get a reference to ScriptManager and check if we have a partial postback
if (ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)
{
// partial (asynchronous) postback occured
// insert Ajax custom logic here
}
// enable property is re-creating page controls
else if (!Page.IsPostBack || enable)
{
//enable = false;
if (Page.Request["Page"] != null || Page.Request["Page"] != "")
{
.
.
.
And a button with AsyncPostBackTrigger:
protected void btnFavourite_Click(object sender, ImageClickEventArgs e)
{
// Access to real ImageButton from repeater
ImageButton ib = (ImageButton)sender;
HiddenField hf = (HiddenField)ib.Parent.FindControl("FieldPhoneId");
Favourite objFav = new Favourite(Convert.ToInt32(hf.Value));
Guid userId = objFav.GetUserIdFromUserName(User.Identity.Name);
using (var context = new MobileGalleryEntities())
{
try
{
// Delete favorited
if (objFav.HadFavorited(User.Identity.Name))
{
int phoneInfoId = Convert.ToInt32(Convert.ToInt32(hf.Value));
// Remove favourite
objFav.RemoveFromFavourite(userId);
// Change image
ib.ImageUrl = "~/images/unfav.png";
}
// Add favorite
else
{
// Add phone
objFav.AddToFavourite(userId);
// Change image
ib.ImageUrl = "~/images/favs.gif";
}
}
catch (Exception ex)
{
}
}
}
Here's DataBound event:
protected void DataListGallery_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (User.Identity.IsAuthenticated)
{
try
{
// Get LoginView for access to ImageButton on it.
var loginView = e.Item.FindControl("LoginView1");
ImageButton btnCom = (ImageButton)e.Item.FindControl("btnCompare");
//ImageButton btnFav = (ImageButton)loginView.FindControl("btnFavourite");
ImageButton btnFav = (ImageButton)e.Item.FindControl("btnFavourite");
btnFav.Visible = true;
ImageButton btnShop = (ImageButton)e.Item.FindControl("btnShop");
btnShop.Visible = true;
//HiddenField hf = (HiddenField)loginView.FindControl("FieldPhoneId");
HiddenField hf = (HiddenField)e.Item.FindControl("FieldPhoneId");
List<int> listFav = (List<int>)Session["Fav"];
if (listFav.Contains(int.Parse(hf.Value)))
btnFav.ImageUrl = "~/images/favs.gif";
}
catch(Exception ex)
{
}
}
else
{
ImageButton btnFav = (ImageButton)e.Item.FindControl("btnFavourite");
btnFav.Visible = false;
ImageButton btnShop = (ImageButton)e.Item.FindControl("btnShop");
btnShop.Visible = false;
}
}
When I fire btnFavourite button, paging in my page disapear. but when i use FullPostBack it's work.
Notice that when I use FullPostBack, I re-create controls such as paging control and other control but when I use AsyncPostBackTrigger I didn't re-create controls.
What am I doing?
Thanks.
I have to re-create controls again the same as FullPostBack.
I got a query regarding adding dynamic buttons with dynamic onclick events on a set of updatepanels.
I've simplified the scenario as the code I have so far is way too long and tied up..I've created a test page with 3 update panels.
In terms of the actual page the first updatepanel will be for the filters which will in turn update the second update panel. 2nd update panel will consist of all the results, depending on filters..this will be a table of buttons.
On the click of any of these buttons on the second update panel, depending on the ID of the button the results will be generated in the last update panel.
The problem I'm facing is tieing in the button click event when the buttons are created.
When I create the button from the onclick from the first update panel, it adds it to the placeholder but the click event does not fire at all.
Here is some code from my testing page.
test.aspx
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="False"
UpdateMode="Conditional">
<ContentTemplate>
<asp:PlaceHolder id="ph2" runat="server"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:PlaceHolder id="ph3" runat="server"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
Codebehind:
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Button up2button = new Button();
up2button.ID = "up2button";
ph2.Controls.Add(up2button);
up2button.Click += new EventHandler(up2button_Click); // Not being registered?
AsyncPostBackTrigger trigger1 = new AsyncPostBackTrigger();
trigger1.ControlID = "up2button";
trigger1.EventName = "Click";
UpdatePanel2.Triggers.Add(trigger1);
ScriptManager1.RegisterAsyncPostBackControl(up2button);
UpdatePanel2.Update();
}
protected void up2button_Click(object sender, EventArgs e) { //and not being fired
Button up3button = new Button();
up3button.ID = "up3button";
up3button.Click += new EventHandler(up3button_click);
ph3.Controls.Add(up3button);
AsyncPostBackTrigger trigger1 = new AsyncPostBackTrigger();
trigger1.ControlID = "up3button";
trigger1.EventName = "Click";
UpdatePanel3.Triggers.Add(trigger1);
UpdatePanel3.Update();
}
protected void up3button_click(object sender, EventArgs e) {
}
}
Thankyou for your time.
here's a quick sample, I find on the internet:
I've used a placeholder to hold the dynamically created controls - the button and textboxes on that button's click event.
and the code:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
if (ViewState["Add"] != null)
{
Button add = new Button();
add.Text = "Add";
add.Click += new EventHandler(add_Click);
PlaceHolder1.Controls.Add(add);
}
if (ViewState["textboxes"] != null)
{
int count = 0;
count = (int)ViewState["textboxes"];
for (int i = 0; i < count; i++)
{
textbox textbox_foradd = new TextBox();
textbox_foradd.ID = "textadd" + (i + 1).ToString();
PlaceHolder1.Controls.Add(textbox_foradd);
}
}
}
}
void add_Click(object sender, EventArgs e)
{
int count = 1;
if (ViewState["textboxes"] != null)
{
count += Convert.ToInt32(ViewState["textboxes"]);
}
TextBox textbox_foradd = new TextBox();
textbox_foradd.ID = "textadd" + count.ToString();
PlaceHolder1.Controls.Add(textbox_foradd);
ViewState["textboxes"] = count;
}
protected void Button1_Click(object sender, EventArgs e)
{
Button add = new Button();
add.Text = "Add";
PlaceHolder1.Controls.Add(add);
ViewState["Add"] = 1;
}
Also, you need to give unique ID to each control in case you add multiple of the same control.
Ex:
Add button with id 'ButtonA'
Add another button with id 'ButtonA'
will fail, after the postback your buttons will trigger the event properly, but the action will not do a proper refresh.
Making it looks like it is not refreshing anymore.
Instead generate a unique ID for each control you add dynamically (and save those IDs).
In the postback, recreate those controls and reassign the IDs properly.
Only then will the UI refresh properly.
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);
}
I've read a few articles on here and the web that have informed me that I cannot simply add a new control dynamically to the page, wire it to a handler, and expect it to work.
The solution given each time is that the dynamic controls need to be added to the page on Init each time.
My problem is, my controls are NOT added to the page on init, they are added after ANOTHER postback.
the workflow is this:
Page Loads
User fills in a textbox, clicks a button
Page Posts back, creating dynamic link controls in the button_click event based on the input
User clicks one of those link controls to proceed to the next step.
so if this is the behavior I need to support, is there any way to do this? It has to happen in the button_click of step 2, because the dynamic controls are based on the input the user puts in step 2.
have I painted myself into a corner here? how else could I handle such a workflow?
After you dynamically create a link button, set a flag in your page's view state. On postback, re-create the link button if the flag is set in view state. Here's a demo:
Markup:
<asp:Button runat="server" ID="button1" OnClick="button_Click" Text="Create button A" CommandArgument="A" />
<asp:Button runat="server" ID="button2" OnClick="button_Click" Text="Create button B" CommandArgument="B" />
<asp:PlaceHolder runat="server" ID="placeHolder"></asp:PlaceHolder>
Code-behind:
public partial class Default : System.Web.UI.Page
{
private bool LinkButtonCreated
{
get { return ((bool?)this.ViewState["LinkButtonCreated"]).GetValueOrDefault(); }
set { this.ViewState["LinkButtonCreated"] = value; }
}
private string LinkButtonCommandArgument
{
get { return (string)this.ViewState["LinkButtonCommandArgument"]; }
set { this.ViewState["LinkButtonCommandArgument"] = value; }
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (this.LinkButtonCreated)
this.CreateLinkButton(this.LinkButtonCommandArgument);
}
protected void button_Click(object sender, EventArgs e)
{
if (!this.LinkButtonCreated)
{
string commandArgument = ((Button)sender).CommandArgument;
this.LinkButtonCreated = true;
this.LinkButtonCommandArgument = commandArgument;
this.CreateLinkButton(commandArgument);
}
}
private void CreateLinkButton(string commandArgument)
{
LinkButton linkButton =
new LinkButton
{
ID = "linkButton",
Text = "Click me",
CommandArgument = commandArgument,
};
linkButton.Click += this.linkButton_Click;
this.placeHolder.Controls.Add(linkButton);
}
private void linkButton_Click(object sender, EventArgs e)
{
LinkButton linkButton = (LinkButton)sender;
linkButton.Text = "I was clicked! Argument: " + linkButton.CommandArgument;
}
}
I'm reading data from database and showing it in a page for editing:
<h2>Create new topic:
<asp:Label ID="_lblTopicName" runat="server" Text=""></asp:Label></h2>
<p>
Edit Level:
<br/>
<asp:DropDownList ID="_dtlEditRole" runat="server"></asp:DropDownList>
<br/>
View Level:
<br/>
<asp:DropDownList ID="_dtlViewRole" runat="server"></asp:DropDownList>
<br/>
<asp:TextBox ID="_tbxTopicText" TextMode="MultiLine" runat="server" Height="204px"
Width="885px"></asp:TextBox>
</p>
<asp:Button ID="_btnSaveTopic" runat="server" Text="Save" onclick="_btnSaveTopic_Click" />
I fill the fields in Page_PreRender() like so:
private string _topicString;
private Topic _topic = null;
private Topics_GetTopicByTopicResult _findTopicResults = null;
protected void Page_PreRender(object sender, EventArgs e)
{
// Load the User Roles into checkboxes.
_dtlEditRole.DataSource = Roles.GetAllRoles();
_dtlEditRole.DataBind();
_dtlViewRole.DataSource = Roles.GetAllRoles();
_dtlViewRole.DataBind();
_topicString = Request.QueryString["Topic"];
if (String.IsNullOrEmpty(_topicString))
{
Response.Redirect("~/Default.aspx");
}
else
{
_topic = new Topic();
_findTopicResults = _topic.FindTopic(_topicString);
if (_topic != null)
{
// Check if the user has permission to access
if (RoleHelper.IsEditAllowed(_findTopicResults.ViewRoleName))
{
_lblTopicName.Text = _findTopicResults.Topic;
_tbxTopicText.Text = _findTopicResults.Text;
_dtlEditRole.SelectedValue = _findTopicResults.EditRoleName;
_dtlViewRole.SelectedValue = _findTopicResults.ViewRoleName;
}
else
{
Response.Redirect("~/Error.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.RawUrl));
}
}
else
{
Response.Redirect("~/CreateTopic.aspx?Topic=" + _topicString);
}
}
}
But now when i click _btnSaveTopic button the fields:
private string _topicString;
private Topic _topic = null;
private Topics_GetTopicByTopicResult _findTopicResults = null;
They are all NULL and im not able to update aything.
Here's my button click event:
protected void _btnSaveTopic_Click(object sender, EventArgs e)
{
_topic.UpdateTopic(_findTopicResults.ID, _findTopicResults.Topic, _tbxTopicText.Text,
_dtlViewRole.SelectedItem.Text, _dtlEditRole.SelectedItem.Text);
Response.Redirect("~/ViewPage.aspx?Topic=" + _topicString);
}
What would be the right way doing this?
ASP.NET Page Life Cycle states that Page_Init should be used to 'initialize control properties' which looks like what you are doing.
Also, it's usually good practice to breakup such large sections of code into smaller refactored methods. Try to keep the amount of code directly placed in event handlers to a minimum.
You can start by right-clicking a section of highlighted code in visual studio -> refactor -> extract method
Also, if you need more help understanding how to improve your code, you should ask a question pointing to this question on the code review site: here
You are re-binding the drop down list (and therefore wiping out the 'SelectedValue') in your Page_PreRender method. Wrap the method in
protected void Page_PreRender(object sender, EventArgs e)
{
if( !IsPostBack){
//your current code
}
}
and it should work.