启用和禁用 gridview 上的链接按钮 [英] Enable and disable link button on gridview
问题描述
我想根据条件在网格视图的某些行上启用或禁用链接按钮.我可以在一行上启用链接按钮并在同一网格视图的另一行上禁用它吗?我的代码在这里
I wants to enable or disable linkbutton on some rows of gridview based on condition.. Can i enable linkbutton on one row and disable it on another row of same grid view ??my code is here
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{LinkButton lnk2 = (LinkButton)e.Row.FindControl("LinkButton2");
if (e.Row.RowType == DataControlRowType.DataRow)
{
SqlCommand cmd12 = new SqlCommand("Select testsession_status from student_vs_testsession_details where testsession_id='" + v_testid.Text + "' ", con12);
SqlDataReader dr12 = cmd12.ExecuteReader();
while (dr12.Read())
{
string test_status = dr12[0].ToString();
LinkButton lnk2 = (LinkButton)e.Row.FindControl("LinkButton2");
foreach (GridViewRow row in GridView1.Rows)
{
if (v_testtype == "Theory Test" && test_status == "Completed")
{
lnk2.Visible = true;
}
else
{
lnk2.Visible = false;
}
}
}
推荐答案
是的,您可以在 RowdataBound Event 中轻松完成,但是您在代码中使用了 lnk2.Visible
属性.
Yes you can easily do it in RowdataBound Event, but you have used lnk2.Visible
property in your code.
您可能将 Visible
属性用于其他要求,但只是想确认它仅用于显示/隐藏链接按钮.要启用/禁用链接按钮,请使用链接按钮的 Enabled
属性.如:
you may be using Visible
property for another requirement but just want to confirm you that it is used to show/hide the Linkbutton only. To enable/disble a Linkbutton, use Enabled
property of Linkbutton. as:
lnk2.Enabled = true;// to enable linkbutton.
lnk2.Enabled = false;// to disable linkbutton.
如果你想用rowindex来做,那么你可以e.Row.RowIndex
在gridview的'RowDatabound`事件中找到当前的行索引.如:
If You want to do it using rowindex, then you can e.Row.RowIndex
to find the current row index inside 'RowDatabound` event of gridview. as:
if(e.Row.RowIndex==2)
{
LinkButton lnk2 = (LinkButton)e.Row.FindControl("LinkButton2");
lnk2.Enabled=false;
}
如果您想根据同一行中其他列的值启用/禁用链接按钮,那么您可以在 Rowdatabound
事件中执行相同的操作.如:
If you want to enable/ disable Linkbutton based on value of some other column in the same row, then you can do the same inside Rowdatabound
event. as:
string Namecolumnvalue = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Name"));
LinkButton lnk2 = (LinkButton)e.Row.FindControl("LinkButton2");
if(Namecolumnvalue =="Disable")
{
lnk2.Enabled=false;
}
else{
lnk2.Enabled=true;
}
这篇关于启用和禁用 gridview 上的链接按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!