How to create button in dynamically created Gridview? - c#

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!

Related

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.

Event handler not firing on dynamic button click

I have a dynamically created button with an onclick event handler. The problem is that when I click the button it does not hit the event in the code-behind.
protected void gvOrder_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable dt = ds.Tables[0];
DropDownList ddl = new DropDownList();
TextBox txt = new TextBox();
int index = 1;
if (e.Row.RowType == DataControlRowType.DataRow)
{
ddl = e.Row.FindControl("ddlNewO") as DropDownList;
txt = e.Row.FindControl("txtNewT") as TextBox;
}
foreach (DataRow r in dt.Rows)
{
string listitem = Convert.ToString(index);
ddl.Items.Add(listitem);
index++;
}
ddl.SelectedIndex = e.Row.RowIndex;
if (e.Row.RowIndex == 0)
{
ddl.Enabled = false;
txt.Enabled = false;
}
else if (e.Row.RowIndex != 0)
{
ddl.Items.Remove("1");
//Create ED button
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnED = new Button();
btnED.ID = "btnED";
btnED.CssClass = "buttonsmall";
//btnED.CommandName = "ED";
btnED.EnableViewState = true;
btnED.Click += new EventHandler(btnED_Click);
foreach (DataRow r in dt.Rows)
{
btnED.Attributes.Add("ID", r.ItemArray[2].ToString());
if (r.ItemArray[3].ToString() == "1")
{
btnED.Text = "Disable";
}
else
{
btnED.Text = "Enable";
}
//Add button to grid
e.Row.Cells[5].Controls.Add(btnED);
}
}
}
}
protected void btnED_Click(object sender, EventArgs e)
{
// Coding to click event
}
So the problem here is that when the page is being recreated on post back - there is no more button! Dynamic controls need to be added on the page on every post back to fire events properly. In your case however on the first load when the GridView is binding you add the button to the page. But on the post back after the click the button is not added again, because GridView is not data bound again. Therefore ASP.NET cannot derive the source of the event, and supresses it.
Fix here is to bind GridView with data on every post back. Literally if you had if (!IsPostBack) - remove it. Or you can add the button in the template field and play with visibility - may be an approach as well.
You need to add a click handler on Row Created not on Data Bound I believe.
protected void gvOrderRowCreated(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType) {
case DataControlRowType.DataRow:
Button btn = (Button)e.Row.FindControl("btnED");
btn.Command += btnED_Click;
break;
}
}

Editing a gridview with AutoGenerateEditButton = true

I created a gridview dynamically and added the AutoGenerateEditButton = true; property and i see that it adds the edit link to all the fields when it loads the table into the gridview. But when i click the edit button nothing happens, except for a postback. What am i doing wrong?
GridView gridData = new GridView();
gridData.ID = "test";
gridData.AutoGenerateEditButton = true;
gridData.RowEditing += (sender, e) => grid_RowEditing(tbl, e, sender);
gridData.DataSource = tbl;
gridData.DataBind();
protected void grid_RowEditing(DataTable tbl, GridViewEditEventArgs e, object sender)
{
((GridView)sender).EditIndex = e.NewEditIndex;
// call your databinding method here
((GridView)sender).DataSource = tbl ;
((GridView)sender).DataBind();
}
My guess is that because you are overwriting the grid with every postback by recreating it, the click is just ignored.
The suggested approach would be to create the invisible grid statically:
<asp:GridView .... Visible="false" />
and then just make it visible when needed. This approach (in contrast to dynamic creation) will save you from dozen of issues, most probably including the one you face.
Edit: if you really, really want to follow your route then my guess is that you just haven't specified your data key names so that the edit click carries no information on the clicked row.
gridData.DataKeyNames = new string[] { "ID" };
(if your primary key column name is ID)
I added a <p id="p" runat="server"> on client side and added that grid inside this control.
Here is my code.
I used SqlDataSource instead of Datatable, you can use it any one of it.
Hope it works for u..
protected void gridview()
{
GridView gridData = new GridView();
gridData.ID = "test";
gridData.AutoGenerateEditButton = true;
gridData.RowEditing += (sender, e) => grid_RowEditing(SqlDataSource1, e, sender);
gridData.DataSource = SqlDataSource1;
gridData.DataBind();
p.Controls.Add(gridData);
}
protected void grid_RowEditing(SqlDataSource tbl, GridViewEditEventArgs e, object sender)
{
((GridView)sender).EditIndex = e.NewEditIndex;
// call your databinding method here
((GridView)sender).DataSource = tbl;
((GridView)sender).DataBind();
}
2) I tried with datatable too, and its working.
here is my code.
protected void gridview()
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("name");
for (int i = 0; i < 100; i++)
{
dt.Rows.Add(i.ToString(),"v"+i.ToString());
}
GridView gridData = new GridView();
gridData.ID = "test";
gridData.AutoGenerateEditButton = true;
gridData.RowEditing += (sender, e) => grid_RowEditing(dt, e, sender);
gridData.DataSource = dt;
gridData.DataBind();
p.Controls.Add(gridData);
}
protected void grid_RowEditing(DataTable tbl, GridViewEditEventArgs e, object sender)
{
((GridView)sender).EditIndex = e.NewEditIndex;
// call your databinding method here
((GridView)sender).DataSource = tbl;
((GridView)sender).DataBind();
}

selectedindexchanged on dynamically created gridview

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.

Categories

Resources