How can I access the row in a RowCommand event - c#

I am trying to find a DropDown element in the GridView_RowCommand but it says that GridViewCommandEventArgs does not contain a definituon for 'Row'. I need to do this in this event because i am evaluating a GridView Command. See failing code below
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Add")
{
DropDownList Myddl = null;
ClientClass client = new ClientClass();
Myddl = e.Row.FindControl("ddlClients") as DropDownList;
if (Myddl != null)
{
updated = client.InsertUpdateClient(ClientID,
int.Parse(e.CommandArgument.ToString()), departmentID);
}
else
{
Labels.Text = "There was an error updating this client";
}
}
}

Something like this:
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
This is assuming what's firing off the RowCommand is a LinkButton. Change that according.

In addition to the #Stephen,
if (e.CommandName == "Add")
{
DropDownList Myddl = null;
ClientClass client = new ClientClass();
//Use this if button type is linkbutton
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
//Use this if button type is Button
//GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
Myddl = row.FindControl("ddlClients") as DropDownList;
if (Myddl != null)
{
updated = client.InsertUpdateClient(ClientID,
int.Parse(e.CommandArgument.ToString()), departmentID);
}
else
{
Labels.Text = "There was an error updating this client";
}
}

Related

No OnClick Event firing after page is open for a few minutes

I've searched for this but didn't find any matching question:
I have a DNN Website hosted on a local server in our company, and I have my own modules, where I have some Forms with TextBoxes and Buttons. I now have the following problem:
When I enter text the time I open the page and click on the Button the OnClick-Event is fired and everything is okay. When I now have the page open for a few minutes and then click on a Button, the page just reloads and the Event is not firing.
I also tried to tebug this multiple times, but the debugger never stops at the OnClick Method if the page is open more than around 5 minutes (I don't know the exact timeout since I don't know which parameter is leading to this)
I am not sure if this is a Problem from IIS, ASP or DNN - so I hoped to find help here. If you need further information, please ask.
Since you asked for the Code, I reduced it to the important lines (total would be a few thousand lines):
View.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="View.ascx.cs" Inherits="View" %>
...
<asp:Panel runat="server" ID="pnlGridView"></asp:Panel>
...
View.ascx.cs
public partial class View : FremdeBestellungenModuleBase, IActionable
{
...
protected void Page_Load(object sender, EventArgs e)
{
try
{
...
BuildGrids(true);
...
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
...
private void BuildGrids(bool all)
{
...
GridView gv = new GridView()
{
ID = "GridView_" + role,
AllowSorting = true,
AutoGenerateColumns = false,
...
};
...
gv.RowDataBound += new GridViewRowEventHandler(Generated_GridView_RowDataBound);
gv.Sorting += new GridViewSortEventHandler(GridviewBestellung_Sorting);
if (Session["sort"] == null)
{
Session[gv.ID] = gv.DataSource = list.Where(elem => elem != null).OrderByDescending(x => x.BestellNr).ToList();
}
else
{
gv.DataSource = Session[gv.ID];
}
gv.DataBind();
if (FindControl(gv.ID) == null)
{
pnlGridView.Controls.Add(gv);
}
}
...
protected void Generated_GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridView gv = sender as GridView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
...
CheckBox chkManager = new CheckBox()
{
ID = "chkManager"
};
e.Row.Cells[ManagerColumns.CheckUndHidden].Controls.Add(chkManager);
...
if (e.Row.RowType == DataControlRowType.Footer)
{
...
Button btnFreigeben = new Button()
{
ID = "btnFreigeben",
Text = "Freigeben",
UseSubmitBehavior = false
};
btnFreigeben.Click += new EventHandler(BtnFreigeben_Click);
...
}
}
protected void BtnFreigeben_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridView gv = (GridView)btn.NamingContainer.NamingContainer;
foreach (GridViewRow r in gv.Rows)
{
CheckBox chkManager = r.FindControl("chkManager") as CheckBox;
if (chkManager.Checked)
{
//do stuff;
BuildGrids(true);
Response.Redirect(Request.RawUrl);
}
}

How to check bool value in Rowdatabound event of gridview in asp.net

I have a gridview and I am binding the data through Datareader. In database t able this is a bit field and I want to check this that if it's FALSE hide some controls in gridview. so how can I get this value while Rowdatabound event. Thank you
here is my code.
protected void AllUsersGridView_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton LinkButton1 = (LinkButton)e.Row.Cells[6].FindControl("LinkButton1");
LinkButton LinkButton2 = (LinkButton)e.Row.Cells[6].FindControl("LinkButton2");
CheckBox c = ((CheckBox)e.Row.Cells[2]);
if (c.Checked)
{
LinkButton1.Visible = true;
LinkButton2.Visible = true;
}
else
{
LinkButton1.Visible = false;
LinkButton2.Visible = false;
}
}
}
You can directly use database field as mentioned below:
protected void AllUsersGridView_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton LinkButton1 = (LinkButton)e.Row.Cells[6].FindControl("LinkButton1");
LinkButton LinkButton2 = (LinkButton)e.Row.Cells[6].FindControl("LinkButton2");
DataRow row = ((DataRowView)e.Row.DataItem).Row;
bool isChecked = row.Field<bool>("[FiledName]");
// CheckBox c = ((CheckBox)e.Row.Cells[2]);
LinkButton1.Visible = isChecked;
LinkButton2.Visible = isChecked;
}
}
See below two lines of code in above code:
DataRow row = ((DataRowView)e.Row.DataItem).Row;
bool isChecked = row.Field<bool>("[FiledName]");
Try this
protected void AllUsersGridView_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");
LinkButton LinkButton2 = (LinkButton)e.Row.FindControl("LinkButton2");
CheckBox c = ((CheckBox)e.Row.FindControl("idOfCheckBox");
LinkButton1.Visible = LinkButton2.Visible = c.Checked
}
}

Trigger GridView1_SelectedIndexChanged event in pageload if session is == to column

I am trying to use selectedindex on my gridview if it has a column with an == value to session.
Right now I am only successfull on changing its background color. But how could I also trigger the button If it has similar value on my Session.
private void load_session_value()
{
string trans_id = Session["transaction_id_report"].ToString();
string trans_number = Session["transaction_no_report"].ToString();
//string grid_value_id = GridView1
//string grid_value_num
if (trans_id != null && trans_number != null)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.Cells[1].Text.ToString() == trans_id && row.Cells[2].Text.ToString() == trans_number)
{
row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
GridView1_SelectedIndexChanged(new object(),new EventArgs());
}
}
}
}
This is my current output during page_load
I'm not sure, but i think you are looking for this.
if (row.Cells[1].Text.ToString() == trans_id && row.Cells[2].Text.ToString() == trans_number)
{
row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
//call the Button1_Click method
Button1_Click(new object(), new EventArgs());
}
protected void Button1_Click(object sender, EventArgs e)
{
//do the button click stuff
}

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

OnRowCommand="grd_RowCommand" event not firing when click on row

here i bind grid columns dynamically using code behind because my GetSocailAnalytics method return dynamic column according to parameter passed.after binding column of grid using data table my grd_OnRowCommand event not firing when i click on grid row. grid is successfully bind. can any one help me out this issue.here is my code...
<asp:GridView ID="grd" EnableViewState="true" AutoGenerateColumns="false" OnRowCommand="grd_RowCommand"
runat="server" OnRowDataBound="grd_RowDataBound">
<Columns>
</Columns>
</asp:GridView>
private void GetData()
{
try
{
int TotalRecords = 0;
DataTable dt = ClsSocialManager.GetSocialAnalytics(Convert.ToInt32(hdnReferrerId.Value), Convert.ToInt32(hdnReferralId.Value), out TotalRecords, Convert.ToInt32(hdnPageIndex.Value));
if (dt != null && dt.Rows.Count > 0)
{
BindTemplateFiled(dt);
grd.Visible = true;
}
else
{
grd.Visible = false;
}
lblStatus.Text = TotalRecords.ToString() + " Record(s) found";
}
catch (Exception ex)
{
lblStatus.Text = "Some Error Occured " + ex.Message;
lblStatus.CssClass = "ErrMsg";
}
}
//Start Crearting GridColumn Dynamically
class LinkColumn : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
LinkButton link = new LinkButton();
link.ID = "lnkbtnReferrerHost";
link.DataBinding += new EventHandler(this.link_DataBinding);
link.CommandName = "sad";
container.Controls.Add(link);
}
private void link_DataBinding(Object sender, EventArgs e)
{
LinkButton lnkReferrerHost = (LinkButton)sender;
GridViewRow row = (GridViewRow)lnkReferrerHost.NamingContainer;
lnkReferrerHost.Text = Convert.ToString((((System.Data.DataRowView)(row.DataItem))).Row[1]);
lnkReferrerHost.CommandArgument = Convert.ToString((((System.Data.DataRowView)(row.DataItem))).Row[0]);
//lnkReferrerHost.CommandName = "Filter";
}
}
private void BindTemplateFiled(DataTable dt)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
if (dt.Columns[i].ColumnName == "Referrer Host")
{
var lnkbtnReferrerHost = new TemplateField();
lnkbtnReferrerHost.ItemTemplate = new LinkColumn();
lnkbtnReferrerHost.HeaderText = dt.Columns[i].ColumnName;
grd.Columns.Add(lnkbtnReferrerHost);
}
else
{
BoundField field = new BoundField();
field.DataField = dt.Columns[i].ColumnName;
field.HeaderText = dt.Columns[i].ColumnName;
grd.Columns.Add(field);
}
}
grd.DataSource = dt;
grd.DataBind();
}
//End Crearting GridColumn Dynamically
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "Filter")
{
GridViewRow gvr = (GridViewRow)grd.Rows[((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex];
LinkButton lbtn = (LinkButton)gvr.FindControl("lnkReferrerHost");
hdnReferrerId.Value = Convert.ToString(Convert.ToInt32(e.CommandArgument));
lblCurrentPage.Text = lbtn.Text;
GetData();
}
}
catch
{
}
}
The RowCommand event is raised when a button is clicked in the
GridView control.
it is not not firing when you click on grid row
solution is adding button (LinkButton) column to the gridview with CommandName ="Filter"

Categories

Resources