GridView : Template field - asp.net

is it possible to create a template field with link option using codebehind.

Yes!
Append handler OnRowDataBound
MyRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Controls.Add(new MyControl(...

Related

Add tooltip to only some cells of Grid View

I need to add a tooltip to a TemplateField in a GridView, but only to Row that meets some condition. How can I achieve that?
protected void GridView1_DataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// check for condition
if (e.Row.Cells[0].Text.Contains("sometext"))
{
e.Row.ToolTip = "Text to be shown on mouseover of the row";
}
}
}
Use the row databound which will hit each row as it is created. Then inside use .findcontrol to get whatever control it is that you are adding a tooltip to.
Then assign the tooltip.
Wrap this in whatever condition you want to use.
If you post your current code then I may be able to help further.
try this,
This event through you can check your all gridview rows.
gridview row event Name is =OnRowDataBound : "GridView1_RowDataBound"
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Add Your Condition here
}
}

Adding/using Attributes for ASP.Net controls

I was wondering where can we get a list of attributes that we can add to an ASP.NET control.
An example is the following block of code from :
protected void Grid1_OnItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
((CheckBox)item["TestColumn"].Controls[0]).Attributes.Add("onClick","javascript:hi();");
}
}
The following code couldn't work which I figure could be due to 'onClick' not applicable to Checkbox ASP.NET control.
Hence, is there a place we can refer to for attributes where we can add to each ASP.NET control? And also for the following code:
Attributes["click"] = abc();
Thanks.
Attributes must be added on RowCreated event of GridView. Please check below.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Dummy Code
Button btnButton =
(Button)e.Row.Cells[4].FindControl("btnButton");
btnButton.Attributes.Add("onmouseover",
"this.className='actionH'");
}
}

HTML tags in Content

I'm implementing full text search on a table named as tbljobs on the cloumn named as jobdescription. In front end I'm getting html tags in the description. I'm showing the records in GridView and on Gridview's RowDataBound enent I'm decoding the text. I'm using the following code on Gridview's RowDataBound event:
protected void GridNewlyPostedJobs_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string decodedText = HttpUtility.HtmlDecode(((Label)e.Row.FindControl("lblJobDescription")).Text);
((Label)e.Row.FindControl("lblJobDescription")).Text = decodedText;
}
}
But nothing works..!!
Use DataRowView to get the Data...
protected void GridNewlyPostedJobs_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView rowView = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
string decodedText = HttpUtility.HtmlDecode(rowView["jobdescription"]);
((Label)e.Row.FindControl("lblJobDescription")).Text = decodedText;
}
}

gridview on select row event

How do I do a grdiview on select row event? On the source page, I added
OnSelectedIndexChanged="grdTanks_OnSelectRow"
in the code behind, I put the function
protected void grdTanks_OnSelectRow(Object sender, GridViewCommandEventArgs e)
{
}
When I try to do it that way, I get No overload for grdTanks_OnSelectRow matches delegate 'System.EventHandler'
If I change the GridViewComandEventArgs to EventArgs, then it won't allow me to do
if (e.CommandName == "Select")
anybody know how to do a OnSelectRow event for a gridview? Thanks
I also added this code:
protected void grdTanks_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex != -1)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.background='#3260a0';;this.style.color='white'";
if (e.Row.RowIndex % 2 == 1)
{
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.background='white';this.style.color='black'";
}
else
{
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.background='#bEc8bE';this.style.color='black'";
}
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdTanks, "Select$" + Convert.ToString(DataBinder.Eval(e.Row.DataItem, "CargoTankID")));
}
}
}
You could try this:
In the page_load, enter
grdTanks.SelectedIndexChanged +=
and press tab twice. Visual Studio automatically generates the handler for you. The second param would be EventArgs
I think you have to change
protected void grdTanks_OnSelectRow(Object sender, GridViewCommandEventArgs e)
To
protected void grdTanks_SelectedIndexChanged(Object sender, GridViewCommandEventArgs e)
In your code behind
I am not sure what you want to do in this event handler. You are already handling for select event and I think, no need to check again for (e.CommandName == "Select"). (MSDN:The SelectedIndexChanged event is raised when a row's Select button is clicked).
The error said no overload for event and you have to use EventArgs argument.
protected void grdTanks_OnSelectRow(Object sender, EventArgs e)
{
// May be you want like..
// Get the currently selected row using the SelectedRow property.
GridViewRow row = YourGridViewID.SelectedRow;
}

Change Field to Checkbox inside GridView

I am trying to change fields to a Checkbox inside a GridView.
I currently create Grid Columns Dynamically based on a query and some of the columns I want to change to a checkbox so it can be checked/unchecked by the user. I know I cant do this via .aspx page using but I am trying to stay away from statically creating the fields.
Any help would be great.
Make use of GridView's RowDataBound Event. Thus you can add any control to your GridView.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk1 = new CheckBox();
chk1.ID = "chkbox1";
e.Row.Cells[0].Controls.Add(chk1);
}
}
Edit for Comment:
Once you passed the values from database to gridview (out of scope for this question), You can access the values using e.Row.Cells[i].Text where "i" is the row.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txt1 = new TextBox();
txt1.Text = e.Row.Cells[0].Text;
e.Row.Cells[0].Controls.Add(txt1);
}
}

Resources