EDUDOTNET

Tuesday, November 4, 2014

Create Dynamic ASP.NET Server Control Using C# With Theme(Template) Style On Controls

This blog post will provide you how to Add and Create Dynamically ASP.NET Server Controls and place the controls in to an html DIV server Tag. Create your ap.net web application [project] and integrate your required theme(template).
As per your requirement create a web form and design page layout and create a conatainer div with id="myFormContainer" and div as a server control-runat="server", In this div going to add child div, label, textbox, button etc controls dynamically.

Create Dynamic ASP.NET Server Control
All controls list retrieved from "Controls" josn object (List). You can create a json file and Deserialize json object with c# class object, easily create number of controls dynamic with required attributes eg, Id, text etc.. You can also try to controls list retrieved from database table.
Code : Classes, Methods and Events

Step 1: Create basic classes
public class Control
{
public string ID { get; set; }
public string LabelText { get; set; }
}

public class ControlList
{
public List<Control> Controls { get; set; }
}

Step 2: Controls list (Get from json object or database table)
// create the div to add form elements from a controls list
string json = @"{Controls:[{ID:'UserName', LabelText:'User Name'},{ID:'EmailId', LabelText:'Email Address'},{ID:'Password', LabelText:'Password'},{ID:'Phone', LabelText:'Phone Number'}]}";
var controls = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<ControlList>(json);
Step 3: Create Method to bind controls
/// <summary>
/// Create & Add Contols to the container div
/// </summary>
/// <param name="controls"></param>
private void AddControls(ControlList controls)
{
foreach (var control in controls.Controls)
{
//Create Group Container Div
HtmlGenericControl div = new HtmlGenericControl("div");
div.Attributes.Add("class", "form-group");

// create label and add to the div
div.Controls.Add(new Label() { Text = control.LabelText, AssociatedControlID = control.ID, CssClass = "col-md-2 control-label" });

//create the div to add controls eg. textbox, checkbox etc.
HtmlGenericControl divInner = new HtmlGenericControl("div");
divInner.Attributes.Add("class", "col-md-10");

//Create TextBox
divInner.Controls.Add(new TextBox() { ID = control.ID, CssClass = "form-control" });
//Create Validator
divInner.Controls.Add(new RequiredFieldValidator() { ControlToValidate = control.ID, CssClass = "text-danger", ErrorMessage = "The user name field is required." });

div.Controls.Add(divInner);
Container.Controls.Add(div);
}

//create button with event and add to the div
var button = new Button { ID = "btnClick", Text = "Create" };
button.Click += btnClick_OnClick;
Container.Controls.Add(button);
}

Step 4: Call CreateControl() method on OnInit event
/// <summary>
/// Load Controls on OnInit event
/// </summary>
/// <param name="e"></param>
override protected void OnInit(EventArgs e)
{
// create the div to add form elements from a controls list
string json = @"{Controls:[{ID:'UserName', LabelText:'User Name'},{ID:'EmailId', LabelText:'Email Address'},{ID:'Password', LabelText:'Password'},{ID:'Phone', LabelText:'Phone Number'}]}";
var controls = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<ControlList>(json);

AddControls(controls); // Method with controls list param
}


HTML Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>DEMO - Create Dynamic ASP.NET Server Control</title>
<link href="css/Site.css" rel="stylesheet" />
<link href="css/bootstrap.min.css" rel="stylesheet" />
<link href="css/bootstrap.css" rel="stylesheet" />
</head>
<body>
<div class="container body-content">
<form id="form1" runat="server">
<div>
<h3>DEMO - Create Dynamic ASP.NET Server Control</h3>
<div class="row">
<div class="col-md-8">
<h4><u>User Register</u></h4>
<section id="Form">
<div id="Container" runat="server" class="form-horizontal">
<span class="menu-icon"><i class="fa fa-globe"></i></span>
</div>
</section>
</div>
</div>
</div>
</form>
</div>
</body>

</html>

Thursday, May 8, 2014

Entity Framework 4 - What is the syntax for joining 2 tables then paging them?

Example of joining two tables - Entity Framework

var data = db.Categorie   
   .Join(db.CategoryMap, 
      cat => cat.CategoryId,    
      catmap => catmap.ChildCategoryId, 
      (cat, catmap) => new { Category = cat, CategoryMap = catmap }) 
   .Select(x => x.Category);


 

See more details : http://stackoverflow.com/questions/10222255/entity-framework-4-what-is-the-syntax-for-joining-2-tables-then-paging-them/10222383#10222383
 

Friday, February 14, 2014

SMTP Microsoft Office 365 .Net: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated.

Issue/Error with SMTP server of MS Office365
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated.

Approach 1. 
When we using SMTP of MS office 365 and we are getting error like below then we need to configure and change setting in Office 365 manage panel. And also need to check Credentials password of associated user account password is expired or account is locked. And also set "Never Expire Password" option office365 exchange account.

Approach 2. 
2.1 Login into office 365 account and go to Admin menu and click on Exchange.

2.2 After that go to "recipient" > "mailboxes"
2.3 Under "mailboxes" > click on "Edit" icon and open a popup

2.4 Inside the popup click on "mailbox delegation"
2.5 Remove display name "NT AUTHORITY\SELF" and Add new display user name click on save button.





Thursday, February 13, 2014

How To Close Popup Window And Redirect To Parent Window

When we click login Facebook redirect to in the popup.But we need to redirect from popup window to parent window after that close popup when user click on login/allow button.

We can use for this ClientScript.RegisterStartupScript function and write java-script code and after that it`ll be close automatically.

Below code we can use where we need to open & close popup from code (C#)

Code:

Function
public void RedirectToParent(string url, Type type)
        {
            string js = "";
            js += "window.opener.location.href='" + url + "';";
            js += "window.close();";
            ClientScript.RegisterStartupScript(type, "redirect", js, true);

        }



Calling Function

Type type = this.GetType();
RedirectToParent("http://www/xyz.co/...", type);