string search= textbox1.text;
protected void grd_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach(TableCell tc in e.Row.Cells)
{
tc.Text = tc.Text.Replace(search, "<span style='color:Red;'>" + search + "</span>");
}
}
}
I'm using that code to highlight a word searched but when I Debug in browser buttons:edit,select,delete are not there .and if i deleted the event the buttons are back.
what shall I do?
One way to do this would be to check and see if the cell contains controls... It looks like the rest of your grid view cells just contain text and you are appending the span into the cell to highlight the search value.
string search= textbox1.text;
protected void grd_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach(TableCell tc in e.Row.Cells)
{
if (tc.Controls.Count == 0){
tc.Text = tc.Text.Replace(search, "<span style='color:Red;'>" + search + "</span>");
}
}
}
}
This will bypass the cells that contain the select, edit and delete controls. And also text boxes and labels and things like that as well. Hope this helps.
Related
I have a grid view ,i use the row command to open a specific window with several parameters.
Now i want to remove this button and make the whole row clickable so if i click on the row open the same window .How to do that .
protected void gv_Details1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Confirm")
{
int index = Convert.ToInt32(e.CommandArgument);
Session["task_code"] = ((HiddenField)gv_Details1.Rows[index].Cells[0].FindControl("hf_tc")).Value;
Session["trans_serial"] = ((HiddenField)gv_Details1.Rows[index].Cells[0].FindControl("hf_ts")).Value;
MasterPage2 obj = (MasterPage2)Page.Master;
obj.SetMainVariables();
obj.PreValidate();
obj.SetDepartment();
Response.Redirect("~/Contents/" + Session["page_new"].ToString() + ".aspx", false);
}
}
Add RowDatabound event :
protected void gv_Details1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("style", "cursor:pointer;");
string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
e.Row.Attributes["onClick"] = "location.href='pagename.aspx?Rumid=" + abc + "'";
}
}
Thanks :
Curtsy : http://forums.asp.net/t/1859787.aspx/1?How+to+fire+RowCommand+event+when+user+click+anywhere+in+the+gridview+row
two of the best links of our own stack overflow community.
How to implement full row selecting in GridView without select button?
Making an entire row clickable in a gridview
protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Product p = (e.Row.DataItem as Product);
e.Row.Attributes.Add("onClick", "location.href='somepage.aspx?id=" + p.ProductID + "'");
}
}
I have a Gridview and on rowDatabound i am creating on click on that row and
showing modal popup. what i want is when the row is clicked , the ID for that row should be passed.
protected void grdOrderProduct_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
e.Row.Attributes.Add("style", "cursor:pointer;");
e.Row.Attributes["onClick"] = Page.ClientScript.GetPostBackClientHyperlink(btnPop, "12");
}
}
How can i Get this "12" on my server control. i have put 12 as static for demo. but it will change.
you also can do like this
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink
(this.GridView1, "Select$" + e.Row.RowIndex);
To know which row was clicked, you have to use the Row property of the GridViewRowEventArgs
protected void grdOrderProduct_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
e.Row.Attributes.Add("style", "cursor:pointer;");
e.Row.Attributes["onClick"] = Page.ClientScript.GetPostBackClientHyperlink(btnPop, e.Row.RowIndex.ToString());
}
}
RowIndex would be passed as an argument to btnPop. In order to receive it btnPop should be implementing IPostBackEventHandler.
Like this:
public class MyControl : Button, IPostBackEventHandler
{
// Use the constructor to defined default label text.
public MyControl()
{
}
// Implement the RaisePostBackEvent method from the
// IPostBackEventHandler interface.
public void RaisePostBackEvent(string eventArgument)
{
//You receive the row number here.
}
}
Refer ClientScriptManager.GetPostBackClientHyperlink
I am using a dropdownlist in footer row of gridview(ASP.Net) and I fill that on rowdatabound event,first time it works fine but when the form is postbacked,dropdown gets cleared.
It can be solved by refilling it on each postback,but I want that only single time code binding call fulfill my need,
means is there any way to stop from being null on postback.
looking for your kind solutions and suggestions
Thnx in advance......
Supriya
Code:
protected void gvProjects_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (gvProjects.Rows.Count > 0 && e.Row.RowIndex != -1)
{
string proj_Id = Convert.ToString(gvProjects.DataKeys[e.Row.RowIndex].Value);
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlProject = (DropDownList)e.Row.FindControl("ddlProject");
if (ddlProject != null && ddlProject.Items.Count == 0)
{
objMaster.FillProjects(ddlProject);
ddlProject.SelectedValue = proj_Id;
}
}
}
}
catch (Exception excep)
{
lbl_msg.Text = excep.Message;
}
}
It's called whenever the grid is binded,can it be avoided.
With this code you will avoid filling the dropdownlist located in the gridview footer in each rowdatabound:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Footer)
{
//do the binding for the normal rows
}
}
As you can see, the rowdatabound will be only executed on the header and normal rows but not in the footer.
Hope this helps!
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDropdown();
}
}
I am using a gridview in aspx page. I am adding Textboxes dynamically to the griview header. How can I insert a value to the textbox in the grid header.
My code goes like
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
TextBox txtBox1 = default(TextBox);
for (int i = 0; i <= (e.Row.Cells.Count - 1); i++)
{
litHeader.Text = e.Row.Cells[i].Text + "<br/>";
e.Row.Cells[i].Controls.Add(litHeader);
e.Row.Cells[i].Controls.Add(txtBox1);
}
}
}
I am trying to bind a value to the textbox 'txtBox1' using the code
private void FillGridView()
{
TextBox txt;
txt = (TextBox)GridView1.HeaderRow.FindControl("txtBox1");
txt.Text = dtValues.Rows[0][5].ToString();
}
But I am not able to fill the textbox. Please help.
This may help someone on very old question. This how I update my header row text box immediately after I insert data to GV1. Hope it helps.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
if (e.Row.RowType == DataControlRowType.Header)
{
//((TextBox)GridView1.HeaderRow.FindControl("tbProjNumInsert")).Text
if (newMaxNum != null && newMaxNum.Length > 0)
{
((TextBox)e.Row.Cells[4].FindControl("tbProjNumInsert")).Text = newMaxNum;
System.Diagnostics.Debug.WriteLine("GridView1_RowDataBound()...GridView1.Width !!!! " + ((TextBox)e.Row.Cells[4].FindControl("tbProjNumInsert")).Text + "........newMaxNum:" + newMaxNum);
}
}
}
I've created a gridview dynamically, now i want to fire an event when the selectedindex has changed.
GridView NewDg = new GridView();
NewDg.ID = "SubGridView" + e.Row.RowIndex.ToString();
NewDg.DataKeyNames = new string[]{"logentry_id"};
NewDg.SelectedIndexChanged += new EventHandler(NewDg_SelectedIndexChanged);
NewDg.RowDataBound += new GridViewRowEventHandler(NewDg_RowDataBound);
The RowDataBound works but it doesn't produce the right postback URL i guess.
In the RowDataBound i have the following code:
GridView sendingGridView = (GridView)sender;
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
this produces the following code:
javascript:__doPostBack('SubGridView4','Select$0')
Only this doesn't lead to the postback to this function:
void NewDg_SelectedIndexChanged(object sender, EventArgs e)
{
GridView sendingGridView = (GridView)sender;
ViewDetails(Convert.ToInt32(sendingGridView.SelectedDataKey["logentry_id"].ToString()));
}
Does anyone know what i'm doing wrong?
First, you are recreating the grid on every page load? This is a requirement for creating the grid this way. Second, try tapping into RowCommand, and looking for the command name that way; maybe that will fire successfully; you get a reference to the command through the command argument as in :
void rowcmd(..) {
if (e.CommandName != null && e.CommandName.StartsWith("Select")) {
//Dothis
}
}
I found the answer to my question on Code Project:
I now use a gridview in my gridview
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="SubGridView"
Because of the extender on GridView, The gridview will be displayed when I click on the plus sign (see link)
on page load i perform the following:
protected void Page_Load(object sender, EventArgs e)
{
GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
GridView1.DataSource = dc.GetLogEntriesWithUsername();
GridView1.DataBind();
I already have a DataBound and a Selected Index Changed event on this gridview.
On the row created event i perform the following:
void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView SubGridView = e.Row.FindControl("SubGridView") as GridView;
List<GetLogEntriesWithUsernameByParentIdResult> subLogEntries = dc.GetLogEntriesWithUsernameByParentId(((GetLogEntriesWithUsernameResult)e.Row.DataItem).logentry_id).ToList();
if (subLogEntries.Count > 0)
{
SubGridView.DataSource = subLogEntries;
SubGridView.DataBind();
(e.Row as ExtGridViewRow).ShowExpand = SubGridView.Rows.Count > 0;
}
}
}
On the subgridview i also have a DataBound and a SelectedIndex Changed event. This works now!
I use this DataBound event:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{ // only apply changes if its DataRow
GridView sendingGridView = (GridView)sender;
if (e.Row.RowType == DataControlRowType.DataRow)
{
// when mouse is over the row, save original color to new attribute, and change it to highlight yellow color
e.Row.Attributes.Add("onmouseover",
"this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#C0C0C0';this.style.cursor='pointer';");
// when mouse leaves the row, change the bg color to its original value
e.Row.Attributes.Add("onmouseout",
"this.style.backgroundColor=this.originalstyle;this.style.cursor='cursor'");
//e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex));
e.Row.Cells[1].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
e.Row.Cells[2].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
e.Row.Cells[3].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
e.Row.Cells[4].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
e.Row.Cells[5].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
And the selected index changed event:
protected void GridView_SelectedIndexChanged(object sender, EventArgs e)
{
GridView sendingGridView = (GridView)sender;
ViewDetails(Convert.ToInt32(sendingGridView.SelectedDataKey["logentry_id"].ToString()));
}
The ViewDetails function show the details of the selected logentry in a different div.
Now i'm busy on the final step, that is keep showing the data as it was before i clicked on a row.
Thanks for the help, but this is the solution to my problem.