1

我有一个gridview,并且有一个超链接列,上面写着更新。单击该页面后,将重定向到另一个页面,并且特定的行值将显示在另一个网格视图中。并且用户需要编辑 1 列并使其更新,然后在 gridview 之外有一个发送或接受按钮,单击它时应该生成邮件,它应该采用更新的网格值并将其发送给他的其他用户和页面应该被重定向到上一页。

gridview的aspx代码

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
                     AutoGenerateColumns="false" BackColor="White" BorderColor="#0061C1" 
                     BorderStyle="None" CaptionAlign="Bottom" EmptyDataText="No Records Found" 
                     Font-Names="Verdana" Font-Size="X-Small" ForeColor="#0061C1" Height="70px" 
                     ShowFooter="True" ShowHeaderWhenEmpty="True" OnRowCommand="GridView1_RowCommand" 
                     OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing"
                     onselectedindexchanged="GridView1_SelectedIndexChanged" OnRowUpdating="GridView1_RowUpdating"
                     Width="796px">
                     <Columns>
                         <asp:BoundField DataField="LeaveID" Visible="false">
                         <FooterStyle BackColor="#0061C1" />
                         <HeaderStyle BackColor="#0061C1" ForeColor="White" HorizontalAlign="Center" 
                             VerticalAlign="Middle" />
                         <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="10px" />
                         </asp:BoundField>
                         <asp:TemplateField HeaderText="Applied By">
                         <ItemTemplate>
                         <asp:Label 
                            ID="LoggedInUser" runat="server" Font-Names="Verdana" Font-Size="X-Small" 
                            Height="24px" Text='<%# Eval("LoggedInUser")%>' Width="100px"></asp:Label>
                    </ItemTemplate>
                    <FooterStyle BackColor="#0061C1" />
                    <HeaderStyle BackColor="#0061C1" ForeColor="White"></HeaderStyle>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Begin Date">
                    <ItemTemplate>
                    <asp:Label 
                            ID="BeginDate" runat="server" DataFormatString="{0:dd/MM/yyyy}" 
                            Font-Names="Verdana" Text='<%# Eval("BeginDate","{0:dd/MM/yyyy}")%>' Font-Size="X-Small" Height="20px" 
                             Width="100px"></asp:Label>
                    </ItemTemplate>
                    <FooterStyle BackColor="#0061C1" />
                    <HeaderStyle BackColor="#0061C1" ForeColor="White"></HeaderStyle>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="End Date">
                    <ItemTemplate>
                    <asp:Label 
                            ID="EndDate" runat="server" Font-Names="Verdana" Font-Size="X-Small" 
                            Height="20px" Text='<%# Eval("EndDate","{0:dd/MM/yyyy}")%>'  Width="100px"></asp:Label>
                    </ItemTemplate>
                    <FooterStyle BackColor="#0061C1" />
                    <HeaderStyle BackColor="#0061C1" ForeColor="White"></HeaderStyle>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Num of Days">
                    <ItemTemplate>
                    <asp:Label 
                            ID="NumofDays" runat="server" Font-Names="Verdana" Font-Size="X-Small" 
                            Height="20px" Text='<%# Eval("NumofDays")%>' Width="100px"></asp:Label>
                    </ItemTemplate>
                    <FooterStyle BackColor="#0061C1" />
                    <HeaderStyle BackColor="#0061C1" ForeColor="White"></HeaderStyle>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Type of Leave ">
                    <ItemTemplate>
                    <asp:Label 
                            ID="LeaveType" runat="server" Font-Names="Verdana" Font-Size="X-Small" 
                            Height="20px" Text='<%# Eval("TypeofLeave")%>'  Width="100px"></asp:Label>
                    </ItemTemplate>
                    <FooterStyle BackColor="#0061C1" />
                    <HeaderStyle BackColor="#0061C1" ForeColor="White"></HeaderStyle>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                    <asp:Label 
                            ID="Status" runat="server" Font-Names="Verdana" Font-Size="X-Small" 
                            ForeColor="Black" Height="20px" Text='<%# Eval("Status")%>'  Width="100px"></asp:Label>
                    </ItemTemplate>
                    <FooterStyle BackColor="#0061C1" />
                    <HeaderStyle BackColor="#0061C1" ForeColor="White"></HeaderStyle>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                    </asp:TemplateField>
                        <asp:TemplateField  HeaderText="Reason for Reject">
                        <ItemTemplate>
                        <asp:Label ID="RejectReason" 
                            runat="server" Font-Names="Verdana" Font-Size="X-Small" ForeColor="Black" 
                            Height="20px" Text='<%# Eval("RejectReason")%>' Enabled="true" Visible="true" Width="100px"></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                    <asp:TextBox ID="TxtRejectReason" 
                            runat="server" Font-Names="Verdana" Font-Size="X-Small" ForeColor="Black" 
                            Height="20px" Text='<%# Eval("RejectReason")%>' Enabled="true" Visible="true" Width="100px"></asp:TextBox>
                    </EditItemTemplate>
                    <FooterStyle BackColor="#0061C1" />
                    <HeaderStyle BackColor="#0061C1" ForeColor="White"></HeaderStyle>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                    </asp:TemplateField>
                         <asp:BoundField DataField="LogdInUser" Visible="false" >
                         <FooterStyle BackColor="#0061C1" />
                         <HeaderStyle BackColor="#0061C1" ForeColor="White" HorizontalAlign="Center" 
                             VerticalAlign="Middle" />
                         <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />
                         </asp:BoundField>
                         <asp:BoundField DataField="Manager" Visible="false" >
                         <FooterStyle BackColor="#0061C1" />
                         <HeaderStyle BackColor="#0061C1" ForeColor="White" HorizontalAlign="Center" 
                             VerticalAlign="Middle" />
                         <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />
                         </asp:BoundField>
                         <asp:CommandField ShowEditButton="true"  ButtonType="Button"  EditText="Edit">
                <ControlStyle Width="50" />
            </asp:CommandField>
                     </Columns>
                 </asp:GridView>

我需要编辑拒绝原因列。

cs页面

protected void Page_Load(object sender, EventArgs e)
    {
        MTMSDTO objc = new MTMSDTO();
        if (!IsPostBack)
        {
            int LeaveID = 0;
            int.TryParse(Request.QueryString["LeaveID"], out LeaveID);
            objc.LeaveID = LeaveID;
            objc.RejectReason = TxtRejectReason.Text;
            DataSet lapp = obj.GetLeaveApproved(objc);
            DataView LApp = new DataView();
            LApp.Table = lapp.Tables[0];
            GridView1.DataSource = LApp;
            GridView1.DataBind();

        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataBind();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int LeaveID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());

        TextBox TxtRejectReason = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TxtRejectReason");

        GridView1.EditIndex = -1;
        GridView1.DataBind();
    }

一旦我单击gridview中的编辑按钮,它就不会显示任何记录

请帮我

4

2 回答 2

2

试试下面的代码:

<asp:GridView ID="GridView1" runat="server"  Width = "550px"
         AutoGenerateColumns = "false" Font-Names = "Arial"
         Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" 
         HeaderStyle-BackColor = "green" AllowPaging ="true"  ShowFooter = "true" 
         OnPageIndexChanging = "OnPaging" onrowediting="EditCustomer"
         onrowupdating="UpdateCustomer"  onrowcancelingedit="CancelEdit"
         PageSize = "10" >
         <Columns>
         <asp:TemplateField ItemStyle-Width = "30px"  HeaderText = "CustomerID">
             <ItemTemplate>
                <asp:Label ID="lblCustomerID" runat="server"
                Text='<%# Eval("CustomerID")%>'></asp:Label>
  </ItemTemplate>
  <FooterTemplate>
        <asp:TextBox ID="txtCustomerID" Width = "40px"
        MaxLength = "5" runat="server"></asp:TextBox>
  </FooterTemplate>
  </asp:TemplateField><asp:TemplateField ItemStyle-Width = "100px"  HeaderText = "Name">
  <ItemTemplate>
    <asp:Label ID="lblContactName" runat="server"
            Text='<%# Eval("ContactName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
    <asp:TextBox ID="txtContactName" runat="server"
        Text='<%# Eval("ContactName")%>'></asp:TextBox>
</EditItemTemplate> 
<FooterTemplate>
    <asp:TextBox ID="txtContactName" runat="server"></asp:TextBox>
   </FooterTemplate>
   </asp:TemplateField>
   <asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Company">
   <ItemTemplate>
   <asp:Label ID="lblCompany" runat="server"
        Text='<%# Eval("CompanyName")%>'></asp:Label>
   </ItemTemplate>
   <EditItemTemplate>
   <asp:TextBox ID="txtCompany" runat="server"
        Text='<%# Eval("CompanyName")%>'></asp:TextBox>
   </EditItemTemplate> 
   <FooterTemplate>
      <asp:TextBox ID="txtCompany" runat="server"></asp:TextBox>
   </FooterTemplate>
   </asp:TemplateField>
   <asp:TemplateField>
   <ItemTemplate>
      <asp:LinkButton ID="lnkRemove" runat="server"
        CommandArgument = '<%# Eval("CustomerID")%>'
     OnClientClick = "return confirm('Do you want to delete?')"
    Text = "Delete" OnClick = "DeleteCustomer"></asp:LinkButton>
   </ItemTemplate>
   <FooterTemplate>
      <asp:Button ID="btnAdd" runat="server" Text="Add"
        OnClick = "AddNewCustomer" />
    </FooterTemplate>
   </asp:TemplateField>
   <asp:CommandField  ShowEditButton="True" />
   </Columns>
   <AlternatingRowStyle BackColor="#C2D69B"  />
   </asp:GridView>

代码背后:

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
        BindData();
   }
}
private void BindData()
{
}
protected void AddNewCustomer(object sender, EventArgs e)
{
         string CustomerID=((TextBox)GridView1.FooterRow.FindControl("txtCustomerID")).Text;
         string Name = ((TextBox)GridView1.FooterRow.FindControl("txtContactName")).Text;
         string Company = ((TextBox)GridView1.FooterRow.FindControl("txtCompany")).Text;
        //Your Code here...
 }
 protected void EditCustomer(object sender, GridViewEditEventArgs e)
 {
       GridView1.EditIndex = e.NewEditIndex;
        BindData();
 }
 protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
 {
      GridView1.EditIndex = -1;
      BindData();
  }
 protected void UpdateCustomer(object sender, GridViewUpdateEventArgs e)
 {
      string CustomerID = ((Label)GridView1.Rows[e.RowIndex]
                    .FindControl("lblCustomerID")).Text;
      string Name = ((TextBox)GridView1.Rows[e.RowIndex]
                    .FindControl("txtContactName")).Text;
      string Company = ((TextBox)GridView1.Rows[e.RowIndex]
                    .FindControl("txtCompany")).Text;
      //Your code here...
  }

  protected void DeleteCustomer(object sender, EventArgs e)
  {
       LinkButton lnkRemove = (LinkButton)sender;
       SqlConnection con = new SqlConnection(strConnString);
       SqlCommand cmd = new SqlCommand();
       cmd.CommandType = CommandType.Text;
       cmd.CommandText = "delete from  customers where " +
       "CustomerID=@CustomerID;" +
       "select CustomerID,ContactName,CompanyName from customers";
       cmd.Parameters.Add("@CustomerID", SqlDbType.VarChar).Value
    = lnkRemove.CommandArgument;
     GridView1.DataSource = GetData(cmd);
     GridView1.DataBind();
  }
于 2013-07-26T08:34:30.803 回答
1

在gridview编辑时使用代码,

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView gridView1= (GridView)sender;
   // Change the row state
    gridView1.Rows[e.NewEditIndex].RowState = DataControlRowState.Edit;

}

对于更新(这是示例代码)

 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {
      GridView gv = (GridView)sender;
      GridViewRow gvr = (GridViewRow)gv.Rows[e.RowIndex];
      TextBox TxtRejectReason= (TextBox)gvr.FindControl("TxtRejectReason");
      string s = TxtRejectReason.Text;
      GridView.EditIndex = -1;
      GridView.DataBind();
 }
于 2013-07-26T06:53:50.440 回答