Yes,Gridview is Container control, Contain control can hold child controls like Button, DropDownlist as child
controls, these controls are postback controls, to handle these events by Parent controls we have to use
command events that is RowCommand of Gridview.
like similarly DataList,DetailsView----------Itemcommand Event
Example Demo: Gridview RowCommand event
Source View
<asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button2" runat="server"
CommandArgument='<%#Eval("Empid")%>' CommandName="edt"
Text="Edit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Codebehind View
class declaration
SqlConnection con = new SqlConnection("uid=sa;pwd=zolt123$;database=PropercodeDB");
DataSet ds,ds1;
SqlDataAdapter da,da1;
static int empid;
SqlParameter[] epars = new SqlParameter[8];
SqlCommand empcmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
EmployeeDetails();
}
public void EmployeeDetails()
{
da = new SqlDataAdapter("select * from EmployeeDetails", con);
ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "edt")
{
empid =Convert.ToInt32 (e.CommandArgument.ToString());
da = new SqlDataAdapter("select * from EmployeeDetails where EmpId=" + empid, con);
ds = new DataSet();
da.Fill(ds);
txtEname.Text = ds.Tables[0].Rows[0].ItemArray[0].ToString();
//txtEname.Text = ds.Tables[0].Rows[0]["EmpName"].ToString();
ddlDesignations.SelectedValue = ds.Tables[0].Rows[0]["EmpDesignation"].ToString();
txtSalaries.Text = ds.Tables[0].Rows[0]["Empsal"].ToString();
txtProdedFund.Text = ds.Tables[0].Rows[0]["EmpProFund"].ToString();
txtAddress.Text = ds.Tables[0].Rows[0]["EmpAddress"].ToString();
txtDateofJoin.Text = ds.Tables[0].Rows[0]["EmpJoindate"].ToString();
CheckBoxList1.SelectedValue = ds.Tables[0].Rows[0]["Status"].ToString();
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
da = new SqlDataAdapter("update_Emp", con);
epars[0] = new SqlParameter("@ename", txtEname.Text);
epars[1] = new SqlParameter("@designation", ddlDesignations.SelectedValue);
epars[2] = new SqlParameter("@esal", txtSalaries.Text);
epars[3] = new SqlParameter("@epf", txtProdedFund.Text);
epars[4] = new SqlParameter("@eadd", txtAddress.Text);
epars[5] = new SqlParameter("@edoj", txtDateofJoin.Text);
epars[6] = new SqlParameter("@estatus", CheckBoxList1.SelectedValue);
epars[7] = new SqlParameter("@eid", empid.ToString());
empcmd = new SqlCommand();
IEnumerator ier = epars.GetEnumerator();
while (ier.MoveNext())
{
SqlParameter item = (SqlParameter)ier.Current;
empcmd.Parameters.Add(item);
}
da = new SqlDataAdapter(empcmd);
ds = new DataSet();
da.Fill(ds);
Label1.Text = "update Succefuly....";
}
Note : Don't Perform CRUD Operations using RowCommand,ItemCommand(DataList,Detailsview) events.
these(Command events) are used to handle child controls(Postback controls) events by Container Controls.
controls, these controls are postback controls, to handle these events by Parent controls we have to use
command events that is RowCommand of Gridview.
like similarly DataList,DetailsView----------Itemcommand Event
Example Demo: Gridview RowCommand event
Source View
<asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button2" runat="server"
CommandArgument='<%#Eval("Empid")%>' CommandName="edt"
Text="Edit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Codebehind View
class declaration
SqlConnection con = new SqlConnection("uid=sa;pwd=zolt123$;database=PropercodeDB");
DataSet ds,ds1;
SqlDataAdapter da,da1;
static int empid;
SqlParameter[] epars = new SqlParameter[8];
SqlCommand empcmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
EmployeeDetails();
}
public void EmployeeDetails()
{
da = new SqlDataAdapter("select * from EmployeeDetails", con);
ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "edt")
{
empid =Convert.ToInt32 (e.CommandArgument.ToString());
da = new SqlDataAdapter("select * from EmployeeDetails where EmpId=" + empid, con);
ds = new DataSet();
da.Fill(ds);
txtEname.Text = ds.Tables[0].Rows[0].ItemArray[0].ToString();
//txtEname.Text = ds.Tables[0].Rows[0]["EmpName"].ToString();
ddlDesignations.SelectedValue = ds.Tables[0].Rows[0]["EmpDesignation"].ToString();
txtSalaries.Text = ds.Tables[0].Rows[0]["Empsal"].ToString();
txtProdedFund.Text = ds.Tables[0].Rows[0]["EmpProFund"].ToString();
txtAddress.Text = ds.Tables[0].Rows[0]["EmpAddress"].ToString();
txtDateofJoin.Text = ds.Tables[0].Rows[0]["EmpJoindate"].ToString();
CheckBoxList1.SelectedValue = ds.Tables[0].Rows[0]["Status"].ToString();
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
da = new SqlDataAdapter("update_Emp", con);
epars[0] = new SqlParameter("@ename", txtEname.Text);
epars[1] = new SqlParameter("@designation", ddlDesignations.SelectedValue);
epars[2] = new SqlParameter("@esal", txtSalaries.Text);
epars[3] = new SqlParameter("@epf", txtProdedFund.Text);
epars[4] = new SqlParameter("@eadd", txtAddress.Text);
epars[5] = new SqlParameter("@edoj", txtDateofJoin.Text);
epars[6] = new SqlParameter("@estatus", CheckBoxList1.SelectedValue);
epars[7] = new SqlParameter("@eid", empid.ToString());
empcmd = new SqlCommand();
IEnumerator ier = epars.GetEnumerator();
while (ier.MoveNext())
{
SqlParameter item = (SqlParameter)ier.Current;
empcmd.Parameters.Add(item);
}
da = new SqlDataAdapter(empcmd);
ds = new DataSet();
da.Fill(ds);
Label1.Text = "update Succefuly....";
}
Note : Don't Perform CRUD Operations using RowCommand,ItemCommand(DataList,Detailsview) events.
these(Command events) are used to handle child controls(Postback controls) events by Container Controls.
No comments:
Post a Comment