1

我有一个带有 sqldatasource 的 gridview。我的数据应该是这样的:

期望的

但是在任何行上,当我单击 EDIT 按钮,然后单击“更新”按钮时,我想检查其上一行“Ending_Price”是否必须小于该行的 Start_Price。此外,此行的 End_Price 必须小于下一行的起始价格。

如果这没有发生,则必须通知用户,因为此范围必须按顺序排列。

这是我的代码:

        protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
    {


        if (e.RowIndex == 0 && GridView1.Rows.Count != 1)
        {
            // Next Row Starting Value
            TextBox txtNextStartingPoint = GridView1.Rows[e.RowIndex + 1].FindControl("txtStartingRange") as TextBox;

            // This row ending value
            TextBox txtThisEndingRange = GridView1.Rows[e.RowIndex].FindControl("txtEndingRange") as TextBox;

            if (Convert.ToDecimal(txtThisEndingRange.Text) >= Convert.ToDecimal(txtNextStartingPoint))
            {
                NotificationHelper.ShowError(this, "Invalid Ending Value");
                e.Cancel = true;
                return;
            }

        }

        TextBox txtStartingPoint = GridView1.Rows[e.RowIndex].FindControl("txtStartingRange") as TextBox;
        TextBox txtEndingRange = GridView1.Rows[e.RowIndex].FindControl("txtEndingRange") as TextBox;
        TextBox txtDiscount_Percentage = GridView1.Rows[e.RowIndex].FindControl("txtDiscount_Percentage") as TextBox;
        Label txtDiscount_ID = GridView1.Rows[e.RowIndex].FindControl("lblId") as Label;


        SqlDataSource1.UpdateParameters["sr"].DefaultValue = txtStartingPoint.Text;
        SqlDataSource1.UpdateParameters["re"].DefaultValue = txtEndingRange.Text;
        SqlDataSource1.UpdateParameters["perc"].DefaultValue = txtDiscount_Percentage.Text;
        SqlDataSource1.UpdateParameters["discount_ID"].DefaultValue = txtDiscount_ID.Text;


        SqlDataSource1.Update();
    }



    /// <summary>
    /// Edit record
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void editRecord(object sender, GridViewEditEventArgs e)
    {
        // Get the current row index for edit record
        GridView1.EditIndex = e.NewEditIndex;

    }


    protected void cancelRecord(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
    }

这将返回一个空值:

  TextBox txtNextStartingPoint = GridView1.Rows[e.RowIndex + 1].FindControl("txtStartingRange") as TextBox;

这是我的网格视图标记

   <asp:GridView ID="GridView1" runat="server" ShowHeaderWhenEmpty="True" AutoGenerateColumns="False"
                OnRowCancelingEdit="cancelRecord" OnRowEditing="editRecord" OnRowUpdating="UpdateRecord"
                CellPadding="4" GridLines="None" Width="673px" ForeColor="#333333" 
                DataSourceID="SqlDataSource1" OnDataBound="GridView1_DataBound"
                >
                <RowStyle HorizontalAlign="Center" />
                <AlternatingRowStyle BackColor="White" />
                <EditRowStyle BackColor="#7C6F57" />
                <FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True" />
                <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#E3EAEB" />
                <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            Id</HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblId" runat="server" Text='<%#Bind("discount_id")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            Starting Point</HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblStartingRange" runat="server" Text='<%#Bind("starting_range") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtStartingRange" runat="server" Text='<%#Bind("starting_range") %>'
                                MaxLength="50"></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtNewStartingRange" runat="server" MaxLength="50"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            Ending Range</HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblEndingRange" runat="server" Text='<%#Bind("ending_range") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtEndingRange" runat="server" Text='<%#Bind("ending_range") %>'
                                MaxLength="2"></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtEndingRange" runat="server" MaxLength="2"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            Discount Percentage</HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lbldiscount_percentage" runat="server" Text='<%#Bind("discount_percentage") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtDiscount_Percentage" runat="server" Text='<%#Bind("discount_percentage") %>'
                                MaxLength="10"></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtNewDiscount_Percentage" runat="server" MaxLength="10"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            Operation</HeaderTemplate>
                        <ItemTemplate>
                            <asp:Button ID="btnEdit" runat="server" CommandName="Edit" Text="Edit" />
                            <asp:Button ID="btnDelete" runat="server" CommandName="Delete" Text="Delete" CausesValidation="true"
                                OnClientClick="return confirm('Are you sure?')" />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Button ID="btnUpdate" runat="server" CommandName="Update" Text="Update" />
                            <asp:Button ID="btnCancel" runat="server" CommandName="Cancel" Text="Cancel" CausesValidation="false" />
                        </EditItemTemplate>
                        <FooterTemplate>
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>
                <EmptyDataTemplate>
                    No record available
                </EmptyDataTemplate>
            </asp:GridView>

我该怎么办?任何想法?

4

2 回答 2

1

有2点需要注意:

1.)获取当前行的值(正在编辑的行) 在这种情况下,值在TextBox控制范围内,因为行现在处于编辑模式。

// This row ending value
TextBox txtThisEndingRange = GridView1.Rows[e.RowIndex].FindControl("txtEndingRange") as TextBox;

2.) 从下一行获取值(正在编辑的当前行旁边的行) 在这种情况下,行值在Label控制范围内,因为行不在Edit模式下,而是在Normal模式下。

这意味着<ItemTemplate>控件当前由 GridView 而不是<EditItemTemplate>控件呈现。

所以现在你可以猜到你不需要找到TextBox: txtStartingRange,但你必须得到Label: lblStartingRange

// Next Row starting Value
Label lblNextStartRange= GridView1.Rows[e.RowIndex + 1].FindControl("lblStartingRange") as Label;

现在对值进行正常检查/比较:

if (Convert.ToDecimal(txtThisEndingRange.Text) >= Convert.ToDecimal(lblNextStartRange.Text))
{
..
..
}
于 2013-09-25T10:16:05.693 回答
1

其他查询返回这样的值?

// This row ending value
TextBox txtThisEndingRange = GridView1.Rows[e.RowIndex].FindControl("txtEndingRange") as TextBox;

还是它们也是 null ?

你可以试着不投价值试试这个。

var txtNextStartingPoint = GridView1.Rows[e.RowIndex + 1].FindControl("txtStartingRange")

也许它不是你得到的文本框。

于 2013-09-25T06:38:00.777 回答