I need find this <a> tag resided in a FormView control, I need to remove this a tag depending on the condition but I can't find it using FormView.FindControl method
<asp:UpdatePanel ID="upDiscipline" runat="server">
<ContentTemplate>
<asp:FormView ID="fvMediaIntro" runat="server">
<ItemTemplate>
<div class="clipControls">
<a runat="server" id="iNeedToFindThis" href="#">here</a>
</div>
</ItemTemplate>
</ContentTemplate>
</asp:UpdatePanel>
I tried fvMediaIntro.FindControl() and fvMediaIntro.Row.FindControl(), neither worked.
Any idea please??
FindControl will work only after those controls are created i.e when data is bound to the FormView. So you need to use appropriate event on FormView such ItemCreated or DataBound. For example,
protected void fvMediaIntro_ItemCreated(Object sender, EventArgs e)
{
var control = fvMediaIntro.Row.FindControl("iNeedToFindThis") as HtmlAnchor;
}
Assuming, you are binding in page_load or using mark-up, you can also use prerender event of parent page/control safely to do FindControl.
Related
I'm having issues with the asp.net textbox within an update panel. It works perfectly fine when adding or removing each individual character but if I highlight all of the text within the textbox, and then remove it a full postback occurs, not a partial postback which is expected.
Why is this happening? I haven't found anything related to this particular problem so it's likely I'm doing something wrong.
Example aspx:
<asp:UpdatePanel ID="updExample" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:Repeater ID="rptExample" runat="server" .... >
<ItemTemplate>
<asp:TextBox ID="txtExample" runat="server" ClientIDMode="static" Text='<%# Eval("Example") %>' OnTextChanged="txtExample_TextChanged" AutoPostBack="true"></asp:TextBox>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
Example TextChanged Event:
protected void txtExample_TextChanged(object sender, EventArgs e)
{
updExample.Update();
}
Additional Notes:
Switching UpdateMode to 'Always' doesn't work.
Karthikeyan Nagaraj pointed out in the comments to try adding triggers alongside what I already had. I did in fact already have this, however, I was assigning the trigger in the ItemDataBound event which I realized was incorrect after reinvestigating. The ItemCreated event was much more suited.
I had no issues finding the control in the ItemCreated event, however adding a new async postback trigger to the update panel gave me grief and said the control couldn't be found when changing the text. To resolve this, I used the script managers RegisterAsyncPostBackControl(); method as shown below.
protected void rptExample_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var input = e.item.FindControl("txtExample");
if (input != null) {
ScriptManager sm = ScriptManager.GetCurrent(this);
sm.RegisterAsyncPostBackControl(input);
}
}
I need to have UpdatePanel with asyncpostback, but in my case it seems that no partial postback happens, but fullpostback. I am new to web forms, please, check the code:
<%# Register TagPrefix="Cust" TagName="CompanyInformationView" Src="~/CustomControls/CompanyInformationView.ascx" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<asp:UpdatePanel runat="server" ID="UpdatePanel1" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" OnClick= "Button1_Click" Text="test" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<Cust:CompanyInformationView ID="CompanyInformationView" runat="server" />
</asp:Content>
So I have Test Button. OnClick it should do "nothing" for test. Also there is custom control on web form. Here is server side code for this form:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// fill in custom control
CompanyInfo c = GetInfo();
CompanyInformationView.Company = c;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
var i = 1;
}
CompanyInformationView is custom control with property "Company". There is no ViewState added for this property (so it cannot be loaded properly if postback is done). When I click on Test Button, the page fails, because "CompanyInformationView.Company" is not set (it is not set, because it cannot be loaded from ViewState, I guess).
Instead, I think that it should not work like this. AsynPostback should deal only with UpdatePanel.
Why it wants to reload custom control? Doesn't it mean that Full postback happen or maybe I do not understand Asyncpostback?
PageLoad and all other events are raised on every get\postback, no matter if it's async or full,
In asyncpostback, the response which the server renders includes only the content inside the updatepanel.
Try to Put <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
Before
<asp:UpdatePanel runat="server" ID="UpdatePanel1" ChildrenAsTriggers="true" UpdateMode="Conditional">
Async post back in web forms still proceeds the whole page life cycle as if it's a traditional post. the difference is only the updated content inside target update panel that will be sent in response. so in this case, async post back will still give you exception unless you populate the custom control no matter get/post.
you need to also put the custom control inside another update panel or in the same update panel as the button in order see partial update to happen.
I have a repeater and I set the value of an html check box control with the
value of an enumeration instead of hard-coding a magic number. When I try to
access the html check box control in the repeater's ItemCreated event handler,
the value is an empty string. Why is this and how can I fix it?
C# Code
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var myObject = e.Item.DataItem as MyObject;
if (myObject != null)
{
var checkBox = e.Item.FindControl("checkbox1") as HtmlInputCheckBox
// The value is empty!
var value = checkBox.Value;
}
}
Not Working
<asp:Repeater ID="Repeater1" OnItemCreated="Repeater1_ItemCreated" runat="server">
<ItemTemplate>
<input type="checkbox" id="checkbox1" value='<%# SomeEnum.Value %>' />
</ItemTemplate>
</asp:Repeater>
Working
<asp:Repeater ID="Repeater1" OnItemCreated="Repeater1_ItemCreated" runat="server">
<ItemTemplate>
<input type="checkbox" id="checkbox1" value="1" />
</ItemTemplate>
</asp:Repeater>
ItemCreated is triggered before ItemDataBound and also on every postback to recreate he controls even when the Repater is not databound again. So i would not use ItemCreated if you need to access the DataSource of any databound WebControl like Repeater.
Apart from that, make the checkbox runat=server(or use a ASP.NET CheckBox) if you want to find it on the server.
I have the following code for an OnTextChanged event:
protected void CustomTextBox_OnTextChanged(object sender, EventArgs e)
{
if (tick.Attributes["class"] == "tick displayBlock")
{
tick.Attributes["class"] = "displayNone";
tick.Attributes.Add("class", "displayNone");
}
checkAvailability.Attributes.Add("class", "displayBlock");
checkAvailability.Attributes["class"] = "displayBlock";
}
And:
<asp:UpdatePanel ID="upMyUpdatePanel" runat="server">
<ContentTemplate>
<uc:CustomTextBox ID="txtUserName"
OnTextChanged="CustomTextBox_OnTextChanged"
AutoPostBack="True"
class="someClass">
</uc:CustomTextBox>
</ContentTemplate>
</asp:UpdatePanel>
So I have the above code works perfectly fine in Chrome, IE 8, 9.
However Firefox 6 doesn't seem to do a partial postback.
Before anyone asks I have bubbled up events ontextchanges and autopostback to be used by my customtextbox instances. You can see how on related question: Exposing and then using OnTextChange Event handler
This issue was being cause by a double AutoPostBack.
Parent control:
<uc:CustomTextBox ID="ctbMyTextBox"
OnTextChanged="CustomTextBox_OnTextChanged"
AutoPostBack="True"
class="someClass">
</uc:CustomTextBox>
Child Control:
<asp:UpdatePanel ID="upMyUpdatePanel" runat="server">
<ContentTemplate>
<uc:CustomTextBoxChild ID="ctbcMyTextBox"
OnTextChanged="CustomTextBox_OnTextChanged"
AutoPostBack="True"
class="someClass">
</uc:CustomTextBoxChild>
</ContentTemplate>
</asp:UpdatePanel>
In the parent control I removed AutoPostBack="True" and this fixed the issue for me.
If someone can give further explanation as to why a Double AutoPostback can cause this I would be happy to check your answer as correct.
Remove the autopostback from the parent and add it to child(your custom one). That will solve the issue. Further, since its a custom control so you are inheriting the properties from your parent. Even if you remove the Autopostback from the custo control, i think it might work as the property is true in its parent, by default.
set UpdatePanel Mode="Conditional" and
AutoPostBack="True" and enableviewstate="true"
now it will work
I have a listview that adds controls in the ItemDataBound Event. When the postback occurs, I cannot find the new controls. After a bit of research, I found that ASP .NET needs these controls created every time, even after postback. From there I moved the function to bind the ListView outside of the if (!Page.IsPostBack) conditional. Now I get the dynamic controls values but the static controls I have are set to their defaults. Here is a sample of what I am trying to accomplish:
For brevity, I left some obvious things out of this example.
<asp:ListView runat="server" ID="MyList" OnItemDataBound="MyList_ItemDataBound">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceholder" />
</LayoutTemplate>
<ItemTemplate>
<asp:PlaceHolder runat="server" ID="ProductPlaceHolder">
<asp:TextBox runat="server" ID="StaticField" Text="DefaultText" />
<asp:PlaceHolder ID="DynamicItems" runat="server" />
</asp:PlaceHolder>
</ItemTemplate>
</asp:ListView>
and here is the codebehind:
protected void MyList_ItemDataBound(object sender, System.Web.UI.WebControls.ListViewItemEventArgs e) {
PlaceHolder DynamicItems = (PlaceHolder)e.Item.FindControl("DynamicItems");
DynamicItems.Controls.Add(textbox);
}
So, like I said, if I only databind when Page != PostBack then I cant find my dynamic controls on postback. If I bind every time the page loads then my static fields get set to their default text.
Try moving the data binding of the ListView into the OnInit() event.
Very similar question (instead of populating a ListView the guy is generating a set of buttons). Briefly, you'll find that you have to store the items in the list in your Viestate - than fish it out on Postback and re-populate the list.
Note that this solutions implies dropping data-binding (which you might not wanna do for others reasons).
Hope it helps.