不使用命令字段编辑gridview行asp.net

时间:2018-01-22 11:01:29

标签: c# asp.net gridview

我想在gridview中编辑一个列,但由于空间问题,没有编辑列(键更新,取消...)。我试图回忆一下与onRowDataBound中的那些按钮相关的javascript函数,但是没有采取onclick事件。在这里我的代码。有人可以帮我吗?

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowIndex == GridView1.EditIndex)    //GridView is in edit mode
            {
                //update or cancel buttons
                LinkButton updateBtn = (LinkButton)e.Row.Cells[0].Controls[0];             
                string updateScript = ClientScript.GetPostBackClientHyperlink(updateBtn, ""); 
                Button1.Attributes["onclick"] = updateScript;                                  

                string cancelScript = string.Format("javascript:__doPostBack('{0}','Cancel${1}')", GridView1.ID, e.Row.RowIndex);
                Button2.Attributes["onclick"] = cancelScript;
            }
            else   //GridView is in read mode
            {
                //edit button
                string editScript = string.Format("javascript:__doPostBack('{0}','Edit${1}')", GridView1.ID, e.Row.RowIndex);
                e.Row.Attributes["onclick"] = editScript;     
            }

        }
        if (GridView1.EditIndex >= 0)
        {
            Button1.Enabled = true;
            Button2.Enabled = true;
        }
        else
        {
            Button1.Enabled = false;
            Button2.Enabled = false;
        }

    }

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false" DataKeyNames="PKCOD" AllowPaging="True" PageSize="10"
                                                CssClass="auto-style1" OnRowDataBound="GridView1_RowDataBound">
        <Columns>
            <%--
            <asp:CommandField ShowEditButton="True" />
            <asp:TemplateField HeaderText="QUANTITA" SortExpression="QUANTITA">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("QUANTITA") %>'></asp:TextBox>
                    <br />
                    <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox1" EnableClientScript="False" ErrorMessage="ErrorMessage" MaximumValue="999" MinimumValue="0" Type="Integer">Valori tra 0 e 999</asp:RangeValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("QUANTITA") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
             --%>

            <asp:CommandField ShowEditButton="True" />
            <asp:TemplateField HeaderText="QUANTITA" SortExpression="QUANTITA">
                <ItemTemplate>
                     <asp:TextBox ID="textBoxQuantita" runat="server" Text='<%# Bind("QUANTITA") %>'></asp:TextBox>
                    <asp:RangeValidator ID="RangeValidator1" Type="Double" MinimumValue="0" MaximumValue="999" ControlToValidate="textBoxQuantita" runat="server"
                        ErrorMessage="Inserisci solo numeri compresi tra 0 e 999"></asp:RangeValidator>
                </ItemTemplate>
            </asp:TemplateField>



            <asp:BoundField DataField="CODICE" HeaderText="CODICE" ReadOnly="True" SortExpression="CODICE" />
            <asp:BoundField DataField="DESCRIZIONE" HeaderText="DESCRIZIONE" ReadOnly="True" SortExpression="DESCRIZIONE" />
            <asp:BoundField DataField="CATEGORIA" HeaderText="CATEGORIA" ReadOnly="True" SortExpression="CATEGORIA" Visible="False" />
            <asp:BoundField DataField="PKCOD" HeaderText="PKCOD" ReadOnly="True" SortExpression="PKCOD" InsertVisible="False" Visible="False" />
            <asp:BoundField DataField="USERNAME" HeaderText="USERNAME" ReadOnly="True" SortExpression="USERNAME" Visible="False" />
            <asp:BoundField DataField="USERID" HeaderText="USERID" ReadOnly="True" SortExpression="USERID" Visible="False" />
            <asp:BoundField DataField="USERDT" HeaderText="USERDT" ReadOnly="True" SortExpression="USERDT" Visible="False" />
            <asp:BoundField DataField="STATO" HeaderText="STATO" ReadOnly="True" SortExpression="STATO" Visible="False" />      
        </Columns>
    </asp:GridView>

0 个答案:

没有答案