gridview编辑命令错误。行不会更改要更新的模式

时间:2013-03-19 20:19:46

标签: c# asp.net

我有gridview,而且我对gridview行的asp.net Edit,Delete,Update命令不是很专业。在这里,我想通过单击编辑模式将gridview行模式更改为编辑。但是当我点击编辑按钮时它什么也没做。有人可以检查一下会出错吗?

   <asp:GridView ID="GridViewTopics" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
                            GridLines="Horizontal" OnRowEditing="GridViewTopics_RowEditing" CssClass="gridstyle"
                            Style="margin-left: 30px;" Width="230px">
                            <Columns>
                                <asp:TemplateField HeaderText="Topics">
                                    <ItemTemplate>
                                        <table>
                                            <tr>
                                                <td>
                                                    <div>
                                                        <asp:Label ID="LabelTopicName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                                                    </div>
                                                </td>
                                                <td valign="top" align="right" >
                                                    <div>
                                                        <asp:LinkButton ID="LinkButtonEdit" runat="server" Text="Edit" CommandName="Edit"></asp:LinkButton>
                                                    </div>
                                                </td>
                                            </tr>
                                        </table>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="TextBoxEditTopicName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                                        <asp:LinkButton ID="LinkButtonUpdate" runat="server" CausesValidation="True" CommandName="Update"
                                            Text="Update"></asp:LinkButton>
                                        <asp:LinkButton ID="LinkButtonCancel" runat="server" CausesValidation="False" CommandName="Cancel"
                                            Text="Cancel"></asp:LinkButton>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                            <HeaderStyle CssClass="headerclass" />
                        </asp:GridView>

1 个答案:

答案 0 :(得分:1)

尝试使用命令字段。它还会为您自动生成更新和取消按钮。相应地处理事件。

以下代码

<强> ASPX

<asp:GridView ID="GridViewTopics" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
    GridLines="Horizontal" OnRowEditing="GridViewTopics_RowEditing" CssClass="gridstyle" OnRowCancelingEdit="GridViewTopics_RowCancelingEdit" OnRowUpdating="GridViewTopics_RowUpdating"
    Style="margin-left: 30px;" Width="230px">
    <Columns>
        <asp:TemplateField HeaderText="Topics">
            <ItemTemplate>
                <asp:Label ID="LabelTopicName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="TextBoxEditTopicName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ButtonType="Link" ShowEditButton="true"/>
    </Columns>
    <HeaderStyle CssClass="headerclass" />
</asp:GridView>

<强> C#

protected void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack) {
    GridViewTopics.DataSource = GetSomeSampleData();
    GridViewTopics.DataBind();
}
}
private DataTable GetSomeSampleData()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Name");
    dt.Columns.Add("Id");
    for (index = 1; index <= 10; index++) {
        DataRow dr = dt.NewRow();
        dr("Id") = index;
        dr("Name") = "SomeName" + index.ToString();
        dt.Rows.Add(dr);
  }
    return dt;
}

protected void GridViewTopics_RowEditing(object sender, GridViewEditEventArgs e)
{
   GridViewTopics.DataSource = GetSomeSampleData();
   GridViewTopics.EditIndex = e.NewEditIndex;
   GridViewTopics.DataBind();
}
protected void GridViewTopics_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//Handle accordingly
}
protected void GridViewTopics_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Handle accordingly
}