了解Gridview RowCommand

时间:2014-08-05 22:03:31

标签: c# asp.net gridview

当我单击编辑按钮时,我编写了一个不会触发的gridview,我发现了一个较旧的问题,通过在RowCommand方法下使用带有if语句的e.CommandName来解决我的问题。我正在试图弄清楚如何用我的代码实现它。

我的问题是,如何在更新期间使用e.RowIndex来查找我的控件并立即引用它们?此外,我尝试调用我的旧Update方法,但它不会让我使用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.NET:

<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>

2 个答案:

答案 0 :(得分:1)

您可以将RowIndex作为CommandBrgument传递给LinkBut​​tons。

 <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")
    {
      ....
    }
}

答案 1 :(得分:0)

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

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