我有一个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;
这是我的gridview Markup
<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>
我该怎么办?有什么想法吗?
答案 0 :(得分:1)
其他查询返回这样的值?
// This row ending value
TextBox txtThisEndingRange = GridView1.Rows[e.RowIndex].FindControl("txtEndingRange") as TextBox;
或者它们也是空的吗?
你可以尝试不要施放价值试试这个。
var txtNextStartingPoint = GridView1.Rows[e.RowIndex + 1].FindControl("txtStartingRange")
也许它不是你得到的文本框。
答案 1 :(得分:1)
有两点需要注意:
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))
{
..
..
}