Monday, 28 December 2015

Entity Framework Development Approaches

0

This article is taken from msdn.microsoft.com, it was a necessary part so i provided the content here also.


The Entity Framework is the easiest ORM framework to learn for an ASP.NET developer. Because Microsoft continues to focus ORM development efforts on the Entity Framework, Microsoft and third parties continue to produce new tutorials, videos, and books. Most new tutorials use the Entity Framework even if they are focused on some other technology, such as the latest release of MVC or new model binding features for Web Forms in ASP.NET 4.5.


There are three ways you can work with data models and databases in the Entity Framework:Database FirstModel First, and Code First.



  • Database First
    If you already have a database, the Entity Framework designer built into Visual Studio can automatically generate a data model that consists of classes and properties that correspond to existing database objects such as tables and columns. The information about your database structure (store schema), your data model (conceptual model), and the mapping between them is stored in XML in an .edmx file. The Entity Framework designer provides a graphical UI that you can use to display and edit the .edmx file.
  • Model First
    If you don't have a database yet, you can begin by creating a model in an .edmx file by using the Entity Framework graphical designer in Visual Studio. When the model is finished, the Entity Framework designer can generate DDL (data definition language) statements to create the database. As in Database First, the .edmx file stores model and mapping information.
  • Code First
    Whether you have an existing database or not, you can use the Entity Framework without using the designer or an .edmx file. If you don't have a database, you can code your own classes and properties that correspond to tables and columns. If you do have a database, Entity Framework tools can generate the classes and properties that correspond to existing tables and columns. The mapping between the store schema and the conceptual model represented by your code is handled by convention and by a special mapping API. If you let Code First create the database, you can use Code First Migrations to automate the process of deploying the database to production. Migrations can also automate the deployment of database schema changes to production when your data model changes.

Choose Code First for new development unless you want to use a graphical designer to model database objects and relationships. The Entity Framework designer only works with Database First and Model First. Before you choose Database First or Model First, however, consider how you want to handle updates to the data model after you create the database, and how you want to deploy the database and deploy updates to it. Code First Migrations automates the process of implementing and deploying database schema changes that result from data model changes. The advantages of Code First Migrations might outweigh the advantages of the Entity Framework designer.

Read More »

Tuesday, 15 December 2015

How to make textbox, dropdownlist combination control using ajax combobox in asp.net

0

Step 1 :

Add the AjaxControlToolkit Reference to your asp.net website and register its assembly on the required page.

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

Step 2:

Add the ToolkitScriptManager above any ajax tool, using this code,

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>


Step 3:

Now add the following combobox ajax control, combobox is a combination of textbox and dropdown with all dropdown properties and few additional.

<asp:ComboBox ID="ddlRequestTo" runat="server" DropDownStyle="Simple" AutoCompleteMode="SuggestAppend" CssClass="col-md-9">
</asp:ComboBox>


Some Imp Points :

DropDownStyle :


  • Simple :- Any text is allowed and the list is always displayed.
  • Dropdownlist :- User are not allowed to enter text that does not match an item in the list.
  • DropDown :- (default value), any text is allowed

AutoCompleteMode :


  • Suggest :- The combobox will show the list, highlight the first matched item.
  • Append :- The combobox will append the remainder of the first matched item to the user-typed text and highlight the appended text.
  • None :- (default value), Combobox autocomplete behavior is disable.
  • SuggestAppend :- Both behavior of append and suggest will be applied.

Now, in case if you need to enhance or override the default css of the combobox, you can do it as follows :


For Textbox container use the following id :


.ajax__combobox_textboxcontainer {
}

For textbox container input box

.ajax__combobox_textboxcontainer input {
}


For Dropdown list items :

.ajax__combobox_itemlist {
}


When changing itemlist css do add the !important tag as below :


.ajax__combobox_itemlist {
top: 0px !important;
width: 80% !important;
left: 185px !important;
}



Read More »

Thursday, 10 December 2015

Using ValidatorCalloutExtender to Enhance RequiredFieldValidator to validate.

0

Step 1 : First you require Ajaxtoolkit to be added to your  asp.net toolbox.

Step 2: 
Add the RequiredFieldValidator control from toolbox for textbox control to validate.

Step 3: 
Add the ToolkitScriptManager control from the toolbox just below the form tag.

Step 4:
Use the ValidatorCalloutExtender control from the ajax toolkit tools which you have got after adding ajaxtoolkit.dll to the toolbox.


The code must look like this :

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>

<span> First Name</span>
<asp:TextBox ID="txtFirstName" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="rqdFirst" runat="server" ControlToValidate="txtFirstName" ErrorMessage="Enter First name!" Display="None"></asp:RequiredFieldValidator>
<asp:ValidatorCalloutExtender ID="ValidatorCalloutExtender1" runat="server" TargetControlID="rqdFirst">

</asp:ValidatorCalloutExtender>



The output window will look like this :



Read More »

Wednesday, 9 December 2015

How to pass repeater/any control from one page to another in asp.net?

0

Step 1:

 Make two pages, one where the control (repeater) is present and another where you need to transfer it, namely source.aspx and destination.aspx.


Step 2:

At source.aspx page write down your control (repeater) and bind it, in my case i am passing the repeater, so that i could print it on another page, you can have other cases to use this functionality..

So at design page of source.aspx

<asp:Repeater ID="rptSalary" runat="server">
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>

<asp:Button ID="btnPrint" runat="server" Text="Print Slip"

onclick="btnPrint_Click" />


At source.aspx.cs

On button click event at which you need this control (repeater) to move to another page.

protected void btnPrint_Click(object sender, EventArgs e)
{
Server.Transfer("~/destination.aspx");
}



Step 3:

At destination.aspx page add all the css file which you might have used at source.aspx, to get the style as well

At destination.aspx.cs page where we code c#, do the following code to find the specific control (repeater in our case) from the source.aspx page


protected void Page_Load(object sender, EventArgs e)
{
if (this.Page.PreviousPage != null)
{
Control ContentPlaceHolder1 = this.Page.PreviousPage.Master.FindControl("ContentPlaceHolder1");
Repeater rptSalary = (Repeater)ContentPlaceHolder1.FindControl("rptSalary");

}
}

Now, if add this rptSalary instance of repeater to any control on this page, or create that too dynamically as below :


Panel Panel1 = new Panel();
Panel1.Controls.Add(rptSalary);
Page.Controls.Add(Panel1);


Now, we have successfully passed the control to another page.
And yeah if you too want to print this repeater or any specific control from a webpage, then follow this whole procedure and just add one more line to it i.e ,

Response.Write("<script>window.print();</script>");



The whole code for destination.aspx will be;



protected void Page_Load(object sender, EventArgs e)
{
if (this.Page.PreviousPage != null)
{
Control ContentPlaceHolder1 = this.Page.PreviousPage.Master.FindControl("ContentPlaceHolder1");
Repeater rptSalary = (Repeater)ContentPlaceHolder1.FindControl("rptSalary");
Panel Panel1 = new Panel();
Panel1.Controls.Add(rptSalary);
Page.Controls.Add(Panel1);
Response.Write("<script>window.print();</script>");
}
}


Read More »

Tuesday, 8 December 2015

How to open new popup form on gridview button click ?

0


Step 1:

We will use ajaxtoolkit to open a new form on gridview button click or on normal button click.
You have to add reference to AjaxToolKit.dll, which you can found in the package of ajaxtoolkit from ajaxcontroltoolkit.codeplex.com.

Step 2:
You can also add Ajax tools into the asp.net toolbox, and then you can directly use its control from there, or you can hard-code.
 First, Register the DLL if you are preferring hard-code.

 <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>


on design.aspx

<style type="text/css">
.tableBackground
{
background-color:silver;
opacity:0.7;
}
</style>



<asp:ToolkitScriptManager ID="ScriptManager1" runat="server"></asp:ToolkitScriptManager>

<asp:GridView ID="grdView" runat="server" AutoGenerateColumns="False"
onrowcommand="grdView_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Emp Id">
<ItemTemplate>
<asp:Label ID="lblEmpId" runat="server" Text='<%#Eval("empId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Generate Salary">
<ItemTemplate>
<asp:LinkButton ID="btnGenerateSalary" runat="server" Text="Generate Salary" CssClass="btn btn-info" CommandArgument='<%#Eval("empId") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<asp:Button ID="modelPopup" runat="server" style="display:none" />
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="modelPopup" PopupControlID="updatePanel"
CancelControlID="btnCancel" BackgroundCssClass="tableBackground">
</asp:ModalPopupExtender>
<asp:Panel ID="updatePanel" runat="server" BackColor="White" style="display:none">

<div>
<span >Gross Salary</span>
<asp:Label ID="lblGrossSalary" runat="server"></asp:Label>
</div>
<asp:Button ID="btnCancel" runat="server" Text="Close" />

</asp:Panel>

The modalPopExtender will open the panel as a popup form :



on.cs


protected void grdView_RowCommand(object sender, GridViewCommandEventArgs e)
{
string empId = e.CommandArgument.ToString();
this.ModalPopupExtender1.Show();
}

//You can do any implementation here with the help of the empId, or any value that you will pass as commandargument.



Read More »