Translate

Home > May 2012

May 2012

ASP.NET GridView Row Delete Confirmation

Tuesday, May 29, 2012 Category : 0

 <asp:TemplateField>
                <ItemTemplate>
                <asp:LinkButton Runat="server" CommandName="Edit">Edit</asp:LinkButton>
           <asp:LinkButton Runat="server" CausesValidation="False"
               OnClientClick="return confirm('Are you sure you want to delete this distribution?');"
               CommandName="Delete">Delete</asp:LinkButton>
       </ItemTemplate>
       <EditItemTemplate>  
           <asp:LinkButton CommandName="Update" Text="Update"    
               runat="server"/>    
           <asp:LinkButton CommandName="Cancel" Text="Cancel"
               runat="server"/>          
       </EditItemTemplate>
 </asp:TemplateField>

ASP.NET GridView with Combobox ObjectDataSource EasyUpdate

Sunday, May 20, 2012 Category : 0



BLL CLass For Data Insert

namespace SampleASP.Model
{
    [DataObject]
    public class Client
    {

        public string ID { get; set; }

        public string UserName { get; set; }

        public string Address { get; set; }
        public string AdminUserID { get; set; }
        public string AdminUserName { get; set; }
       
        private Result obResult = new Result();
        private DALS obDAL = new DALS();

        [DataObjectMethod(DataObjectMethodType.Insert)]
        public Result Save(Client obClients)
        {
            string query = @"INSERT INTO Client([id],[name],[address],[userID])   VALUES   (" + obClients.ID + ",'" + obClients.UserName + "', '" + obClients.Address + "','" + obClients.AdminUserID + "')";
            obResult = obDAL.Create(query);
            return obResult;       
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public List<Client> SelectAll()
        {
            List<Client> obCLientList = new List<Client>();
            string query = "Select * from Client";
            obResult = obDAL.Select(query);
            DataTable dt = obResult.Data as DataTable;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Client obClient = new Client();
                obClient.ID = dt.Rows[i]["id"].ToString();
                obClient.UserName = dt.Rows[i]["name"].ToString();
                obClient.Address = dt.Rows[i]["address"].ToString();
                obClient.AdminUserID = dt.Rows[i]["userID"].ToString();
                obCLientList.Add(obClient);

            }
            return obCLientList;
        }

        [DataObjectMethod(DataObjectMethodType.Update)]
        public int Update(Client obClients)
        {
            string query = "UPDATE Client set name='" + obClients.UserName + "', address='" + obClients.Address + "',userID='" + obClients.AdminUserID + "' where id=" + obClients.ID;
            obResult = obDAL.Update(query);
            if (obResult.ResultState)
            {
                return 1;
            }
            return 0;
        }

        [DataObjectMethod(DataObjectMethodType.Delete)]
        public int Delete(Client obClients)
        {
            string query = "DELETE FROM Client where id="+ obClients.ID;
            obResult = obDAL.Update(query);
            if (obResult.ResultState)
            {
                return 1;
            }
            return 0;
        }

    }

ASPX GRID CODE

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
        AutoGenerateColumns="False" DataSourceID="ObjectDataSource2"
        DataKeyNames="id" onrowdatabound="GridView1_RowDataBound"
        onrowupdating="GridView1_RowUpdating">
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" ReadOnly="true" />
            <asp:BoundField DataField="UserName" HeaderText="UserName"
                SortExpression="UserName" />
            <asp:BoundField DataField="Address" HeaderText="Address"
                SortExpression="Address" />
           
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server"
                        DataSourceID="ObjectDataSource1" DataTextField="UserName" DataValueField="AdminUserID" SelectedValue='<%# Eval("AdminUserID") %>' >
                    </asp:DropDownList>
                    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
                        OldValuesParameterFormatString="original_{0}" SelectMethod="SelectAll"
                        TypeName="SampleASP.Model.Users"></asp:ObjectDataSource>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:ObjectDataSource ID="ObjectDataSource2" runat="server"
        DataObjectTypeName="SampleASP.Model.Client" DeleteMethod="Delete"
        OldValuesParameterFormatString="original_{0}" SelectMethod="SelectAll"
        TypeName="SampleASP.Model.Client" UpdateMethod="Update"
        onupdated="ObjectDataSource2_Updated"
        onupdating="ObjectDataSource2_Updating">
        <UpdateParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="AdminUserID" Type="String" />
        </UpdateParameters>
    </asp:ObjectDataSource>


CodeBehid File Code

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            DropDownList ddl = GridView1.Rows[e.RowIndex].FindControl("DropDownList1") as DropDownList;
            e.NewValues["AdminUserID"] = ddl.SelectedValue.ToString();
        } 


 

Powered by Blogger.