How to remove href from gridview cells? - c#

I have a button click that will generate the text from gridview cells. But I want to remove the href tag when it get the text. How can I do it? Trying to do .atrributes.remove("href) but can't get it to save to a arraylist
protected void Button_Example_Click(object sender, EventArgs e)
{
foreach (GridViewRow s in GridView_Staff.Rows)
{
CheckBox CheckBox_Staff = (s.FindControl("CheckBox_Staff") as CheckBox);
if (CheckBox_Staff.Checked == true)
{
s.Cells[2].Attributes.Remove("href");
Response.Write("<script>alert('" + s.Cells[2].Text + "');</script>");
}
}
}
s.Cells[2].Text contains:
Example A

It's been a while since I was last using webforms and GridView, but to my eye it looks as though you are calling .Attributes.Remove() on the Cell object itself (.Cells[2]). But I would have thought that the anchor tag was inside the cell?
Would it not be more like this (excuse the pseudo-code):
var Anchor = s.Cells[2].FindControl("TheAnchor") as AnchorTag;
Anchor.Attributes.Remove("href");
Response.Write("<script>alert('" + Anchor.Text + "');</script>");
Or something similar?

You could use a regular expression to remove the href attribute like this:
protected void Button_Example_Click(object sender, EventArgs e)
{
foreach (GridViewRow s in GridView_Staff.Rows)
{
CheckBox CheckBox_Staff = (s.FindControl("CheckBox_Staff") as CheckBox);
if (CheckBox_Staff.Checked == true)
{
string text = s.Cells[2].Text;
string pattern = #"(?<=<[^<>]+)\s+(?:href)\s*=\s*([""']).*?\1";
text = System.Text.RegularExpressions.Regex.Replace(text, pattern, "", RegexOptions.IgnoreCase);
s.Cells[2].Text = text;
Response.Write("<script>alert('" + s.Cells[2].Text + "');</script>");
}
}
}

Related

Add multiple CommandArguments to button programmatically

I got some code to create new buttons programmatically.
foreach (DataRow dtRow in dtTable.Rows)
{
string question_id = Convert.ToString(dtRow["QUESTION_ID"]);
string question_text = Convert.ToString(dtRow["QUESTION_TEXT"]);
var btn_system = new Button
{
ID = "btn_question" + question_id,
Text = question_text,
CssClass = "quest_buttons"
};
btn_system.Command += ButtonClick_Parent;
btn_system.CommandArgument = Convert.ToString(question_id);
}
Now I would like to add multiple CommandArgument in line 12 of my code snippet. How can I do this from code behind?
Thanks in advance!
You need to pass multiple arguments as a string separating by some character and in event handler, you need to parse them. I have shown here using comma
btn_system.CommandArgument = "argument1,argument2,argument2,...";
then get this using below code
protected void ButtonClick_Parent(object sender, EventArgs e)
{
Button button = (Button)sender;
string[] commandArgs = button.CommandArgument.ToString().Split(',');
}

How to find a specific word in WebBrowser control C#

I use this code to find CDE in the HTML. How can I find my request data in tag page with different ids, for example if page id is 1 my result is CDE and when page id is 2 my result is IJK, How can I set id-value in my search?
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = #"<html><head><title></title></head><body>"
+ "<page id=\"1\">"
+ #"ABCDEF</page>"
+ "<page id=\"2\">"
+ #"GHIJKLMN</page></body></html>";
webBrowser1.DocumentCompleted += HtmlEditorDocumentCompleted;
}
void HtmlEditorDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var document = (IHTMLDocument2)((WebBrowser)sender).Document.DomDocument;
if (document != null)
{
IHTMLBodyElement bodyElement = document.body as IHTMLBodyElement;
if (bodyElement != null)
{
IHTMLTxtRange trg = bodyElement.createTextRange();
if (trg != null)
{
trg.move("character", 2);
trg.moveEnd("character", 3);
trg.select();
trg.pasteHTML("<font color=#FF0000><strike>" + trg.text + "</strike></font>");
}
}
}
}
I have no idea, what do you need to do, you may have to rephrase the question...
But you might want to have a look at webBrowser1.Document.GetElementDyId() to get a specific element by "id"

Getting checkbox tags (in a panel) to new form

Rather new to C# so please forgive me if i am missing something simple or am trying to just do this the wrong way.
I am creating another form to compliment my main form and it needs to pull some of the information from Main form on button click of Second form. The information on the Main for is stored in checkboxes and textboxes.
I have the textboxes working fine but cannot figure out how to pull the checkboxes tag data over along with the formatting. Main Form is working fine as is except I cannot figure out how to bring the checkbox data over as well.
This is the code i currently use to display the checkbox TAG data on my main form.
//Statements to write checkboxes to stringbuilder
string checkBoxesLine = "\u2022 LIGHTS ";
foreach (Control control in pnlCheckBoxes.Controls)
{
if (control is CheckBox)
{
CheckBox checkBox = (CheckBox)control;
if (checkBox.Checked && checkBox.Tag is string)
{
string checkBoxId = (string)checkBox.Tag;
checkBoxesLine += string.Format("{0}, ", checkBoxId);
}
}
}
This is the button i am using to open the new form and move the checkbox tag data and textbox.text data to the new form.
private void code_blue_link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
trouble_shooting = tshooting_text.Text;
services_offered = services_offered_text.Text;
other_notes = other_notes_text.Text;
cust_modem = cust_modem_text.Text;
//Opens CODE BLUE form and pushes text to it.
code_blue_form CBForm = new code_blue_form();
CBForm.cust_name_cb = cust_name_text.Text;
CBForm.cust_cbr_cb = cust_callback_text.Text;
CBForm.cust_wtn_cb = cust_btn_text.Text;
CBForm.cust_notes_cb = cust_modem + "\r\n" + trouble_shooting + "\r\n" + services_offered + "\r\n" + other_notes;
CBForm.Show();
}
Here is my code for the Second form and how i am getting the information to populate textboxes on that form.
public partial class code_blue_form : Form
{
public string cust_name_cb;
public string cust_wtn_cb;
public string cust_cbr_cb;
public string cust_notes_cb;
public string cust_modem_cb;
public code_blue_form()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
cb_name_text.Text = cust_name_cb;
cb_wtn_text.Text = cust_wtn_cb;
cb_cbr_text.Text = cust_cbr_cb;
cb_notes_text.Text = cust_notes_cb;
}
}
}
Please forgive the long post! Any ideas/direction on this would be greatly appreciated. Thanks!
I am not going to answer straight away using ur code. I find code smell. If I were you I would do this (but then if you are adamant about going with the same design, then you can tweak my code accordingly, no big deal, the bottom line is you get the idea how to do):
void code_blue_link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var checkBoxIds = GetCheckBoxIds();
cust_modem = cust_modem_text.Text;
trouble_shooting = tshooting_text.Text;
services_offered = services_offered_text.Text;
other_notes = other_notes_text.Text;
//take off underscore convetion and replace with camelCase convention.
CodeBlueForm cbForm = new CodeBlueForm(checkBoxIds, cust_name_text.Text,
cust_callback_text.Text,
cust_btn_text.Text,
cust_modem + "\r\n" +
trouble_shooting + "\r\n" +
services_offered + "\r\n" +
other_notes);
cbForm.Show();
}
private List<int> GetCheckBoxIds()//even better naming required like GetFruitIds()
{ //(whatever the id is of) or even better Tag
//checkBoxes with the Fruit object iself and not ids.
List<int> checkBoxIds = new List<int>(); //So you can call GetFruits();
foreach (Control control in pnlCheckBoxes.Controls)
{
if (control is CheckBox)
{
CheckBox checkBox = (CheckBox)control;
if (checkBox.Checked && checkBox.Tag is int) //tag them as ints.
checkBoxIds.Add((int)checkBox.Tag); //I hope ids are integers.
}
}
return checkBoxIds;
}
public partial class CodeBlueForm : Form
{
List<int> checkBoxIds = new List<int>():
string cust_cbr_cb; //should be private.
string cust_name_cb;
string cust_wtn_cb;
string cust_notes_cb;
string cust_modem_cb;
public CodeBlueForm(List<int> ids, string cust_name_cb, string cust_wtn_cb,
string cust_notes_cb, string cust_modem_cb)
{
InitializeComponent();
this.checkBoxIds = ids;
this.cust_name_cb = cust_name_cb;
this.cust_wtn_cb = cust_wtn_cb;
this.cust_notes_cb = cust_notes_cb;
this.cust_modem_cb = cust_modem_cb;
}
private void button1_Click(object sender, EventArgs e)
{
cb_name_text.Text = cust_name_cb;
cb_wtn_text.Text = cust_wtn_cb;
cb_cbr_text.Text = cust_cbr_cb;
cb_notes_text.Text = cust_notes_cb;
string checkBoxesLine = "\u2022 LIGHTS ";
// if you dont require a lot of string formatting, then just:
checkBoxesLine += string.Join(", ", checkBoxIds);
// or go with your classical:
//foreach (int id in checkBoxIds)
// checkBoxesLine += string.Format("{0}, ", checkBoxIds);
//and do what u want with checkboxline here.
}
}

Highlight gridview row in update panel without posting back

I have a gridview in an update panel with the following code to select a row, this in turn updates another updatepanel with details from the form record.
protected void gvMainGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Make the entire row clickable to select this record
//Uses javascript to post page back
e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(this.gvMainGrid, "Select$" + e.Row.RowIndex));
}
}
I am manually binding the gridview from a database and don't want to rebind the grid just to highlight the row, but I can't seem to add any javascript to the onclick event, it seems to either show the GetPostBackClientHyperlink or the row highlight javascript.
How to highlight gridview when row is selected
for this you have to write this code in your code behind file in OnRowCreated event or your can also write this code in OnRowDataBound event of grid...
protected void ctlGridView_OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "onGridViewRowSelected('" + e.Row.RowIndex.ToString() + "')");
}
}
and add this one script
<script language="javascript" type="text/javascript">
var gridViewCtlId = '<%=ctlGridView.ClientID%>';
var gridViewCtl = null;
var curSelRow = null;
function getGridViewControl()
{
if (null == gridViewCtl)
{
gridViewCtl = document.getElementById(gridViewCtlId);
}
}
function onGridViewRowSelected(rowIdx)
{
var selRow = getSelectedRow(rowIdx);
if (curSelRow != null)
{
curSelRow.style.backgroundColor = '#ffffff';
}
if (null != selRow)
{
curSelRow = selRow;
curSelRow.style.backgroundColor = '#ababab';
}
}
function getSelectedRow(rowIdx)
{
getGridViewControl();
if (null != gridViewCtl)
{
return gridViewCtl.rows[rowIdx];
}
return null;
}
</script>
and it will highlight the selected row..
I was struggling to add both on click events to the row databound:
e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(this.gvMainGrid, "Select$" + e.Row.RowIndex));
e.Row.Attributes.Add("onclick", "onGridViewRowSelected('" + e.Row.RowIndex.ToString() + "')");
Appending the PostBack select after the row highlight method with ';' seems to have worked.
e.Row.Attributes.Add("onclick", "onGridViewRowSelected('" + e.Row.RowIndex.ToString() + "');" + ClientScript.GetPostBackClientHyperlink(this.gvMainGrid, "Select$" + e.Row.RowIndex));
Firstly, you can't apply text-decoration to a <tr>... or a <td> for that matter. You need to apply it to the elements inside.
Here are a couple adjustments you can try-
e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand';";
e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(this.gvMainGrid.ClientId, "Select$" + e.Row.RowIndex));
The 1st works for me in code. Didn't have anything handy to test the 2nd.

How to add a Hyperlink to a dynamic gridview column

I have an issue hope someone can help.
I have a dynamic Gridview. I need to have a hyperlink on gridview column. These hyperlink should open a popup to display certain data on clicking.
I tried this by having a dynamic template field . But even on binding the data , I'm unable to get the hyper link for the column. I'm able to get the data but not the hyperlink.
This is the HyperLinkTemplate class which is implementing ITemplate.
public class HyperLinkTemplate : ITemplate
{
private string m_ColumnName;
public string ColumnName
{
get { return m_ColumnName; }
set { m_ColumnName = value; }
}
public HyperLinkTemplate()
{
//
// TODO: Add constructor logic here
//
}
public HyperLinkTemplate(string ColumnName)
{
this.ColumnName = ColumnName;
}
public void InstantiateIn(System.Web.UI.Control ThisColumn)
{
HyperLink HyperLinkItem = new HyperLink();
HyperLinkItem.ID = "hl" + ColumnName;
HyperLinkItem.DataBinding += HyperLinkItem_DataBinding;
ThisColumn.Controls.Add(HyperLinkItem);
}
private void HyperLinkItem_DataBinding(object sender, EventArgs e)
{
HyperLink HyperLinkItem = (HyperLink)sender;
GridViewRow CurrentRow = (GridViewRow)HyperLinkItem.NamingContainer;
object CurrentDataItem = DataBinder.Eval(CurrentRow.DataItem, ColumnName);
HyperLinkItem.Text = CurrentDataItem.ToString();
}
}
I'm not entirely sure that I understand what you are trying to accomplish, but I don't think that you should have to build your own template class for this.
You might mean something other than what I'm thinking by the term "dynamic gridview", but if you need to add a hyperlink to each row of a column in a GridView, and if you need to do this in the code-behind, then I would suggest handling the GridView's RowDataBound event and doing something like the following in the event handler:
protected void grdData_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink link = new HyperLink();
link.Text = "This is a link!";
link.NavigateUrl = "Navigate somewhere based on data: " + e.Row.DataItem;
e.Row.Cells[ColumnIndex.Column1].Controls.Add(link);
}
}

Categories