0

我有gridview,我对gridview 行的asp.net 编辑、删除、更新命令不是很专家。这里我想通过单击编辑模式将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>
4

1 回答 1

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
}
于 2013-03-19T20:57:23.143 回答