HTML tags in Content - asp.net

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;
}
}

Related

Changing the style of specific records in GridView

I'm designing a logistics system in ASP.Net . In the Order processing page, orders are displayed by Grid View,i want to change the font style of the rows to BOLD, which are marked as"ordedr not processed". thanks.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string OrStatus = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Orderstatus"));
if (OrStatus == "Order not processed")
{
//You can use whatever you want to play with rows
e.Row.Cells[0].Font.Bold = true;
e.Row.Cells[2].CssClass = "gridcss";
}
}
}
Follow that code. It will helps
You can do this in "rowdatabound" event of grid.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridView grid = GridView1;
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
string orderstatus= Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Orderstatus"));
if(orderstatus=="Order not processed)
{
//write your code to change css
}
}
}

Finding Column value in GridView

I would like to get the column (Description) from DataRowView.I have the following code.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem != null)
{
DataRowView dataRowView = ???????????
string description = dataRowView["Description"] as string;
}
}
How to replace ?????????? with appropriate code?
Use (DataRowView)e.Row.DataItem;

Data transfer from gridview column of one page to label of another page in C# where the gridview is autogenerated

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var firstCell = e.Row.Cells[0];
firstCell.Controls.Clear();
firstCell.Controls.Add(new HyperLink { NavigateUrl = "ser_job_status1.aspx?Complaint_No = " + firstCell.Text, Text = firstCell.Text, Target = "_blank" });
}
}
}
I have used this to include hyperlink in my gridview.If that link is clicked it may take me to ser_job_status1.aspx where Complaint_No is my table field name.
is it correct...
If correct how do i get that Complaint_No value in the Label field of my second page..
Kindly help me to figure it out...
I use C# in code behind
In ser_job_status1.aspx page:
string strComplaintNo=Request.Querystring["Complaint_No"];
label1.text=strComplaintNo
or
Use Session rowdatabound page
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var firstCell = e.Row.Cells[0];
Session["ComplaintNo"]=firstCell.text;
firstCell.Controls.Clear();
firstCell.Controls.Add(new HyperLink { NavigateUrl = "ser_job_status1.aspx?Complaint_No = " + firstCell.Text, Text = firstCell.Text, Target = "_blank" });
}
}
}
In ser_job_status1.aspx page:
label1.text=convert.tostring(Session["ComplaintNo"]);

How do I get the value of a gridview Cell?

How can I get the value of a gridview cell? I have been trying the code below with no luck.
protected void grvExpirations_RowDataBound(object sender, GridViewRowEventArgs e) {
int test = Convert.toInt32(e.Row.Cells[5].text;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if(e.Row.RowType == DataControlRowType.DataRow)
{
string LinkText = (string)System.Web.UI.DataBinder.Eval(e.Row.DataItem, "RegistrationLinkText");
if(LinkText == "text")
{
e.Row.Cells[3].Text = "your text";
}
}
}
protected void grvExpirations_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int test = Convert.toInt32(e.Row.Cells[5].Text);
}
}
use this code below,
protected void grvExpirations_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int test = Convert.ToInt32(Server.HtmlDecode(e.Row.Cells[5].Text.Trim()));
}
}
and please remember cell index number starts from 0
If you use the above methods you will get the value of cell[5] for the last row only.
So if you are specific about a certain row I think you can get the value from any other gridview event handlers.

gridview arrange strings data right

1)Is it possible to arrane strings to the right?
2)Is it possible in case The value of the field is null to have "-" in the corresonding gridview field
protected void Page_Load(object sender, EventArgs e)
{
GridView1.Columns[2].ItemStyle.HorizontalAlign = HorizontalAlign.Right;
}
void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Due to the fact that DBnull value by default would be just empty string
e.Row.Cells[1].Text = (string.IsNullOrEmpty(e.Row.Cells[1].Text))?"-":e.Row.Cells[1].Text;
}
}

Resources