LinkButton inside a repeater inside an update panel causing full postback ASP.NET C#

If you place a Linkbutton inside a repeater inside an UpdatePanel the button will cause a full postback.

A way around this is to register the LinkButton for Asynchronous postback in the code behind.

Code to do this:

foreach (RepeaterItem ri in rpt.Items)
{
if (ri.ItemType == ListItemType.Item || ri.ItemType == ListItemType.AlternatingItem)
{
LinkButton lb = (LinkButton)ri.FindControl("lb");
ScriptManager1.RegisterAsyncPostBackControl(lb);
}
}

True

How to find the type of a control in ASP.NET C#

Below is example code on how to find the type of a control in ASP.NET C#

if (plcCategories.HasControls()) // Check if the placeholder has controls
            {
                foreach (Control c in plcCategories.Controls) //loop through each control
                {
                    if (c.GetType().Name.ToLower() == "checkbox") //check if the type is checkbox
                    {
                        CheckBox chk = (CheckBox)c; // cast the object as a Checkbox
                        if (chk.Checked == true) // Check if it has been checked
                            lstChecked.Add(chk.ID.ToString()); //if so then add to the check listed
                    }
                }
            }

True

Refresh a div using jQuery and AJAX

To reload only a certain part of a page use the below jQuery call. The below example will refresh the area with Id of content

$("#content").load(location.href + " #content>*", "");

True

Disable a ScriptManager in the code behind ASP.NET C#

If you are using your controls in jQuery modal windows then in some cases these may cause the ScriptManager to function incorrectly.

You can disable the ScriptManager in the code behind if you place it inside a panel in the front end code:

<asp:Panel ID="pnlScriptManager" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
</asp:Panel>

Then pass a querystring called popup to your controls when opening them in a modal window and use the below code:

//DISABLE SCRIPTMANAGER IF IN POPUP MODE
if(!String.IsNullOrEmpty(Request.QueryString["popup"]))
  pnlScriptManager.Visible = false;
else
  pnlScriptManager.Visible = true;

True

ValidateRequest="false" not working in ASP.NET 4.0

To revert to the behavior of the ASP.NET 2.0 request validation feature, add the following setting in the Web.config file:

<system.web>
 <httpRuntime requestValidationMode="2.0" />
</system.web>

True

Adding a meta tag from the code behind ASP.NET C#

Below is the code that will allow you to add a meta tag from the code behind:

First ensure you have:

using System.Web.UI.HtmlControls;

Then use the below code

public static void AddMeta(HtmlHead head, string name, string content)
{
// Prepare the meta tag
HtmlMeta metaTag = new HtmlMeta();
metaTag.Name = name;
metaTag.Content = content;

// Add the meta tag to the head
head.Controls.Add(metaTag);
}

Then call the function like below:

AddMeta(Page.Header, "google-site-verification", "abcdefghijk");

True

Adding a CSS Link from the code behind in ASP.NET C#

Below is the code that will allow you to add a CSS Link from the code behind:

private void addCSS()        

{           
// CREATE CSS LINK           
HtmlHead head = (HtmlHead)Page.Header;           
HtmlLink link = new HtmlLink();           
link.Attributes.Add("href", Page.ResolveClientUrl("~/Styles/Clients/Site.css"));           
link.Attributes.Add("type", "text/css");           
link.Attributes.Add("rel", "stylesheet");           
head.Controls.Add(link);       
}

 

True