Click here to Skip to main content
15,881,833 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
XML
<asp:GridView ID="griddistrict" Width="95%" runat="server" AutoGenerateColumns="False"
    AlternatingRowStyle-BackColor="#CCE6FF" GridLines="both" CellPadding="5" CellSpacing="1"
    RowStyle-HorizontalAlign="Center" ShowHeader="true" DataKeyNames="nid" ShowFooter="true"
    OnRowCommand="griddistrict_RowCommand" OnSelectedIndexChanged="griddistrict_SelectedIndexChanged"
    OnRowDeleting="griddistrict_RowDeleting1" AllowPaging="True" PageSize="10"
    OnPageIndexChanging="griddistrict_PageIndexChanging"
    onrowediting="griddistrict_RowEditing">
    <Columns>
        <asp:TemplateField HeaderText="Select" Visible="false">
            <ItemTemplate>
                <asp:Label ID="lblnid" Text='<%#  bind("nid")%>' runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Select">
            <HeaderTemplate>
                <asp:CheckBox ID="chkSelectAll" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelectAll_CheckedChanged" />
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="chkSelect" runat="server"></asp:CheckBox>
            </ItemTemplate>
            <FooterTemplate>
                <asp:LinkButton ID="lnkdelete" runat="server" OnClick="lnkselect_Click" OnClientClick="return confirm('Are you sure! You want to Delete Selected?')"> Delete
                </asp:LinkButton>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="SNo.">
            <ItemTemplate>
                <%#Container.DataItemIndex+1 %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="CSC Name">
            <ItemTemplate>
                <asp:Label ID="lblcscname" runat="server" Text='<%# Eval("cscname") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="CSC Code">
            <ItemTemplate>
                <asp:Label ID="lblcsccode" runat="server" Text='<%# Eval("csccode") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="CSC Address">
            <ItemTemplate>
                <asp:Label ID="lblcscaddress" runat="server" Text='<%# Eval("address") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Contact Details">
            <ItemTemplate>
                <asp:Label ID="lblcontact" runat="server" Text='<%# Eval("primarycontactno")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Approval Status">
            <ItemTemplate>
                <asp:Label runat="server" ID="lnkapprove" Text=' <%# Bind("approvalstatus") %>' />
            </ItemTemplate>
            <FooterTemplate>
                <asp:LinkButton ID="lnkchangestatus" runat="server" OnClientClick="return confirm('Are you sure! You want to Approve Or Not?')"
                    OnClick="lnkchangestatus_Click"> Approval Status
                </asp:LinkButton>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Enable Status">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblenable" Text=' <%# Bind("enablestatus") %>' />
            </ItemTemplate>
            <FooterTemplate>
                <asp:LinkButton ID="lnkenable" runat="server" OnClientClick="return confirm('Are you sure! You want to Change Status?')"
                    OnClick="lnkenable_Click">  Enable/Disable
                </asp:LinkButton>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Creation Date" Visible="false">
            <ItemTemplate>
                <asp:Label runat="server" ID="createddate" Text=' <%# Bind("creationdate", "{0:dd MMM,yy}") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="false">
            <ItemTemplate>
                <asp:Button runat="server" ID="btnview" Text="View" OnClientClick="return confirm('Are you sure! You want to View Record?')"
                    CommandName="viewrecord" CommandArgument='<%#bind("nid") %>' CssClass="btnstyle" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Action">
            <ItemTemplate>
                <asp:Button runat="server" ID="btnedit" Text="Edit" OnClientClick="return confirm('Are you sure! You want to Edit?')"
                    CommandName="Editrow" CommandArgument='<%#bind("nid") %>' CssClass="btnstyle" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="false">
            <ItemTemplate>
                <asp:Button runat="server" ID="Delete" Text="Delete" OnClientClick="return confirm('Are you sure! You want to Delete?')"
                    CommandName="Delete" CssClass="btnstyle" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <EmptyDataRowStyle BackColor="#888543" ForeColor="White" HorizontalAlign="Center" />
    <HeaderStyle BackColor="#0972A2" ForeColor="#FAFCFC" />
    <RowStyle Font-Size="8pt" Font-Names="Verdana" HorizontalAlign="Center" />
    <FooterStyle HorizontalAlign="Center" />
</asp:GridView>



aspx.cs Page

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class manage_cscinformation : System.Web.UI.Page
{
    DataAccess objda = new DataAccess();
    DataSet ds = new DataSet();
    DataSet ds1 = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            griddistrict.Visible = true;
            bindgroupcompany();
            bindstate();
            bindgrid();
            views.ActiveViewIndex = 2;
            ListItem item = new ListItem("--Select--", "select");
            ddldistrict.Items.Insert(0, item);
            ddldistrict.Enabled = false;
            if (Request.QueryString["branch"] == null)
            {
                Response.Redirect("branchhome.aspx");
            }
            else if (Request.QueryString["branch"].ToString() == "Add")
            {
                views.ActiveViewIndex = 0;

            }
            else
            {
                views.ActiveViewIndex = 2;
            }

        }

    }

    public void bindgroupcompany()
    {

        ds = objda.getallgroupcompany();

        if (ds.Tables[0].Rows.Count > 0)
        {
            ddlinsure.DataSource = ds;
            ddlinsure.DataValueField = "groupcompanyname";
            ddlinsure.DataTextField = "groupcompanyname";
            ddlinsure.DataBind();
            ListItem item = new ListItem("-Select Group Company-", "select");
            ddlinsure.Items.Insert(0, item);
            ddlgcompany.DataSource = ds;
            ddlgcompany.DataValueField = "groupcompanyname";
            ddlgcompany.DataTextField = "groupcompanyname";
            ddlgcompany.DataBind();
            ListItem item1 = new ListItem("-Select Group Company-", "select");
            ddlgcompany.Items.Insert(0, item1);

        }
    }

    public void bindstate()
    {
        objda.status = "";
        ds = objda.getallstate();
        if (ds.Tables[0].Rows.Count > 0)
        {
            ddlstate.DataSource = ds;
            ddlstate.DataValueField = "nid";
            ddlstate.DataTextField = "statename";
            ddlstate.DataBind();
            ddlstate.SelectedIndex = 0;
        }
    }

    public void binddistrict()
    {

        ddldistrict.Enabled = true;
        objda.Stateid = Convert.ToInt32(ddlstate.SelectedValue.ToString());
        objda.status = "enabled";
        ds1 = objda.getenableddist();
        if (ds1.Tables[0].Rows.Count > 0)
        {
            ddldistrict.DataSource = ds1;
            ddldistrict.DataValueField = "nid";
            ddldistrict.DataTextField = "distname";
            ddldistrict.DataBind();
            ddldistrict.SelectedIndex = 0;
        }
    }



    public void bindgrid()
    {
        objda.status = "All";
        ds = objda.getcscdetail();
        if (ds.Tables[0].Rows.Count > 0)
        {
            griddistrict.Visible = true;
            griddistrict.DataSource = ds;
            griddistrict.DataBind();
            divgrid.InnerText = "";
        }
        else
        {
            griddistrict.Visible = false;
            divgrid.InnerText = "Record Not Found";
        }
    }

    protected void griddistrict_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "InsertNew")
        {
            griddistrict.Visible = false;
            blank();
            views.ActiveViewIndex = 0;
        }
        else if (e.CommandName == "Editrow")
        {
            int nid = Convert.ToInt32(e.CommandArgument.ToString());
            filldata(nid);
            try
            {
                string strHostName = System.Net.Dns.GetHostName();
                string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

                objda.Ipaddress = clientIPAddress;
                objda.loginid = Session["username"].ToString();
                objda.description = "Upadte The Record Of nid  " + nid + "  of CSC Information Table";
                objda.insertlog();
            }
            catch
            {
            }
            views.ActiveViewIndex = 0;
        }
        else if (e.CommandName == "viewrecord")
        {
            int nid = Convert.ToInt32(e.CommandArgument.ToString());
            viewdata(nid);

            try
            {
                string strHostName = System.Net.Dns.GetHostName();
                string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

                objda.Ipaddress = clientIPAddress;
                objda.loginid = Session["username"].ToString();
                objda.description = "View The Record Of Nid  " + nid + " of CSC Information Table";
                objda.insertlog();
            }
            catch
            {
            }
            views.ActiveViewIndex = 1;
        }
        else if (e.CommandName == "Delete")
        {

        }
    }

    public void viewdata(int nid)
    {
        objda.nid = nid;
        ds = objda.getcscdetailbynid();
        if (ds.Tables[0].Rows.Count > 0)
        {

            lblcscname.Text = ds.Tables[0].Rows[0]["cscname"].ToString();
            lblemail.Text = ds.Tables[0].Rows[0]["email"].ToString();
            lblcountry.Text = ds.Tables[0].Rows[0]["country"].ToString();
            lbladdress.Text = ds.Tables[0].Rows[0]["address"].ToString();
            lblstate.Text = ds.Tables[0].Rows[0]["state"].ToString();
            lbldistrict.Text = ds.Tables[0].Rows[0]["district"].ToString();
            lblzipcode.Text = ds.Tables[0].Rows[0]["zipcode"].ToString();
            lblfaxno.Text = ds.Tables[0].Rows[0]["faxno"].ToString();
            lblfno.Text = ds.Tables[0].Rows[0]["primarycontactno"].ToString();
            lblsno.Text = ds.Tables[0].Rows[0]["secondrycontact"].ToString();
            lblremark.Text = ds.Tables[0].Rows[0]["remark"].ToString();


        }
    }
    public void filldata(int nid)
    {
        objda.nid = nid;
        ds = objda.getcscdetailbynid();
        if (ds.Tables[0].Rows.Count > 0)
        {
            ddlinsure.SelectedItem.Text = ds.Tables[0].Rows[0]["companyname"].ToString();
            txtcscname.Value = ds.Tables[0].Rows[0]["cscname"].ToString();
            txtemailid.Value = ds.Tables[0].Rows[0]["email"].ToString();
            txtcountry.Value = ds.Tables[0].Rows[0]["country"].ToString();
            txtaddess.Value = ds.Tables[0].Rows[0]["address"].ToString();
            ddlstate.SelectedItem.Text = ds.Tables[0].Rows[0]["state"].ToString();
            binddistrict();
            ddldistrict.SelectedItem.Text = ds.Tables[0].Rows[0]["district"].ToString();
            txtzip.Value = ds.Tables[0].Rows[0]["zipcode"].ToString();
            txtfaxno.Value = ds.Tables[0].Rows[0]["faxno"].ToString();
            txtfno.Value = ds.Tables[0].Rows[0]["primarycontactno"].ToString();
            txtsno.Value = ds.Tables[0].Rows[0]["secondrycontact"].ToString();
            txtremark.Text = ds.Tables[0].Rows[0]["remark"].ToString();
            ViewState["edit"] = nid;

        }
    }
    protected void griddistrict_SelectedIndexChanged(object sender, EventArgs e)
    {

    }




    protected void griddistrict_RowDeleting1(object sender, GridViewDeleteEventArgs e)
    {
        objda.nid = Convert.ToInt32(griddistrict.DataKeys[e.RowIndex].Value.ToString());

        objda.deletecscdetail();
        try
        {
            string strHostName = System.Net.Dns.GetHostName();
            string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

            objda.Ipaddress = clientIPAddress;
            objda.loginid = Session["username"].ToString();
            objda.description = "Delete The Record Of Nid  " + Convert.ToInt32(griddistrict.DataKeys[e.RowIndex].Value.ToString()) + " of CSC Information Table";
            objda.insertlog();
        }
        catch
        {
        }
        bindgrid();


    }
    protected void griddistrict_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        griddistrict.PageIndex = e.NewPageIndex;
        bindgrid();



    }
    protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkAll = (CheckBox)griddistrict.HeaderRow.FindControl("chkSelectAll");
        if (chkAll.Checked == true)
        {
            foreach (GridViewRow gvRow in griddistrict.Rows)
            {
                CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
                chkSel.Checked = true;

            }
        }
        else
        {
            foreach (GridViewRow gvRow in griddistrict.Rows)
            {
                CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
                chkSel.Checked = false;

            }
        }

    }


    protected void lnkselect_Click(object sender, EventArgs e)
    {
        int flag = 0;

        foreach (GridViewRow gvRow in griddistrict.Rows)
        {
            CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
            if (chkSel.Checked == true)
            {
                flag = flag + 1;
                objda.nid = Convert.ToInt32(griddistrict.DataKeys[gvRow.RowIndex].Value);
                objda.deletecscdetail();
                try
                {
                    string strHostName = System.Net.Dns.GetHostName();
                    string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

                    objda.Ipaddress = clientIPAddress;
                    objda.loginid = Session["username"].ToString();
                    objda.description = "Delete The Record Of Nid  " + Convert.ToInt32(griddistrict.DataKeys[gvRow.RowIndex].Value) + " of CSC Information Table";
                    objda.insertlog();
                }
                catch
                {
                }
            }


        }
        if (flag == 0)
        {
            string msg = @"<script language='javascript' type='text/javascript'>alert('Please Select row !')</script>";
            Page.RegisterStartupScript("msg", msg);
            return;
        }
        bindgrid();


    }

    protected void lnkchangestatus_Click(object sender, EventArgs e)
    {
        int flag = 0;
        foreach (GridViewRow gvRow in griddistrict.Rows)
        {
            CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
            if (chkSel.Checked == true)
            {
                flag = flag + 1;
                objda.nid = Convert.ToInt32(griddistrict.DataKeys[gvRow.RowIndex].Value);
                objda.Approvalby = Session["username"].ToString();
                objda.Approvalstatus = ((Label)(griddistrict.Rows[gvRow.RowIndex].FindControl("lnkapprove"))).Text;
                objda.cscchangeapproval();
                try
                {
                    string strHostName = System.Net.Dns.GetHostName();
                    string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

                    objda.Ipaddress = clientIPAddress;
                    objda.loginid = Session["username"].ToString();
                    if (((Label)(griddistrict.Rows[gvRow.RowIndex].FindControl("lnkapprove"))).Text == "approved")
                    {
                        objda.description = "Change Approval from approved to  pending Of Nid  " + Convert.ToInt32(griddistrict.DataKeys[gvRow.RowIndex].Value) + " of CSC Information Table";
                    }
                    else
                    {
                        objda.description = "Change Approval from pending to  approved Of Nid   " + Convert.ToInt32(griddistrict.DataKeys[gvRow.RowIndex].Value) + " of CSC Information Table";
                    }
                    objda.insertlog();
                }
                catch
                {
                }

            }

        }
        if (flag == 0)
        {
            string msg = @"<script language='javascript' type='text/javascript'>alert('Please Select row !')</script>";
            Page.RegisterStartupScript("msg", msg);
            return;
        }

        bindgrid();
    }
    protected void lnkenable_Click(object sender, EventArgs e)
    {
        int flag = 0;
        foreach (GridViewRow gvRow in griddistrict.Rows)
        {
            CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
            if (chkSel.Checked == true)
            {
                flag = flag + 1;
                objda.nid = Convert.ToInt32(griddistrict.DataKeys[gvRow.RowIndex].Value);
                objda.Enableby = Session["username"].ToString();
                objda.status = ((Label)(griddistrict.Rows[gvRow.RowIndex].FindControl("lblenable"))).Text;
                objda.Enablecscstatus();
                try
                {
                    string strHostName = System.Net.Dns.GetHostName();
                    string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
                    objda.Ipaddress = clientIPAddress;
                    objda.loginid = Session["username"].ToString();
                    if (((Label)(griddistrict.Rows[gvRow.RowIndex].FindControl("lnkapprove"))).Text == "enabled")
                    {
                        objda.description = "Change Approval from enabled to  disabled Of Nid  " + Convert.ToInt32(griddistrict.DataKeys[gvRow.RowIndex].Value) + " of CSC Information Table";
                    }
                    else
                    {
                        objda.description = "Change Approval from disabled to  enabled Of Nid   " + Convert.ToInt32(griddistrict.DataKeys[gvRow.RowIndex].Value) + " of CSC Information Table";
                    }
                    objda.insertlog();
                }
                catch
                {
                }
            }


        }
        if (flag == 0)
        {
            string msg = @"<script language='javascript' type='text/javascript'>alert('Please Select row !')</script>";
            Page.RegisterStartupScript("msg", msg);
            return;
        }
        bindgrid();


    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        objda.Cscname = txtcscname.Value;
        objda.name = txtcscname.Value;
        objda.type = "csc";
        ds = objda.getdatabyname();
        objda.emailid = txtemailid.Value;
        objda.country = txtcountry.Value;
        objda.address = txtaddess.Value;
        objda.name = ddlinsure.SelectedValue.ToString();
        objda.state = ddlstate.SelectedItem.ToString();
        objda.district = ddldistrict.SelectedItem.ToString();
        objda.zip = txtzip.Value;
        objda.fax = txtfaxno.Value;
        objda.mobile = txtfno.Value;
        objda.mobileno = txtsno.Value;
        objda.Remark = txtremark.Text;
        objda.Createdby = Session["username"].ToString();
        if (ViewState["edit"] != null)
        {
            objda.nid = Convert.ToInt32(ViewState["edit"].ToString());
            objda.updatecscdetail();
            try
            {
                string strHostName = System.Net.Dns.GetHostName();
                string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

                objda.Ipaddress = clientIPAddress;
                objda.loginid = Session["username"].ToString();
                objda.description = "Upadte The Record Of nid  " + Convert.ToInt32(ViewState["edit"].ToString()) + "  of CSC Information Table";
                objda.insertlog();
            }
            catch
            {
            }
            string msg = @"<script language='javascript' type='text/javascript'>alert('Record is Updated !')</script>";
            Page.RegisterStartupScript("msg", msg);
            bindgrid();
            views.ActiveViewIndex = 2;

        }
        else
        {
            objda.insertcscdetails();
            try
            {
                string strHostName = System.Net.Dns.GetHostName();
                string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

                objda.Ipaddress = clientIPAddress;
                objda.loginid = Session["username"].ToString();
                objda.description = "Insert The Record into CSC Information Table";
                objda.insertlog();
            }
            catch
            {
            }
            blank();
            string msg = @"<script language='javascript' type='text/javascript'>alert('Record is Inserted !')</script>";
            Page.RegisterStartupScript("msg", msg);
            views.ActiveViewIndex = 0;
            griddistrict.Visible = true;
        }
        bindgrid();

    }
    protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
    {
        binddistrict();
    }

    public void blank()
    {

        txtcscname.Value = "";
        txtemailid.Value = "";
        txtcountry.Value = "";
        txtaddess.Value = "";
        ddlstate.SelectedIndex = -1;
        ddldistrict.Enabled = false;
        txtzip.Value = "";
        txtfaxno.Value = "";
        txtfno.Value = "";
        txtsno.Value = "";
        txtremark.Text = "";
    }
    protected void btnback_Click(object sender, EventArgs e)
    {
        views.SetActiveView(view3);
        griddistrict.Visible = true;

    }
    protected void ddlgcompany_SelectedIndexChanged(object sender, EventArgs e)
    {
        objda.company = ddlgcompany.SelectedItem.Text;
        ds = objda.getcsccodebycompany();
        if (ds.Tables[0].Rows.Count > 0)
        {
            griddistrict.Visible = true;
            griddistrict.DataSource = ds.Tables[0];
            griddistrict.DataBind();
            divgrid.InnerText = "";
        }
        else
        {
            griddistrict.Visible = false;
            divgrid.InnerText = "Record not Found";
            views.ActiveViewIndex = 2;
        }
    }
    protected void btnsearch_Click(object sender, EventArgs e)
    {
        objda.company = ddlgcompany.SelectedItem.Text;
        objda.branch = txtbranch.Value;
        ds = objda.getcsccodebycompanyandbranch();
        if (ds.Tables[0].Rows.Count > 0)
        {
            griddistrict.Visible = true;
            griddistrict.DataSource = ds.Tables[0];
            griddistrict.DataBind();
            divgrid.InnerText = "";
        }
        else
        {
            griddistrict.Visible = false;
            divgrid.InnerText = "Record not Found";
            views.ActiveViewIndex = 2;
        }
    }
    protected void griddistrict_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        views.SetActiveView(view3);
        griddistrict.Visible = true;
    }
}



i cannot change my active edit or view from grid view row command
Posted
Comments
Sandeep Mewara 4-Dec-10 9:47am    
Code dump. No one is going to go through it. Please paste relevant code part and be specific.
GenJerDan 6-Dec-10 15:53pm    
Not putting this in as an answer, but I've had better success doing something like this:
views.SetActiveView(TheViewIWant);
rather than using the index.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900