selectedindexchanged on dynamically created gridview - c#

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.

Related

How to make GridView Clickable and not Clickable when a button is clicked in C#?

I want to make my GridView Clickable or Not if a certain Button was clicked. Any idea how can I do it?
Here is my Code-Behind:
protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(grdRecentCases, "Select$" + e.Row.RowIndex);
}
}

how to create button click event in gridview RowDataBound c# asp.net

i am trying to create button and click event in rowdatabound in gridview c# asp.net like below code
protected void btnerror_Click(object sender, EventArgs e)
{
GridView gv = new GridView();
gv.RowDataBound += gv_RowDataBound;
gv.RowCommand += gv_RowCommand;
gv.RowCreated += gv_RowCreated;
gv.EnableViewState = true;
gv.DataSource = _dt;
gv.DataBind();
}
void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton btnUpdate = new ImageButton();
btnUpdate.ID = "btnupdate";
btnUpdate.ImageUrl = "~/SmartAdmin/Images/update.png";
btnUpdate.ToolTip = "Click Update";
btnUpdate.CommandName = "update";
btnUpdate.Click += btnUpdate_Click;
TableCell tc = new TableCell();
tc.Controls.Add(btnUpdate);
e.Row.Cells.Add(tc);
}
}
void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "update")
{
}
}
while click that button click event is not firing ...
where i made error...
thank u.......
Do you really want to create the GridView manually? I strongly doubt that. Instead add it declaratively to the aspx-page and make it visible in btnerror_Click.
Don't create the control dynamically and register the event handler in RowDataBound but in RowCreated which is triggered on every postback (as opposed to RowDataBound):
void gv_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton btnUpdate = new ImageButton();
btnUpdate.Click += btnUpdate_Click;
TableCell tc = new TableCell();
tc.Controls.Add(btnUpdate);
e.Row.Cells.Add(tc);
}
}
void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton btnUpdate = (ImageButton)e.Row.FindControls("btnupdate")
btnUpdate.ID = "btnupdate";
btnUpdate.ImageUrl = "~/SmartAdmin/Images/update.png";
btnUpdate.ToolTip = "Click Update";
btnUpdate.CommandName = "update";
}
}
So create it in RowCreated but initialize it in RowDataBound where you can also access the datasource (if required).
Also note that you should DataBind the GridView only if(!IsPostBack) not on every postback (if that's the case). So add this check where you assign the datasource.
You need to add function for click event
btnUpdate.Click += btnUpdate_Click;
protected void btnUpdate_Click(object sender, EventArgs e)
{
}

RowEditing is fired when update button is clicked in GridView

I have created a GridView as below:
protected void Page_Load(object sender, EventArgs e)
{
GridView gv = new GridView();
gv.ID = pId.ToString();
gv.AutoGenerateEditButton = true;
gv.DataKeyNames = ids;
gv.RowEditing += gv_RowEditing;
gv.RowUpdating += gv_RowUpdating;
bindGv(pId, gv);
}
I have also written the following methods:-
RowUpdating:
void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView gv = sender as GridView;
GridViewRow row = (GridViewRow)gv.Rows[e.RowIndex];
ProductCategory pc = context.ProductCategories.First(s => s.Name ==gv.ID );
TextBox txtName = row.FindControl("txtName") as TextBox;
pc.Name = txtName.Text;
}
RowEditing:
void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView gv = sender as GridView;
gv.EditIndex = e.NewEditIndex;
bindGv(Convert.ToInt32(gv.ID), gv);
}
But when I ran the codes in debugging mode, clicking on the update button invokes gv_RowEditing method instead of gv_RowUpdating. What is the problem?
The Sequence of firing the events of gridview when you click on edit button to update any record it calls rowediting event and after that when you click on update button it calls rowupdating event.
rowediting event always invokes first then rowupdating.

How to replace the row command event for a gridview with clickable manner?

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 + "'");
}
}

How to create button in dynamically created Gridview?

I'm using AJAX Control Toolkit to create Tabpanels. Each panel is populated with a gridview as per below code.
Now, I want to add one button per each row. When it is clicked it should pass as parameter one of the cells of that row, but as the Gridview is dynamically created, I don't know how. Any tips?
foreach (DataTable dt in DataSet1.Tables)
{
GridView gv = new GridView();
var thepanel = new AjaxControlToolkit.TabPanel();
gv.DataSource = dt;
gv.DataBind();
thepanel.Controls.Add(gv);
TabContainer.Controls.Add(thepanel);
}
You can add a select button to your grid as follows:
Gridview1.AutoGenerateSelectButton=true;
I just found a solution for whom this may interest:
First, you should include fllwg lines BEFOREthe databind:
gv.RowDataBound += gv_RowDataBound;
gv.RowCommand += gv_RowCommand;
Then define the RowDataBound to insert the Linkbutton:
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton butIgnorar = new LinkButton()
{
CommandName = "Ignorar",
ID = "butIgnorar",
Text = "Ignorar",
//optional: passes contents of cell 1 as parameter
CommandArgument = e.Row.Cells[1].Text.ToString()
};
//Optional: to include some javascript cofirmation on the action
butIgnorar.Attributes.Add("onClick", "javascript:return confirm('Are you sure you want to ignore?');");
TableCell tc = new TableCell();
tc.Controls.Add(butIgnorar);
e.Row.Cells.Add(tc);
}
}
Finally, you call the command from the RowCommand
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
string currentCommand = e.CommandName;
string parameter= e.CommandArgument.ToString();
if (currentCommand.Equals("Ignorar"))
{
yourMethodName(parameter);
}
}
Hope that this is helpful for somebody!

Categories

Resources