62,197
社区成员
发帖
与我相关
我的任务
分享
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
ViewState["InEditMode"] = false; //用于跟踪是否在编辑模式下。
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
if (!Convert.IsDBNull(drv["UnitPrice"]) && Convert.ToDecimal(drv["UnitPrice"]) < 50)
e.Row.Visible = false;
}
if ((bool)ViewState["InEditMode"] == true) //如果在编辑模式下
{
TextBox tb = e.Row.FindControl("TextBox2") as TextBox; //在编辑模式下才能找到TextBox2,也就是输入UnitPrice的TextBox
if (tb != null)
{
if (decimal.Parse(tb.Text) > 70) //如果价格大于70,则设置为只读
tb.ReadOnly = true;
}
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewEditIndex];
string s = ((Label)row.FindControl("LabelUnitPrice")).Text;
if (decimal.Parse(s) > 100)
{
Literal msg = new Literal();
msg.Text = "<script>alert('价格大于100,不能编辑此行');</script>";
this.Controls.Add(msg);
e.Cancel = true;
e.NewEditIndex = -1;
}
ViewState["InEditMode"] = true; // 设置进入编辑模式。
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ProductID" DataSourceID="SqlDataSource1"
onrowdatabound="GridView1_RowDataBound" onrowediting="GridView1_RowEditing">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
<asp:TemplateField HeaderText="ProductName" SortExpression="ProductName">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ProductName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UnitPrice" SortExpression="UnitPrice">
<ItemTemplate>
<asp:Label ID="LabelUnitPrice" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID"
InsertCommand="INSERT INTO [Products] ([ProductName], [UnitPrice]) VALUES (@ProductName, @UnitPrice)"
SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products]"
UpdateCommand="UPDATE [Products] SET [ProductName] = @ProductName, [UnitPrice] = @UnitPrice WHERE [ProductID] = @ProductID">
<DeleteParameters>
<asp:Parameter Name="ProductID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="UnitPrice" Type="Decimal" />
<asp:Parameter Name="ProductID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="UnitPrice" Type="Decimal" />
</InsertParameters>
</asp:SqlDataSource>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//if (e.Row.RowType == DataControlRowType.DataRow)
//{
// DataRowView drv = (DataRowView)e.Row.DataItem;
// if (!Convert.IsDBNull(drv["UnitPrice"]) && Convert.ToDecimal(drv["UnitPrice"]) < 50)
// e.Row.Visible = false;
//}
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow row = e.Row;
string s = ((Label)row.FindControl("LabelUnitPrice")).Text;
if (!string.IsNullOrEmpty(s) && decimal.Parse(s) < 50)
row.Visible = false;
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewEditIndex];
string s = ((Label)row.FindControl("LabelUnitPrice")).Text;
if (decimal.Parse(s) > 100)
{
Literal msg = new Literal();
msg.Text = "<script>alert('价格大于100,不能编辑此行');</script>";
this.Controls.Add(msg);
e.Cancel = true;
e.NewEditIndex = -1;
}
}