编辑Gridview Row ASP.net

时间:2017-03-20 16:12:38

标签: c# asp.net gridview edit

努力编辑Gridview Row。一旦我点击编辑,gridview就会消失,我猜是因为我只绑定了网格!IsPostBack - 虽然教程告诉我这样做。

我尝试在回发上重新绑定,这让我可以编辑gridview但保留默认值 - 而不是新值。

任何帮助表示感谢。

if (!IsPostBack)
{
    if (Session["Order_ID"] != null)
    {
        showgrid(int.Parse(Session["Order_ID"].ToString()));
    }
}

public void showgrid(int Order_ID)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn"].ToString());

    string sqlstring = "Removed - is working";

    SqlCommand cmd = new SqlCommand(sqlstring, conn);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    conn.Close();

    if (ds.Tables[0].Rows.Count > 0)
    {
        gv_ExistingAds.DataSource = ds;
        gv_ExistingAds.DataBind();
    }
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    gv_ExistingAds.EditIndex = e.NewEditIndex;
    showgrid(int.Parse(Session["Order_ID"].ToString()));
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    Label lb = (Label)gv_ExistingAds.Rows[e.RowIndex].FindControl("Label3");

    TextBox tx1 = (TextBox)gv_ExistingAds.Rows[e.RowIndex].FindControl("TextBox3");

    string tx11 = tx1.Text;

    gv_ExistingAds.EditIndex = -1;
    showgrid(int.Parse(Session["Order_ID"].ToString()));
}

HTML

<asp:GridView runat="server" AutoGenerateColumns="false" ShowFooter="True" ShowHeaderWhenEmpty="false" EmptyDataText="This order has no ads..."
    ID="gv_ExistingAds" CssClass="table table-striped table-bordered table-hover"
    Visible="true" DataKeyNames="Orders_Editions_ID, Cancelled, Order_ID" EnableViewState="true" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
    <Columns>
        <asp:BoundField HeaderText="Publication Title" HtmlEncode="false" DataField="Publication_Title" />
        <asp:BoundField HeaderText="Insertion Date" HtmlEncode="false" DataField="Insertion_Date" />
        <asp:BoundField HeaderText="Advert Type" HtmlEncode="false" DataField="Description" />
        <asp:BoundField DataField="Ad_Size" HeaderText="Size" ItemStyle-Width="150" />
        <asp:BoundField DataField="Position" HeaderText="Position" ItemStyle-Width="150" />
        <asp:BoundField DataField="Revenue" HeaderText="Revenue" ItemStyle-Width="150" />
        <asp:TemplateField HeaderText="Note" ItemStyle-Width="150">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Eval("Note") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("Note") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Edit" ShowHeader="false">
            <ItemTemplate>
                <asp:LinkButton ID="btnedit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>
                <asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Action">
            <ItemStyle Width="165px" />
            <ItemTemplate>
                <%--<asp:LinkButton ID="btn_UpdateAdvert" ControlStyle-CssClass="btn btn-primary btn-xs" CommandName="Update_Advert" CommandArgument='<%#Eval("Orders_Editions_ID")%>' runat="server" Text="Update" CausesValidation="false" />--%>
                <asp:LinkButton ID="btn_CancelAdvert" ControlStyle-CssClass="btn btn-warning btn-xs" CommandName="Cancel_Advert" CommandArgument='<%#Eval("Orders_Editions_ID")%>' runat="server" Text="Cancel" CausesValidation="false" />
                <asp:LinkButton ID="btn_DeleteAdvert" ControlStyle-CssClass="btn btn-danger btn-xs" CommandName="Delete_Advert" CommandArgument='<%#Eval("Orders_Editions_ID")%>' runat="server" Text="Delete" CausesValidation="false" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

    <HeaderStyle BackColor="#FF6699" />

</asp:GridView>

0 个答案:

没有答案