Creating an asyncpostback from a control inside a repeater - c#

I have a repeater whose ItemTemplate contains a PlaceHolder that I add input controls (ListBox, TextBox, CalendarExtender etc.) to on ItemDataBound:
<asp:UpdatePanel ID="ReportParameterUpdatePanel" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Repeater ID="ReportParameterEditRepeater" OnItemDataBound="ReportParameterEditRepeater_ItemDataBound" runat="server">
<ItemTemplate>
<asp:PlaceHolder runat="server" ID="ParameterEntryPlaceholder"></asp:PlaceHolder>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
How can I generate an asyncpostback from one of these TextBoxes inside the repeater (on TextChanged)?
The control is created dynamically and I only want to create the postback under certain conditions, so it needs to be done from code behind.
I have tried:
OnItemCommand (but this appears to be just for buttons)
ScriptManager.RegisterAsyncPostBackControl (doesn't seem to do anything on TextChanged)
UpdatePanel.Triggers.Add(new AsyncPostBackTrigger ...) (unable to find the TextBox as it is inside the repeater)

In ReportParameterEditRepeater_ItemDataBound you will need to assign a unique ID to each control, and then bind the text changed event. I then like to store those in session. Then add it to your placeholder.
Below is how I did it in my site for a button click event:
TemplateControls_Headline ctrl = (TemplateControls_Headline)LoadControl("~/Controls/TemplateHeadline.ascx");
ctrl.ID = "MyCtrl_" + CMSSession.Current.AddedTemplateControls.Count;
ctrl.Remove.Click += new EventHandler(RemoveItem_OnClick);
MySession.Current.AddedTemplateControls.Add((Control)ctrl);
PlaceHolder ph = accAddTemplates.FindControl("phAddTemplateControlsArea") as PlaceHolder;
ph.Controls.Add(ctrl);
Then, in your OnInit of the page, you have to re-bind everything from the viewstate since you are creating them dynamically, this is where the unique id you created comes in (this is mainly for postbacks):
protected override void OnInit(EventArgs e)
{
PlaceHolder ph = accAddTemplates.FindControl("phAddTemplateControlsArea") as PlaceHolder;
int counter = 0;
foreach (UserControl ctrl in MySession.Current.AddedTemplateControls)
{
ctrl.ID = "MyCtrl_" + counter;
ctrl.Remove.CommandArgument = counter.ToString();
ctrl.Remove.Click += new EventHandler(RemoveItem_OnClick);
counter++;
ph.Controls.Add(ctrl);
}
base.OnInit(e);
}

Related

Update Panel updates once and causes post back on other triggers

I am dynamically building Image buttons and placing them inside an ASP placeholder. When the user clicks on an image(button) it should update the panel with the school's information. The panel updates correctly the first time a button is clicked but when I click another button it causes a post back. On that second, third, etc... it should update the panel without a post back.
When I am building the buttons I am also creating triggers and setting the control ID to the button's ID. I have debugged and verified that these triggers are being created properly.
Button and Trigger Creation
int counter = 0;
foreach (DataRow row in tempTable.Rows)
{
var button = new ImageButton
{
ID = counter.ToString(),
CausesValidation = false
//OnClientClick= "return false;"
};
button.Command += new CommandEventHandler(buildSchoolInfo); //The onclick event that builds the school info
button.CommandName = "ImageButton" + counter.ToString();
button.CommandArgument = "Test" + counter.ToString();
SchoolListPH.Controls.Add(button); //Add image button to placeholder
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = button.ID;
//trigger.EventName = "Click";
schoolInfoUP.Triggers.Add(trigger); //Adding the trigger to the Update Panel
counter++;
}
ASPX Code
<div style="border: 1px solid black">
<asp:PlaceHolder ID="SchoolListPH" runat="server"></asp:PlaceHolder>
</div>
<%-- This is the literal that builds with all of the selected schools information --%>
<asp:UpdatePanel ID="schoolInfoUP" runat="server" >
<ContentTemplate>
<asp:Literal ID="litTable" runat="server"></asp:Literal>
</ContentTemplate>
</asp:UpdatePanel>
After searching the internet for wayyyy to long this single line fixed it for me.
ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(button);
I placed it just after I added the trigger to the update panel.

Clearing the TextBox values in Update Panel when dropdown index changed in c#

I want to clear the Text field's values which is taken in Update panel but because of update panel the values are not getting cleared. It is not finding the TextBox control declared inside the Update panel in .aspx. So, when DropDown selected index changes, I want to reset the text fields values. Can anybody please help me out?
Here is my C# code for clearing the textfield's values:
void ClearInputs(ControlCollection ctrls)
{
foreach (Control ctrl in ctrls)
{
if (ctrl is TextBox)
((TextBox)ctrl).Text = String.Empty;
ClearInputs(ctrls.Controls);
}
}
to find textbox in updatepanel use
TextBox tb = (TextBox)updatepanel1.FindControl("textbboxid");
use update panel mode conditional and do updatepanel1.Update(); after cleaning your textboxes
Try This:
public void CleartextBoxes(Control parent)
{
foreach (Control x in parent.Controls)
{
if ((x.GetType() == typeof(TextBox)))
{
((TextBox)(x)).Text = "";
}
if (x.HasControls())
{
CleartextBoxes(x);
}
}
}
on your drop down change event call this funcation as:
CleartextBoxes(this);
Ok Try this, you have update panel with 20 textbox in it. In the Update panel get all textboxes in one div like this :
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager2" runat="server" ScriptMode="Release"></asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="div_d2">
<ContentTemplate>
<div runat="server" id="div_d">
<asp:TextBox runat="server" ID="txt1">
</asp:TextBox>
<asp:TextBox runat="server" ID="txt2"></asp:TextBox>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button runat="server" ID="btnClear" Text="dasdas" OnClick="btnClear_Click" />
</form>
When the textboxes are in the div the clear code will work like this example I show you :
protected void btnClear_Click(object sender, EventArgs e)
{
clearText(div_d);
}
private void clearText(Control PanelID)
{
foreach (Control c in PanelID.Controls)
{
if (c is TextBox)
{
TextBox thetextBox = c as TextBox;
thetextBox.Text = "";
}
}
}

ASP updatepanel doesn't refresh ajax tabpage

I have an ajax tabcontainer in an updatepanel with all tabpages set visible until you want to add a tabpanel based on the dropdownlist selected value
CODE:
<cc1:TabContainer ID="tabControlParameters" runat="server" CssClass="ajax__tab_xp"
ScrollBars="Both" ActiveTabIndex="15" UseVerticalStripPlacement="True">
<%--EnvironmentTab --%>
<cc1:TabPanel ID="pnlEnvironment" HeaderText="Environment" runat="server" Visible="false">
<ContentTemplate>
//somecontent
</ContentTemplate>
</cc1:TabPanel>
<cc1:TabPanel ID="pnlDatabase" HeaderText="Environment" runat="server" Visible="false">
<ContentTemplate>
//somecontent
</ContentTemplate>
</cc1:TabPanel>
<cc1:TabPanel ID="pnlFirstError" HeaderText="Environment" runat="server" Visible="false">
<ContentTemplate>
//somecontent
</ContentTemplate>
</cc1:TabPanel>
With a button add which is inside the Updatepanel and has a correct async trigger assigned to it.
From C# codebehind I've made a loop to check if the dropdownlist selectedvalue = panel_headertext if so make it visible
CODE:
protected void btnAddParameters_Click(object sender, EventArgs e)
{
String Parameter = ddlParameterTypes.SelectedValue.ToString();
AjaxControlToolkit.TabContainer container = (AjaxControlToolkit.TabContainer)tabControlParameters;
foreach (object obj in container.Controls)
{
if (obj is AjaxControlToolkit.TabPanel)
{
AjaxControlToolkit.TabPanel tabPanel = (AjaxControlToolkit.TabPanel)obj;
if (tabPanel.HeaderText == ddlParameterTypes.SelectedValue)
{
tabPanel.Visible = true;
tabPanel = tabControlParameters.ActiveTab;
container.ActiveTab = tabPanel;
}
}
}
}
Now this works perfectly if the updatepanel trigger is set to fullPostback but it's set to async postback then it only works on the first click even though the event is fired every time I'm clicking on the button. Am I missing something obvious here?
Petar
You have the same value in HeaderText for each of your TabPanels. I think it'll work if you correct the HeaderText attributes.

cross page posting asp.net not returning values

I have a web form as
<asp:TextBox ID="txtname" runat="server" Text="Post on Next Page"/>
<asp:Button ID="btn1" runat="server" PostBackUrl="~/Page2.aspx" Text="Post on next page" />
Now on Page2.aspx the code-behind is as follows:
protected void Page_Load(object sender, EventArgs e)
{
if(PreviousPage!=null && PreviousPage.IsCrossPagePostBack)
{
TextBox txt1 = (TextBox)PreviousPage.FindControl("txtname");
label1.Text = "Value: " + txt1.Text;
}
}
I end up getting the error object reference not set to instance of an object for txt1
Where label1 is a label used to display the output. However, the value is not displayed.
What step am i missing?
Try this
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
//get the content place holder from master page of your previous page where your controls are placed
//In this code the txtname textbox is placed inside ContentPlaceHolderID="MainContent"
var cp =PreviousPage.Master.FindControl("MainContent") as ContentPlaceHolder;
//find the textbox inside content place holder from previous page
TextBox txt1 = cp.FindControl("txtname") as TextBox;
label1.Text = "Value: " + txt1.Text;
}
Are you sure that PostBackURL is valid on a Textbox? Normally this attribute is attached to something that submits, such as a Button or LinkButton, eg:
<form runat="server">
Name:<asp:textbox id="TextBox1" runat=Server />
<asp:button id="Button1" Text="Submit"
PostBackUrl="demo_postbackurl.aspx" runat="Server" />
</form>
Edit: Aha! - you do use a button.
Your code looks OK to me.
If the TextBox is within another control FindControl might not find it - if (for example) it's within a Panel you would need to do something like
TextBox txt1 = (TextBox)PreviousPage.MyPanel.FindControl("txtname");
If it's not within another control then I'm afraid I don't know.

ASP.net UpdatePanel dynamically using C# code

I have a custom usercontrol that has some Asp.net code. I would like to write the same code but with C#.
The problem is that i don't know how to put a repeater and some buttons into the ContentTemplate.
The Asp.net Code :
<asp:UpdatePanel runat="server" ID="up">
<ContentTemplate>
<n2:Repeater ID="rpt" runat="server">
<ItemTemplate></ItemTemplate>
</n2:Repeater>
<asp:LinkButton runat="server" ID="btnFirst"
Visible="false" Enabled="false" Text="<<" OnClick="btnFirst_Click" />
</ContentTemplate>
</asp:UpdatePanel>
So how could I write this chunk in C# code? To be precise, how could I insert the repeater and the Linkbutton into the ContentTemplate.
Note : I don't want to use the LoadTemplate to do it.
Edit
I have tried ContentTemplateContainer.Controls.Add():
private UpdatePanel up = new UpdatePanel();
private Repeater rpt = Repeater();;
public Paging{
//Add repeater to updatePanel
up.ContentTemplateContainer.Controls.Add(rpt);
AsyncPostBackTrigger apb3 = new AsyncPostBackTrigger();
apb3.ControlID = "btnFirst";
apb3.EventName = "Click";
//Add Triggers to updatePanel
up.Triggers.Add(apb1);
//Create buttons
btnFirst = new LinkButton();
btnFirst.Visible = false;
btnFirst.Enabled = false;
btnFirst.Text = "<<";
btnFirst.Click += new EventHandler(btnFirst_Click);
//Add buttons to update panel
up.ContentTemplateContainer.Controls.Add(btnFirst);
}
protected void Page_Load(object sender, EventArgs e)
{
rpt.ItemTemplate = LoadTemplate("~/UI/Templates/NewsEvent.ascx");
....
}
I have an error caused by the first line on Page_Load :
Databinding methods such as Eval(), XPath(), and Bind() can only be used in controls contained in a page.
This is NewsEvent.ascx:
<img src='<%# Eval("ImageThumbnail") %>' alt="" />
you cant do this with ITemplate types ... i had a similar problem trying to clone a tabpanel in a tabcontainer control ... i already had a hidden tabpanel and all i wanted to do was create a new tabpanel and basically instantiate the ITemplate from the hidden one in the new one.
The problem is ITemplate ... it's not very dynamic for code behind interactions i would suggest putting that markup on the page as you already have and setting visible = false on the parent then when you need to databind and show the hidden panel.
getting the initial bind to work isn't the problem ... its the postback handling ...
Ajax TabContainerTabPanels Break postbacks

Categories