2014年7月13日 星期日

[RESOLVED] Dynamically set visibility of ButtonField and RowCommand


Hello,


Is it possible to dynamically set the visibility of a ButtonField based upon a function and still be able to use the Gridview's RowCommand functionality?  If I can't, do I need to use a Template field with a button inside it?  If I need to go with the TemplateField
route, how can that particular button access the Gridview's RowCommand event?


Thanks!



hi, you can set the visibility of button field like below in grid_rowdatabound event,


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//here cells[0] represents firt column in grid and ButtonField ButtonType=Button
(e.Row.Cells[0].Controls[0] as Button).Visibel=false;
}
}
or you can also use TemplateField with you button and to handle in Grid_RowCommand use CommandName in button like below







protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Add"))
{
//your code
}
}

based on you condition you can either enable or disable the templatefield button


ButtonField does not allow access to individual LinkButtons it generates. So if the condition is different for each Row, you will need either code-behind to work on the generated LinkButton in the Cell of the Row, or a TemplateField. If you use TemplateField
you may be able to use a DataBinding expression on the Visible attribibute to keep it all without code-behind.


A LinkButton in a TemplateField can have CommandName and CommandArgument set. You can even set these with the built-in commands. In that case, the CommandArgument will need to be set to the RowIndex of the Row. You can do that by setting a DataBinding expression
to cast Container to GridViewRow, and then taking the RowIndex of that.



How would you grab a hold of the LinkButton? I need to set the Text values and Command Arg dynamically...


 


protected void gvPlacements_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
bool isActive = (bool)DataBinder.Eval(e.Row.DataItem, "IsActive");
if (isActive)
{
e.Row.Cells[9].BackColor = System.Drawing.Color.LightGreen;
e.Row.Cells[9].Text = "Activated";
e.Row.Cells[10].BackColor = System.Drawing.Color.LightGray;
//e.Row.Cells[10].Text = "Deactivate?";
gvPlacements.FindControl("LinkBtnActuation")
}
else
{
e.Row.Cells[9].BackColor = System.Drawing.Color.Yellow;
e.Row.Cells[9].Text = "Deactivated";
e.Row.Cells[10].BackColor = System.Drawing.Color.LightGray;
//e.Row.Cells[10].Text = "Reactivate?";
}
}
}





onclick="LinkBtnActuation_Click">Actuation




 



nevermind...

 

                

    LinkButton lbtn = ((LinkButton)e.Row.FindControl("LinkBtnActuation"));
lbtn.Text = "Deactivate?";



How about


grid.Columns[?].Visible = true/false


?


(Assuming grid is the ID of your GridView.)



 



 


沒有留言:

張貼留言