Gautam Buddha Temple Varanasi,Varanasi - Buddhist Pilgrimage

Saturday, August 28, 2010

GridView Edit:on clicking edit a new page should be open where user can update data

 GridView Edit:on clicking edit a new page should be open where user can update data  
 On clicking edit in GridView a new page should be open where user can update data. In this article i am explaining how to update record using GridView.When User Click edit button in GridView new page will be open where user can update record.in second page i have just find the data not wrriten the code for update.  
 Here also i have written code for delete record in GridView and if GridView has no record show message no record found.  
 First Page code  
 public void GridViewBind()  
 {  
      using (SqlConnection conn = new SqlConnection(conStr))  
      {  
           DataSet ds = SqlHelper.ExecuteDataset(conn, "LeaveMaster_Select");  
           GridView1.DataSource = ds.Tables[0];  
           GridView1.DataBind();  
      }  
 }  
 protected void ImgDelete_Click(object sender, ImageClickEventArgs e)  
 {  
      ImageButton img = (ImageButton)sender;  
      Label Leave = img.FindControl("Label1") as Label;  
      using (SqlConnection conn = new SqlConnection(conStr))  
      {  
           conn.Open();  
           using (SqlTransaction t = conn.BeginTransaction())  
           {  
                try  
                {  
                     exceptionMessage.Text = SqlHelper.ExecuteScalar(t, "LeaveMaster_Delete", Leave.Text, "", 'D').ToString();  
                     t.Commit();   
                     GridViewBind();  
                }  
                catch  
                {  
                     t.Rollback();  
                }  
           }  
           conn.Close();  
      }  
 }   
 protected void ImgbtnDetails_Click(object sender, ImageClickEventArgs e)  
 {  
      //Selecting a Record from Grid For Updation..  
      ImageButton aspSender = sender as ImageButton;  
      Label lblId = aspSender.FindControl("lblLeaveID") as Label;  
      Response.Redirect("~/LeaveDetails.aspx?Id=" + lblId.Text + "");   
 }  
 ===========  
 2nd page  
 if (Request.QueryString["Id"] != null)  
 {  
      Id = Convert.ToInt32(Request.QueryString["Id"].ToString());  
      string str = "SELECT LeaveCode, LeaveDesc, LeaveId FROM LeaveMaster WHERE (LeaveId = "+Id+")";  
      DataTable dt1 = SqlHelper.ExecuteDataset(conStr, CommandType.Text, str).Tables[0];  
      txtCode.Text = dt1.Rows[0]["LeaveCode"].ToString();  
      txtCode.Enabled = false;  
      txtShortName.Text = dt1.Rows[0]["LeaveDesc"].ToString();  
      txtShortName.Enabled = false;   
 }  
 aspx Page  
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="LeaveId" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"   
 Width="700px" PageSize="10" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" >  
      <Columns>  
           <asp:TemplateField HeaderText="LeaveId" InsertVisible="False" SortExpression="LeaveId" Visible="False">  
                <EditItemTemplate>  
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("LeaveId") %>'></asp:Label>  
                </EditItemTemplate>  
                <ItemTemplate>  
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("LeaveId") %>'></asp:Label>  
                </ItemTemplate>  
           </asp:TemplateField>  
           <asp:BoundField DataField="LeaveCode" HeaderText="Leave Code" SortExpression="LeaveCode" />  
           <asp:BoundField DataField="LeaveShortDesc" HeaderText="Short Desc" SortExpression="LeaveShortDesc" />  
           <asp:BoundField DataField="LeaveLongDesc" HeaderText="Long Desc" SortExpression="LeaveLongDesc" />  
           <asp:CommandField ButtonType="Image" HeaderText="Edit" SelectImageUrl="~/Image/img_edit.png" ShowSelectButton="True">  
           <ItemStyle HorizontalAlign="Center" />  
           </asp:CommandField>  
           <asp:TemplateField HeaderText="Details">  
                <EditItemTemplate>  
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
                </EditItemTemplate>  
                <ItemStyle HorizontalAlign="Center" Width="40px" />  
                <ItemTemplate>  
                <asp:ImageButton ID="ImgbtnDetails" runat="server"   
                OnClick="ImgbtnDetails_Click" />  
                </ItemTemplate>  
           </asp:TemplateField>  
           <asp:TemplateField HeaderText="Delete">  
                <ItemTemplate>  
                <asp:ImageButton ID="imgbtnDelete" runat="server"   
                OnClick="ImgDelete_Click" OnClientClick="return ConfirmDelete();" />  
                </ItemTemplate>  
                <ItemStyle HorizontalAlign="Center" Width="50px" />  
           </asp:TemplateField>  
      </Columns>  
      <EmptyDataTemplate>No record found !</EmptyDataTemplate>  
 </asp:GridView>  

No comments:

Post a Comment