了解Gridview行命令

本文关键字:命令 Gridview 了解 | 更新日期: 2023-09-27 18:16:39

我已经编写了一个gridview,当我单击编辑按钮时不会触发,我发现了一个较旧的问题,通过在RowCommand方法下使用e.p commandname和if语句来解决我的问题。我试图找出如何实现它与我的代码。

我的问题是,我如何使用e.RowIndex在更新期间找到我的控件和引用那些现在?此外,我已经尝试调用我的旧更新方法,但它不会让我使用sender和e作为参数,因为GridviewCommandEventArgs不同于GridViewEventUpdateArgs。我很困惑,如果你能帮我解决这个问题,我将不胜感激。

c#:

void RowCommand(Object sender, GridViewCommandEventArgs e)
{  
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if (e.CommandName == "Edit")
    {
    }
   if (e.CommandName == "Update")
    {
        UpdateCustomer(sender, e);
        string nFirstName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFirstName")).Text;
        string nLastName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtLastName")).Text;
        string nEmergency = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEmergency")).Text;
        string nCell = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCell")).Text;
        string nAge = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAge")).Text;
        string nActivityCard = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtActivityCard")).Text;
        string nBoat = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBoat")).Text;
        string nInitials = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtInitials")).Text;
        string nGroup = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGroup")).Text;
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "update Person set FirstName=@FirstName, LastName=@LastName, " +
         "Emergency#=@Emergency, Cell#=@Cell, Age=@Age, ActivityCard=@ActivityCard, Initials=@Initials, CraftType=@Boat, Group#=@Group " +
         "where Person.PersonID=@Pid;" +
         "SELECT Person.PersonID, Person.FirstName AS FirstName, Person.LastName AS LastName, Person.Emergency# AS Emergency#, Person.Cell# AS Cell#, Person.Age AS Age, " +
        "Person.ActivityCard AS ActivityCard, Person.CraftType AS CraftType, Person.Initials AS Initials, Person.Group# AS Group# " +
        "FROM Person INNER JOIN " +
        "TripSchedule ON Person.PersonID = TripSchedule.PersonID where TripSchedule.Date = @Date and " +
        "TripSchedule.Time = @Time and TripSchedule.TripType = @Type order by Person.Group#;";
        cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = nFirstName;
        cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = nLastName;
        cmd.Parameters.Add("@Emergency", SqlDbType.NChar).Value = nEmergency;
        cmd.Parameters.Add("@Cell", SqlDbType.NChar).Value = nCell;
        cmd.Parameters.Add("@Age", SqlDbType.NChar).Value = nAge;
        cmd.Parameters.Add("@ActivityCard", SqlDbType.NChar).Value = nActivityCard;
        cmd.Parameters.Add("@Initials", SqlDbType.NChar).Value = nInitials;
        cmd.Parameters.Add("@Boat", SqlDbType.VarChar).Value = nBoat;
        cmd.Parameters.Add("@Group", SqlDbType.VarChar).Value = nGroup;
        cmd.Parameters.AddWithValue("@Date", TextBox1.Text);
        cmd.Parameters.AddWithValue("@Time", ddlTripTime.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@Type", ddlTripType.SelectedItem.ToString());
        long personID = long.Parse(GridView1.DataKeys[e.RowIndex].Values["PersonID"].ToString());
        cmd.Parameters.AddWithValue("@Pid", personID);
        GridView1.EditIndex = -1;
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();
    }
    if (e.CommandName == "Cancel")
    {
    }
}

ASP。净:

<div id="dvGrid" style="padding: 0px">
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:GridView ID="GridView1" DataKeyNames="PersonID" runat="server" AutoGenerateColumns="False"
                        Font-Names="Arial" Font-Size="10pt" AlternatingRowStyle-BackColor="blue" HeaderStyle-BackColor="aqua"
                        ShowFooter="true" OnRowEditing="EditCustomer" OnRowUpdating="UpdateCustomer"
                        OnRowCancelingEdit="CancelEdit" ShowHeaderWhenEmpty="true" Height="95px">
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <%# Container.DataItemIndex + 1 %>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="First Name">
                                <ItemTemplate>
                                    <asp:Label ID="lblFirstName" runat="server"  Height="20px" Text='<%# Eval("FirstName")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtFirstName"  runat="server" Text='<%# Eval("FirstName")%>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemStyle Width = "60px" />
                                <FooterTemplate>
                                    <asp:TextBox ID="txtFirstName" width="60px"  MaxLength="15" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                </asp:TemplateField>
                            <asp:TemplateField  HeaderText="Last Name">
                                <ItemTemplate>
                                    <asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LastName")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtLastName" runat="server" Text='<%#Bind("LastName")%>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtLastName" width="60px" MaxLength="15" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField  HeaderText="Age">
                                <ItemTemplate>
                                    <asp:Label ID="lblAge" runat="server" Text='<%# Eval("Age")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtAge" runat="server" Text='<%# Eval("Age")%>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtAge" Width="30px" MaxLength="2" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField  HeaderText="Activity Card">
                                <ItemTemplate>
                                    <asp:Label ID="lblActivityCard" runat="server" Text='<%# Eval("ActivityCard")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtActivityCard" runat="server" Text='<%# Eval("ActivityCard")%>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtActivityCard" Width="50px" MaxLength="7" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                </asp:TemplateField>
                            <asp:TemplateField  HeaderText="Cell Phone">
                                <ItemTemplate>
                                    <asp:Label ID="lblCell" runat="server" Text='<%# Eval("Cell#")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtCell" runat="server" Text='<%# Eval("Cell#")%>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtCell" Width="70px" MaxLength="10" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField  HeaderText="Emergency Phone">
                                <ItemTemplate>
                                    <asp:Label ID="lblEmergency" runat="server" Text='<%# Eval("Emergency#")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtEmergency" runat="server" Text='<%# Eval("Emergency#")%>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtEmergency" width="70px" MaxLength="10" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>               
                            <asp:TemplateField HeaderText="Boat Type">
                                <ItemTemplate>
                                    <asp:Label ID="lblBoat" runat="server" Text='<%# Eval("CraftType")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtBoat" runat="server" Text='<%# Eval("CraftType")%>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtBoat" Width="80px" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField  HeaderText="Initials">
                                <ItemTemplate>
                                    <asp:Label ID="lblInitials" runat="server" Text='<%# Eval("Initials")%>'></asp:Label>
                                </ItemTemplate>
                                 <EditItemTemplate>
                                    <asp:TextBox ID="txtInitials" runat="server" Text='<%# Eval("Initials")%>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtInitials" width="30px" MaxLength="2" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                             <asp:TemplateField  HeaderText="Group #">
                                <ItemTemplate>
                                    <asp:Label ID="lblGroup" runat="server" Text='<%# Eval("Group#")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtGroup" runat="server" Text='<%# Eval("Group#")%>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtGroup" MaxLength="2" Width="20px" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%# Eval("PersonID")%>'
                                        OnClientClick="return confirm('Are you sure you want to delete?')" Text="Delete"
                                        OnClick="DeleteCustomer"></asp:LinkButton>
                                        </ItemTemplate>
                                <FooterTemplate>
                                    <asp:Button ID="btnAdd" runat="server" Text="Submit" OnClick="AddNewCustomer" />
                                </FooterTemplate>
                            </asp:TemplateField>
                           <asp:TemplateField HeaderText="">
    <ItemTemplate>
        <asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update">Update</asp:LinkButton>
        <asp:LinkButton ID="lnkCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
    </EditItemTemplate> 
</asp:TemplateField>
                        </Columns>
                    </asp:GridView>

了解Gridview行命令

您可以将RowIndex作为您的LinkButtons的CommandArgument。

 <EditItemTemplate>
    <asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update" CommandArgument='<%# Container.DataItemIndex %>'>Update</asp:LinkButton>
    <asp:LinkButton ID="lnkCancel" runat="server" CommandName="Cancel" CommandArgument='<%# Container.DataItemIndex %>'>Cancel</asp:LinkButton>
</EditItemTemplate> 

可以从后面的代码访问。使用rowindex,您可以获得当前行并使用FindControl

找到任何控件。
void RowCommand(Object sender, GridViewCommandEventArgs e)
{ 
    int rowindex = Convert.ToInt32(e.CommandArgument);
    if (e.CommandName == "Update")
    {
      ....
    }
    if (e.CommandName == "Cancel")
    {
      ....
    }
}

e.CommandArgument将包含所单击行的索引,因此您可以通过

访问相关控件
int index = Convert.ToInt32(e.CommandArgument);
GridView1.Rows[index].Controls[x];