我有两个按钮(btn_Out和btn_In)。如果名为 Machines 的SQL表中的字段 Active 的值为True(1),则显示btn_Out,如果 Active 设置为False(0)。
gridview中数据的每一行都可能有不同的Active标志,因此按钮需要反映这一点。该按钮会将活动标志从0更改为1,反之亦然(我的工作正常!)。
我正在使用gridview,我的代码如下:
ASP.NET
<ItemTemplate>
<asp:Button ID="btn_In" runat="server" Text="Set in Scope" CommandName="Update" CssClass="Button" />
</ItemTemplate>
<ItemTemplate>
<asp:Button ID="btn_Out" runat="server" Text="Set in Scope" CommandName="Update" CssClass="Button" />
</ItemTemplate>
C#:
DataTable dt = new DataTable();
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Open();
con = new SqlConnection(cs);
// SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select [active] from [ALLMACHINES].[dbo].[Machines] where [serial_number] = @serialNumber";
cmd.Parameters.AddWithValue("@Serial_Number", serialNumber);
int Active = Convert.ToInt32(cmd.ExecuteScalar());
if (Active == 1)
{
btn_In.Visible = false;
btn_Out.Visible = true;
}
else if (Active == 0)
{
btn_Out.Visible = false;
btn_In.Visible = true;
}
}
我的aspx.net页面不喜欢我在if和else if语句中使用了按钮而不会编译!任何提示将非常感谢:)
答案 0 :(得分:1)
动态更改gridview单元格值的最佳方法是onrowdatabound
事件。可以按照以下代码帮助您:
Aspx Page
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Lable ID="lblstus" runat="server" Text="#Eval("active")" Visible=false>
</ItemTemplate>
... otherfields you wanted to add
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn_In" runat="server" Text="Set in Scope" CommandName="Update" CssClass="Button" />
<asp:Button ID="btn_Out" runat="server" Text="Set in Scope" CommandName="Update" CssClass="Button" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码隐藏:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Lable lblstus = e.Row.FindControl("lblstus") as Lable;
Button btn_Out = e.Row.FindControl("btn_Out") as Button;
Button btn_In = e.Row.FindControl("btn_In") as Button;
if(lblstus.Text == "1")
{
btn_In.Visible = false;
btn_Out.Visible = true;
} else {
btn_In.Visible = false;
btn_Out.Visible = true;
}
}
}
答案 1 :(得分:1)
为什么不直接在按钮中设置可见性?那你就不需要OnRowDataBound
<asp:Button ID="btn_In" Visible='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "Active")) == true %>' Text="Set in Scope" CommandName="Update" CssClass="Button" runat="server" />
<asp:Button ID="btn_Out" Visible='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "Active")) == false %>' runat="server" Text="Set in Scope" CommandName="Update" CssClass="Button" />
如果您的标志不是布尔值,也适用于字符串等。
Visible='<%# DataBinder.Eval(Container.DataItem, "Active").ToString() == "1" %>'
答案 2 :(得分:0)
我有两个按钮(btn_Out和btn_In)。
每个行中有2个按钮,每行可能有不同的值,即您不能像在代码中那样选择该值一次。相反,请阅读RowDataBound事件(MSDN,How to find control in TemplateField of GridView?等),删除当前代码并执行与此类似的操作
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the underlying data item. In this example
// the underlying data item is a DataRowView object.
DataRowView rowView = (DataRowView)e.Row.DataItem;
// Retrieve the state value for the current row.
String flag = rowView["active"].ToString();
Button btn_Out = e.Row.FindControl("btn_Out") as Button;
Button btn_In = e.Row.FindControl("btn_In") as Button;
if (flag == "1") {
btn_In.Visible = false;
btn_Out.Visible = true;
} else {
btn_In.Visible = false;
btn_Out.Visible = true;
}
}
}