A strange problem.
On my page i have a placeholder where I create a repeater on run time.
In ItemTemplate i have a button that invokes repeater_ItemCommand event.
I put a breakpoint on first line of repeater_ItemCommand event, when the button is triggered the event is invoked as planned, but after RebindRepeaters() is finished, when i click on the button again repeater_ItemCommand event is not invoked!
Only if i click on the button one again it invokes, in other scenarios(this is simple version of my page, in original page i have updatepanel that complex everything) it never invokes!
My aspx code:
<form id="form1" runat="server">
<div id="talk">
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
My Code-behind:
protected void Page_Init(object sender, EventArgs e)
{
RebindRepeaters();
}
void RebindRepeaters()
{
PlaceHolder1.Controls.Clear();
CreateRepeater(PlaceHolder1);
}
void CreateRepeater(Control container)
{
Repeater repeater1 = new Repeater();
repeater1.ItemCommand += new RepeaterCommandEventHandler(repeater_ItemCommand);
repeater1.ItemTemplate = Page.LoadTemplate("CommentsTemplate.ascx");
container.Controls.Add(repeater1);
repeater1.DataSource = Comment.GetCommentsP(8, 0);
repeater1.DataBind();
}
void repeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "Edit":
RebindRepeaters();
break;
}
}
The CommentsTemplate.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="CommentsTemplate.ascx.cs" Inherits="CommentsTemplate" %>
<ul>
<li>
<div class="show">
<div class="c">
<asp:LinkButton ID="lbtnTitle" runat="server" Text='<%#Eval("EncodedTitle")%>'
CausesValidation="false" CommandName="VoteUp" OnClientClick="viewHide(this);return false;" CommandArgument='<%#Eval("ID")%>'></asp:LinkButton>
<p class="d"><%#Eval("AddedBy")%>,<%#Eval("AddedDate")%></p>
<div class="CommentBody"><%# Eval("EncodedBody") %>
</div>
</div>
<div class="userpanel">
<asp:Panel runat="server" ID="panAdmin" Visible='<%# UserCanEdit %>' style="float:left;">
<asp:ImageButton runat="server" ID="btnSelect" CommandName="Edit" CommandArgument='<%# Eval("ID") %>'
CausesValidation="false" AlternateText="Edit comment" ImageUrl="~/Images/Edit.gif" />
<asp:ImageButton runat="server" ID="btnDelete" CommandName="Delete" CommandArgument='<%# Eval("ID") %>'
CausesValidation="false" AlternateText="Delete comment" ImageUrl="~/Images/Delete.gif"
OnClientClick="if (confirm('Are you sure you want to delete this comment?') == false) return false;" />
</asp:Panel>
</div>
</div>
</li>
</ul>
If you need more info just ask.
Could you try overidding createChildcontrols and add the RebindRepeaters call there instead of in Page_Init? And make sure to give your repeater an explicit ID:
Repeater repeater1 = new Repeater();
repeater1.ID = "repeater1";
Without an id the asp.net event handling won't know where the event was called from.
Dynamic controls have to be re-added on every postback in order for their events to fire. A new control tree is being created on the postback and the dynamic control is no longer in it, therefore ASP.NET can't evaluate the change in order to fire the event.
EDIT: I was able to fire the event each click by removing RebindRepeaters(). You were binding it twice, once here and once in (Page_Init):
void repeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "Edit":
//RebindRepeaters();
break;
}
}
Related
I have a slideshow that images load from sql server.
here is my html code:
<!-- Elastislide Carousel -->
<ul id="Ul1" class="elastislide-list">
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<li>
<asp:Label ID="lbl" runat="server" Text='<%#Eval("IDMusic") %>' Visible="True"></asp:Label>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%#Eval("ImageUrl") %>' Width="250px" Height="250px" OnClick="ImageButton1_Click" />
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
<!-- End Elastislide Carousel -->
<br /><br />
</div>
</div>
Now when, I run Project and user click on one of the imageButtons, I want to understand what is he/she clicked? for example he/she click on second ImageButton, Then I want to save IDMusic into a session and use this session in another page but I don't know how can i get IDmusic user clicked on?
here is my code behind :
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
foreach (RepeaterItem item in Repeater2.Items)
{
Label lbl = (Label)item.FindControl("lbl");
if (lbl != null)
{
Session["selectedmusicID"] = lbl.Text;
}
}
Response.Redirect("music.aspx");
}
There is no easy way to do so while handling click on the image itself. However you can leverage ItemCommand event here, which is a repeater event. Basically when some control is clicked inside the repeater, it generates a command in repeater which you can handle, and you can have item-specific arguments for it.
To define a command and args:
<asp:ImageButton ID="ImageButton1" ... CommandName="Select" CommandArgument='<%#Eval("IDMusic") %>' />
Command name here is custom, choose whatever you like. Now subscribe to the event of the repeater:
<asp:Repeater ID="Repeater2" runat="server"
OnItemCommand="Repeater2_ItemCommand">
And to handle:
protected void Repeater2_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Select") //just in case you have more than one
{
e.CommandArgument //here is your ID
}
}
I have create the user control and add the same in aspx code behind. I want to handle the selected index change event for dropdownlist in aspx page. Kindly help me to do this.
<div class="row">
<div class="col-lg-2">
<asp:Label ID="lbl_parent" runat="server" Text="Parent Node" CssClass="control-label"></asp:Label>
</div>
<div class="col-lg-6">
<asp:DropDownList ID="ddl_parent" runat="server" CssClass="form-control" AutoPostBack="true">
</asp:DropDownList>
</div>
</div>
phContactDetails.Controls.Add(LoadControl("~/UserControl/WUC_DisLabelControl.ascx"));
foreach (var control in phContactDetails.Controls)
{
var usercontrol = control as WUC_DisLabelControl;
DropDownList ddl_parenrt = ((DropDownList)usercontrol.FindControl("ddl_parent"));
ddl_parenrt.SelectedIndexChanged += new EventHandler(ddlparent_SelectedIndexChanged);
}
protected void ddlparent_SelectedIndexChanged(object sender, EventArgs e)
{
throw new NotImplementedException(); //Not Firing
}
You have not added SelectedIndexChanged event on dropdown.
Add SelectedIndexChanged="ddlparent_SelectedIndexChanged" on dropdown.
<asp:DropDownList ID="ddl_parent" runat="server" CssClass="form-control" AutoPostBack="true" OnSelectedIndexChanged="ddlparent_SelectedIndexChanged">
</asp:DropDownList>
I want load two user controls on demand.
asp:UpdatePanel ID="UpdatePanel1" runat="server"
ContentTemplate
asp:Button ID="Button1" runat="server" Text="Button" UseSubmitBehavior="false"
OnClick="Button1_Click" /
div id='Div_UserControlPlace' enableviewstate="true" runat="server"
/div
/ContentTemplate
Triggers
asp:PostBackTrigger ControlID="Button1" /
/Triggers
/asp:UpdatePanel
asp:UpdatePanel ID="UpdatePanel2" runat="server"
ContentTemplate
asp:Button ID="Button2" runat="server" Text="Button" UseSubmitBehavior="false"
OnClick="Button2_Click" /
div id='Div_UserControlPlace2' enableviewstate="true" runat="server"
/div
/ContentTemplate
aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Control FeaturedProductUserControl = new Control();
FeaturedProductUserControl = LoadControl("WebUserControl1.ascx");
FeaturedProductUserControl.EnableViewState = true;
Div_UserControlPlace.Controls.Add(FeaturedProductUserControl);
}
protected void Button2_Click(object sender, EventArgs e)
{
Control FeaturedProductUserControl2 = new Control();
FeaturedProductUserControl2 = LoadControl("WebUserControl2.ascx");
FeaturedProductUserControl2.EnableViewState = true;
Div_UserControlPlace2.Controls.Add(FeaturedProductUserControl2);
}
I load the first user control by clicking on the first button - this works properly but when I click on the other button to load the second UserControl, the first UserControl disappears and the second UserControl loads.
Thanks
IFA_User
You should use the Placeholder control to dynamically add your controls to the form.
Take a look at my last responses about dynamic controls:
OnClick event of dynamically created LinkButtons is not working
Dynamically Added DropDownlists Are Not Firing SelectedIndexChanged Event
Dynamically create an ImageButton
Now I already have some code working for demo purpose, each dynamic user controls keeps its state across post backs
This is the output:
ASPX
<asp:PlaceHolder runat="server" ID="addresses" /><br />
<asp:Button Text="Add Address" runat="server" ID="addAddress" OnClick="addAddress_Click" />
ASPX Code behind
protected void Page_PreLoad(object sender, EventArgs e)
{
for (int i = 0; i < this.DynamicControlsCount; i++)
{
var c = this.LoadControl("~/AddressControl.ascx");
this.addresses.Controls.Add(c);
}
}
protected void addAddress_Click(object sender, EventArgs e)
{
this.DynamicControlsCount++;
var c = this.LoadControl("~/AddressControl.ascx");
this.addresses.Controls.Add(c);
}
protected int DynamicControlsCount
{
get
{
if (this.ViewState["ac"] == null)
{
return 0;
}
return (int)this.ViewState["ac"];
}
set
{
this.ViewState["ac"] = value;
}
}
ASCX
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="AddressControl.ascx.cs" Inherits="WebApplication1.AddressControl" %>
<asp:Panel ID="Panel1" runat="server" GroupingText="Address" DefaultButton="btnSave">
Street: <asp:TextBox runat="server" ID="txtStreet" /><br />
City: <asp:TextBox runat="server" ID="txtCity" /><br />
<asp:Button Text="Save" runat="server" ID="btnSave" OnClick="btnSave_Click" />
</asp:Panel>
<asp:Panel runat="server" GroupingText="Address Summary" Visible="false" ID="summary">
<asp:Label ID="lblStreet" runat="server" /><br />
<asp:Label ID="lblCity" runat="server" />
</asp:Panel>
ASCX Code behind
protected void btnSave_Click(object sender, EventArgs e)
{
this.summary.Visible = true;
this.lblCity.Text = "Selected city: " + this.txtCity.Text;
this.lblStreet.Text = "Selected street: " + this.txtStreet.Text;
}
When a user control is created in the HTML, asp.net will persist across postbacks without any user interaction. But if you are loading them programatically (dynamically), they will not persist accross postbacks. So if you load them programmatically, you have the added task of persisting them programmatically as well. Use the ViewState (or Session I suppose) to store what has been loaded and perhaps any other necessary information that needs to be loaded between postbacks. Every single postback will require you to reload every control or else they will disappear.
There are couple of ways of doing it:
U can load the UserControls using Ajax. Benefit of using Ajax, is ur page does not get post back, thus for example, on click event of Button1, call a ajax(traditional/Jquery) to load UserControl1, and on button click of Button2 User control2.
Put the two button in two different updated panel, by doing this the click event will only refresh a part of ur page.
U have to save somewhere (ViewState/Session),which buttons are clicked, and upon clicking of any button check the value of that variable, and explicit load the control.
Points to note - If u want to get ur data back when ur page made a complete postback, then u have to add the controls keeping in mind the Page load event cycle.
I have an ASP.Net UpdatePanel that updates on a timer. Within the UpdatePanel and nested in a GridView, I have a TextBox that the user enters a value in with a submit button. Everything works fine, except if the user does not submit the value before the timed interval, the text is removed from the TextBox.
Is there a way to persist the user entry into the TextBox between updates? Is there a better way of doing this?
All suggestions welcome.
Respectfully,
Ray K. Ragan
Code Postscript:
aspx:
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(beginRequest);
function beginRequest() {
prm._scrollPosition = null;
}
</script>
<asp:Timer ID="Timer1" runat="server" Interval="900" OnTick="Timer1_Tick"></asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:DataList RepeatColumns="5" RepeatDirection="Horizontal" ID="dlMine" runat="server" OnItemCommand="Item_Command">
<ItemTemplate>
<div style="border:1px solid black;margin:3px;height:300px;text-align:center;padding:5px;">
<div style="width:150px;">
<asp:Label ID="lblTitle" runat="server" Text='<%# left(DataBinder.Eval(Container.DataItem,"title").ToString(), 75) %>'></asp:Label>
</div>
<asp:Label ID="Label1" runat="server" Text='<%# (DateTime)DataBinder.Eval(Container.DataItem,"end_date") %>'></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text='<%# String.Format("{0:C}",DataBinder.Eval(Container.DataItem,"current_value")) %>'></asp:Label>
<br />
<asp:TextBox ID="txtNewValue" runat="server"></asp:TextBox>
<asp:Button Visible='<%# (isInThePast((DateTime)DataBinder.Eval(Container.DataItem,"end_date"))) ? false : true %>' CssClass="bid_button" runat="server" CommandArgument='<%# Eval("ID") %>' CommandName="Revalue" ID="btnBid" Text="Submit New Valuation" />
</div>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
Codebehind:
protected void Page_Load(object sender, EventArgs e)
{
Timer1.Tick += new EventHandler<EventArgs>(Timer1_Tick);
if (!IsPostBack)
{
dataBindList();
}
}
protected void dataBindList()
{
if (Request.QueryString["GroupID"] != null)//Are they coming here with a URL var? If so, build content object
{
List<Item> itemList = ItemManager.getItemsByGroupID(Request.QueryString["GroupID"].ToString());
dlMine.DataSource = itemList.Take(15);
dlMine.DataBind();
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
dataBindList();
UpdatePanel1.Update();
}
protected void Item_Command(Object sender, DataListCommandEventArgs e)
{
if (e.CommandName == "Revalue")
{
Person p = (Person)Session["Person"];
foreach (DataListItem item in dlMine.Items)
{
string textBoxText = ((TextBox)item.FindControl("txtNewValue")).Text;
Utilities.writeLog("txtNewValue: " + textBoxText, 1);
}
dataBindList();
UpdatePanel1.Update();
}
}
You're rebinding the DataList every time the Timer ticks. All changes in the ItemTemplates of the DataList will be lost on postback.
Why not use Javascript to "pause" the timer whenver one of the textboxes gains focus. This will prevent the Timer from firing and let users finish entering text. Once they leave the textbox of "onblur" then you can restart the timer.
Update
The following will take a bit of effort to make it happen but you can do something like:
When the Timer posts back, before rebinding, iterate over the DataList while searching for textboxes with text in them. Something like:
foreach (DataListItem item in dlMine.Items)
{
//find the textbox control and check for text
}
At this point, you'll know which rows need there textboxes repopulated. Store this information in a hashtable.
In the DataList ItemDataBound event, check each rowID against the hashtable to see if its corresponding textbox exists. If so, repopulate the textbox in the DataList row.
Are you initializing the TextBbox value in your code-behind, perhaps in Page_Load or another page method?
TextBox1.Text = "";
If so, that code is executing on every timer event. You can prevent that like this:
if (! IsPostback) {
TextBox1.Text = "";
}
The first request that hits an ASP.NET page is usually an HTTP GET request, while ASP.NET buttons and update panel events issue HTTP POST requests. So the code above will clear TextBox1 the first time you access the page, but leave the value alone when receiving requests from itself. (If you really are setting the text box's value to its default - empty - you could just remove the initialization code.)
I have a rather annoying issue here
I can't get my CheckBox CheckedChange event to fire, or catch or whatever it is that fails:
ASPX Code
<asp:UpdatePanel runat="server" ID="udp_Lists" UpdateMode="Always">
<ContentTemplate>
<asp:Repeater ID="rep_showings" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<div class="div_assignment">
<div class="div_assignment_text">
<asp:LinkButton runat="server" ID="lnk_show_task" OnClick="lnk_show_task_Click" CommandArgument='<%# Eval("Id") %>' Text='<%# Eval("TaskTitle") %>'></asp:LinkButton>
</div>
<div class="div_assignment_checkbox">
<asp:CheckBox runat="server" ID="chk_handle" AutoPostBack="true" OnCheckedChanged="chk_handle_Changed" ToolTip='<%# Eval("Id") %>' />
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
</ContentTemplate>
<Triggers>
</Triggers>
The Code behind function "chk_handle_Changed" is never reached.
The Linkbutten works perfectly.
I took a look at your problem. I used the following code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.rep_showings.DataSource = new object[] { new { Title = "title", ID = "id" } };
this.rep_showings.DataBind();
}
}
protected void chk_handle_Changed(object source, EventArgs e)
{
Trace.Write("here");
}
protected void lnk_show_task_Click(object source, EventArgs e)
{
Trace.Write("here 2");
}
protected void rep_showings_ItemCommand(object source, RepeaterCommandEventArgs e)
{ }
The above code works. I think you are probably re-binding your repeater on every postback - I tested this by removing the "if (!IsPostBack)" statement in Page_Load(), and I was able to reproduce the problematic behaviour you describe.
Rebinding a control on every postback should be avoided if possible. Once a control is populated, it's data is taken care of by ViewState, so unless the data is changing, you should probably not be rebinding it all the time.