在asp.net中将链接按钮text = approve更改为"APPROVED
本文关键字:quot APPROVED approve net asp 链接 text 按钮 | 更新日期: 2023-09-27 17:51:18
我有这个在我的html:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnApprove" runat="server" Text="Approve" CommandName="Approve" CommandArgument='<%# Eval("ProductID") %>' />
</ItemTemplate>
</asp:TemplateField>
,下面是我的代码:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("chkCtrl") as CheckBox);
LinkButton link = ((LinkButton)GridView1.FindControl("btnApprove"));
if (chkRow.Checked)
{
using (SqlConnection scn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"))
{
scn.Open();
SqlCommand cmd = new SqlCommand("update o set o.Updatedproduct = p.ProductQuantity - o.Totalproduct from CustomerProducts o inner join Products p on o.ProductID = p.ProductID WHERE o.CustomerID=@CustomerID", scn);
cmd.Parameters.AddWithValue("@CustomerID", SqlDbType.Int).Value = row.Cells[0].Text;
cmd.ExecuteNonQuery();
link.Text = "Approved";
GridView1.DataBind();
}
}
}
}
我想要发生的是,在我点击approve link button
之后,它将执行其功能,然后将链接按钮文本更改为批准,如果可能的话,更改为只读状态。
我试过做link.Enabled
,但似乎没有读它。Link.Text="Approved
也不能做到这一点。有什么诀窍吗?非常感谢
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("chkCtrl") as CheckBox);
LinkButton link = row.Cells[8].FindControl("btnApprove") as LinkButton;
if (link != null)
link.Text = "Approved";
if (chkRow.Checked)
{
using (SqlConnection scn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"))
{
scn.Open();
SqlCommand cmd = new SqlCommand("update o set o.Updatedproduct = p.ProductQuantity - o.Totalproduct from CustomerProducts o inner join Products p on o.ProductID = p.ProductID WHERE o.CustomerID=@CustomerID", scn);
cmd.Parameters.AddWithValue("@CustomerID", SqlDbType.Int).Value = row.Cells[0].Text;
cmd.ExecuteNonQuery();
//link.Text = "Approved";
Label1.Visible = true;
GridView1.DataBind();
}
}
当我点击approve链接按钮时,它将所有文本更改为approved。它应该是被点击的按钮,只会改变。希望它能被永久地改为批准。谢谢你,先生
您查找LinkButton
控件的方式是错误的。它应该像下面这样。同样,单元格编号0
就是一个例子。您应该填写正确的单元格号码。
LinkButton link = row.Cells[0].FindControl("btnApprove") as LinkButton;
if(link != null)
link.Text = "Approved";
假设单击链接按钮后,网格中的数据被刷新(语法可能不太正确,仍在唤醒!)
诀窍是在数据库中有一个可以再次读出的字段,显示标签可以操作的新状态。
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnApprove" runat="server" Text="<%=this.getText(Eval("some_DB_trigger_flag"));%>" CommandName="Approve" CommandArgument='<%# Eval("ProductID") %>' />
</ItemTemplate>
</asp:TemplateField>
和代码后面;
protected string getText(int value)
{
if(value==0) then return "Approved";
else return "Other";
}
您需要在数据库中保存链接的状态,然后根据状态字段的值,您需要将链接文本设置为approve
或approved
。使用相同的状态字段,您也可以将其设置为readonly
。
因此,修改数据库表并向当前审批状态添加一个新字段。
当前,您没有将其保存到数据库中,因此当您重新加载页面时,它将再次恢复到原始状态。
希望有帮助。
更新:您有两个表,即Products
和CustomerProducts
。您需要通过运行这个查询来修改表并添加一个bit类型的新列ApproveStatus
。Alter table CustomerProducts add ApproveStatus bit null
。现在,在您的选择查询中,您需要包含ApproveStatus
列并修改GridView
代码。
protected string GetApprovalStatus(object value)
{
if(Convert.ToBoolean(value))
return "Approved";
else
return "Approve";
}
修改你的GridView
模板来批准LinkButton
。
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnApprove" runat="server" Text="<%=this.GetApprovalStatus(Eval("ApproveStatus"));%>" CommandName="Approve" CommandArgument='<%# Eval("ProductID") %>' />
</ItemTemplate>
现在,您需要将这行代码更改为:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
bool status = false;
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("chkCtrl") as CheckBox);
LinkButton link = ((LinkButton)GridView1.FindControl("btnApprove"));
if (chkRow.Checked)
{
if(link.Text == "Approved")
{
status = true;
}
else
{
status = false;
}
using (SqlConnection scn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"))
{
scn.Open();
SqlCommand cmd = new SqlCommand("update o set o.Updatedproduct = p.ProductQuantity - o.Totalproduct, o.ApproveStatus = @status from CustomerProducts o inner join Products p on o.ProductID = p.ProductID WHERE o.CustomerID=@CustomerID", scn);
cmd.Parameters.AddWithValue("@CustomerID", SqlDbType.Int).Value = row.Cells[0].Text;
cmd.Parameters.AddWithValue("@status", status)
cmd.ExecuteNonQuery();
GridView1.DataBind();
}
}
}
}