Buttons on ASP Repeater as trigger for Update Panel? - asp.net

I am trying to work out with the idea of assigning a trigger for Update Panel Dynamically.
<asp:Repeater ID='justAnID' runat='server'>
<ItemTemplate>
<asp:Button ID='justAnotherID' runat='server'/>
<ItemTemplate>
</asp:repeater>
<asp:UpdatePanel runat='server'>
<ContentTemplate>
//mycontents
<ContentTemplate>
<triggers>
//??
<triggers>
</asp:repeater>
The point is - I can't come up any idea how to assign as trigger the buttons created by my repeater. Any idea for this concept?

You can assign an async trigger in code behind by looping the Repeater items. This has to be done on every postback.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
foreach (RepeaterItem item in justAnID.Items)
{
Button button = item.FindControl("justAnotherID") as Button;
ScriptManager.GetCurrent(Page).RegisterPostBackControl(button);
}
}

Related

How to make UpdatePanel to do Update after custom Event?

I'm setting UpdatePanel1.UpdateMode = UpdatePanelUpdateMode.Conditional; to make manual updates but it doesn't work for some custom events, when I have some event alike here:
protected void Button1_Click(object sender, EventArgs e) {
discovery.FindAlreadyRegisteredServices();
discovery.discoveryClient.FindCompleted += FoundEvent;
protected void FoundEvent(object sender, FindCompletedEventArgs e) {
Label1.Text = (discovery.endpoints.Count > 0) ? discovery.endpoints[0].Address.ToString() : "nothing";
UpdatePanel1.Update();
}
My project is failing with:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.Internals.dll
Additional information: The Update method can only be called on UpdatePanel with ID 'UpdatePanel1' before Render.
even if I set ChildrenAsTriggers or not. Error message is not clear for me and I can't understand what should I do to process update right after I process my event?
addition:
aspx:
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:ListView ID="ListView1" runat="server">
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
I suppose you should change your markup like this
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
....
You should set UpdateMode="Conditional" in your markup itself
protected void Button1_Click(object sender, EventArgs e) {
discovery.FindAlreadyRegisteredServices();
discovery.discoveryClient.FindCompleted += FoundEvent;
// Try to call the update method after calling the custom even but in the click event of the button. Ensure you update the Trigger accordingly in the update panel
**UpdatePanel1.Update();**
}
protected void FoundEvent(object sender, FindCompletedEventArgs e) {
Label1.Text = (discovery.endpoints.Count > 0) ? discovery.endpoints[0].Address.ToString() : "nothing";
}
Try to add AsyncPostBackTrigger to the update panel with update mode as conditional
Although you are doing same thing explicitly.
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
Just to check if there is any other issue, you can set the updateMode property of the update panel to "Always"

ASP.Net AJAX UpdatePanel not working with Repeater and RadioButtons

I have a repeater which includes a radio button in each item, and the whole thing sites inside an update panel. When I select a radio button the whole page reloads. Why is it not just updating the update panel. I've reduced this to a pretty simple example to avoid clutter. Code here...
ASPX...
<asp:ScriptManager ID="SM1" runat="server" />
<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater runat="server" ID="history">
<ItemTemplate>
<asp:RadioButton runat="server" ID="radioButton" AutoPostBack="true" GroupName="HistoryGroup" OnCheckedChanged="RadioButton_CheckChanged" /><br />
</ItemTemplate>
</asp:Repeater>
<p><asp:Literal runat="server" ID="output" /></p>
</ContentTemplate>
</asp:UpdatePanel>
Code...
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<int> list = new List<int>();
for (int i = 0; i < 20; i++)
list.Add(i);
history.DataSource = list.ToArray();
history.DataBind();
}
}
protected void RadioButton_CheckChanged(Object sender, EventArgs e)
{
output.Text = DateTime.Now.ToString();
}
}
Setting ClientIDMode=Auto on the RadioButton should fix it (it's an infamous .NET bug, http://connect.microsoft.com/VisualStudio/feedback/details/584991/clientidmode-static-in-updatepanel-fails-to-do-async-postback)
please add up1.Update() after output.Text = DateTime.Now.ToString(). Your RadioButton is not the trigger for updatepanel
Turns out the solution was to remove the GroupName from the RadioButton. When I remove this tag it fires asynchronously and just updates the panel. I don't actually need this tag anyway (due to the known bug where GroupName doesn't work on RadioButtons in Repeaters) as I handle the grouping within my click event (i.e. uncheck any other RadioButtons of the same name in other repeater items).

radiobutton checked status on asp:linkbutton postback

I have four radiobuttons inside a repeater control which by itself is inside an update panel
// code is something like this
`<asp:update panel ..>
...
<asp:Repeater>
..
<asp:checkbox>
..
..
</asp:update panel ..>
<asp:LinkButton ID="next2" runat="server" CssClass="button_Submit" Font-Bold="true" OnClick="next_ServerClick" Text="Submit">
<asp:ImageButton ID="next" ImageUrl="~/images/newSummary.jpg" runat="server" OnClick="next_ServerClick" ImageAlign="Middle"/>
protected void next_ServerClick(object sender, EventArgs e)
{
foreach (System.Web.UI.WebControls.RepeaterItem Item in repeatercontrol.Items)
{
chkbox = ((CheckBox)Item.FindControl(chkboxName));
if (chkbox.checked)
{
...
}
}
}`
I select one of the checkboxes and when i click image button, am able to get the correct status (checked =true) .
But when i use link button, it is always coming as checked =false as if the selection did not register.
Any ideas on why this is happening?
You need to register the linkbutton click event as a trigger in the update panel. See example below:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
...
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="LinkButtonID" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Without seeing the code, the only reason the controls would be incorrect is if you are checking them in the Page Life Cycle before the ViewState is loaded.
EDIT:
Use separate event declarations and centralize the lookup logic:
protected void LinkButton1_Click(object sender, EventArgs e)
{
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
}

UpdatePanel wrapped around a user control

I have a user control which contains some buttons and a placeholder. Those buttons cause controls to be added/removed from placeholder. Everything works fine.
Now I want to put this user control in a page, and wrap it in an updatepanel like so:
<asp:UpdatePanel ChildrenAsTriggers="true" ID="UpdatePanelFoo" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<grid:tablegrid ID="tablegrid_chapters" runat="server" SomeProperty="bar" />
</ContentTemplate>
</asp:UpdatePanel>
When I run the page, it's still doing a full postback when I hit one of the buttons inside the user control. What am I doing wrong, and how can I remedy this?
Update:
protected void Page_Init()
{
ScriptManager scr = ScriptManager.GetCurrent(this.Page);
Response.Write("EnablePartialRendering: " + scr.EnablePartialRendering);
}
Outputs "EnablePartialRendering: true"
Make sure you have EnablePartialRendering=true on your ScriptManager in the page.
Update
It looks like your UserControl has no events to be looking for...you have 2 options here. Move the UpdatePanel inside the UserControl .ascx so it can see the button events as children to rig up or add an event for it to see, to do that try something like this:
public event EventHandler Click;
void btn_del_Click(object sender, EventArgs e)
{
if (NumberOfRowControls > 0)
{
var rowToWhack = panel_rows.Controls.Children().Single(x => x.ID == "myrow" + (NumberOfRowControls - 1));
panel_rows.Controls.Remove(rowToWhack);
NumberOfRowControls--;
}
if(Click != null) Click(this, e);
}
void btn_add_Click(object sender, EventArgs e)
{
var row = NewRow(NumberOfRowControls);
panel_rows.Controls.Add(row);
if(Click != null) Click(this, e);
}
And update the UpdatePanel to be looking for it:
<asp:UpdatePanel ID="UpdatePanelFoo" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<grid:tablegrid ID="tablegrid_chapters" runat="server" SomeProperty="bar" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="tablegrid_chapters" EventName="Click">
</Triggers>
</asp:UpdatePanel>
Make sure you add a ScriptManager as well to the page, otherwise there's no UpdatePanel functionality.

Reload a Gridview onclientclick

I have a gridview that is only shown in a modal popup. right before I call the modal popup I set a value in a textbox. The gridview inside the modal popup depends on that textbox's value for it's data to show up at all. SO onclick I want to reload the gridview so that it will reload with the textbox's value. Any ideas?
Essentially... Using update panel, the button press event should trigger the partial postback where your query is rerun that would then allow you to do another databind on your grid. THis would all be followed by a modalPopUp.Show()...
CODE BEHIND
protected void btnAdd_Click(object sender, EventArgs e)
{
if(!String.IsNullOrEmpty(this.txtMyValue.Text))
{
AddValue(this.txtMyValue.Text);
UpdateGrid();
this.UpdatePanel1.Update();
}
else
{
//ooooops
}
}
private void AddValue(String str)
{
DataAccess.AddSomeValue(str);
}
private void UpdateGrid()
{
this.GridView1.DataSource = DataAccess.GetData();
this.GridView1.DataBind();
}
FRONT END
<asp:UpdatePanel ID="UpdatePanel1" runat="server" updatemode="Conditional">
<ContentTemplate>
<asp:TextBox id="TextBox1" runat="server" />
<asp:Button id="btnAdd" OnClick="btnAdd_Click" runat="Server">
<div id="MyModalArea">
<asp:GridView id="GridView1" runat="Server" ..... >
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
Put popup window content (grid and the rest there is) in separate aspx page and then when you initialize popup window, send textbox value as parameter:
MyPopupWindowContent.aspx?TextBoxValue=something

Resources