I have a asp:GridView inside of an asp:UpdatePanel, which has a column of asp:LinkButton controls.
On the row databound event, the LinkButton gets it's click event handler assigned.
I've tried every way I could find to wire up the click even and none of the events ever fire.
Am I doing something wrong?
aspx:
<asp:UpdatePanel ID="MainUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTest" Text="test" runat="server" />
<asp:GridView ID="gvClientsArchive" runat="server" AllowSorting="true" DataSourceID="dsClients"
OnRowDataBound="gvClientsArchive_RowDataBound" SkinID="gvList"
AllowPaging="true" PageSize="25" Visible="false">
...
Code behind:
protected void gvClientsArchive_RowDataBound(object sender, GridViewRowEventArgs e)
{
...
int company_id = int.Parse(drvRow["company_id"].ToString());
LinkButton lnkRestore = (LinkButton)e.Row.FindControl("lnkRestore");
lnkRestore.Click += new System.EventHandler(this.doRestore);
Button handler code:
private void doRestore(object sender, EventArgs e)
{
lblTest.Text = "restore clicked";
}
I've also tried:
protected void gvClientsArchive_RowDataBound(object sender, GridViewRowEventArgs e)
{
...
LinkButton lnkRestore = (LinkButton)e.Row.FindControl("lnkRestore");
lnkRestore.Click += delegate
{
lblTest.Text = "restore clicked";
};
RowDataBound is inappropriate if you want to register event handlers. Use RowCreated:
protected void gvClientsArchive_RowCreated(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnkRestore = (LinkButton)e.Row.FindControl("lnkRestore");
lnkRestore.Click += new System.EventHandler(this.doRestore);
}
}
RowDataBound is triggered only if you Databind the grid not on every postback which is needed since all controls are disposed at the end of the page's lifecycle. It's also too late.
If you use TemplateFields it's easier to register the handler declaratively on the aspx.
Related
On a link button click , I want to fetch the value of a hidden field which is contained inside a repeater. Below is my code
aspx page:
<asp:Repeater ID="rpt1" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lnkBtn1" runat="server" OnClick="btnClick"/>
<asp:HiddenField ID="hdn1" runat="server" Value="true"/>
</ItemTemplate>
</asp:Repeater>
CodeBehind:
protected void btnClick(object sender, EventArgs e)
{
//How to get the value of the hidden field hdn1 over here
}
try this
Find Row of repeater from Button as sender then find Hidden field inside (RepeaterItem) repeater row
protected void btnClick(object sender, EventArgs e)
{
LinkButton lnkBtn1= sender as LinkButton;
RepeaterItem Rptitem = (RepeaterItem)lnkBtn1.NamingContainer;
HiddenField hdn1 = (HiddenField ) Rptitem.FindControl("hdn1");
string hiddenvalue=hdn1.Value;
}
I want to remove the event handler of an ItemTemplate in asp .net. Here is the asp code
<ItemTemplate>
<asp:RadioButton runat="server" ID="rdbAnswer" GroupName="Group" AutoPostBack="True"
OnCheckedChanged="rdbAnswer_CheckedChanged" />
</ItemTemplate>
I want to remove the checkedchanged event on code behind. How can i do this ?
Use -= to remove an event handler. You could do that in RowCreated:
protected void gridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
RadioButton rdbAnswer = (RadioButton)e.Row.FindControl("rdbAnswer");
if(YourCondition)
{
// Remove event handler
rdbAnswer.CheckedChanged -= new EventHandler(rdbAnswer_CheckedChanged);
// maybe you also want to set rdbAnswer.AutoPostBack="false" to prevent the postback
}
}
}
Remember to register the RowCreated event handler:
<asp:GridView ID="gridView1" OnRowCreated="gridView1_RowCreated" runat="server">
I am using a LinqDataSource and a FormView with paging enabled on an ASP.NET page. I am trying to access the FormView's DataItem property on PageLoad and I have no trouble on the first page load, but as soon as I use the Next/Prev page button (causing a postback) on the FormView the DataItem property is null, even if there a record showing in the FormView. Any ideas why it works fine on the first page load but not on a postback?
If you're curious what my PageLoad event looks like, here it is:
protected void Page_Load(object sender, EventArgs e)
{
Label lbl = (Label)fvData.FindControl("AREALabel");
if (fvData.DataItem != null && lbl != null)
{
INSTRUMENT_LOOP_DESCRIPTION record = (INSTRUMENT_LOOP_DESCRIPTION)fvData.DataItem;
var area = db.AREAs.SingleOrDefault(q => q.AREA1 == record.AREA);
if (area != null)
lbl.Text = area.AREA_NAME;
}
}
The object you bind to any data-bound control won't be persisted in the page's ViewState
Therefore, on subsequent posts the DataItem property will be null unless you re-bind the control
This property will contain a reference to the object when the control is bound.
Usually you would need to access this property if you want to do something when the objects is bound, so you need to react to the DataBound event
Example:
Output
Code behind
protected void ds_DataBound(object sender, EventArgs e)
{
var d = this.fv.DataItem as employee;
this.lbl.Text = d.lname;
}
ASPX
<asp:LinqDataSource ID="lds" runat="server"
ContextTypeName="DataClassesDataContext"
TableName="employees"
>
</asp:LinqDataSource>
<asp:FormView runat="server" ID="fv" DataSourceID="lds" AllowPaging="true"
OnDataBound="ds_DataBound">
<ItemTemplate>
<asp:TextBox Text='<%# Bind("fname") %>' runat="server" ID="txt" />
</ItemTemplate>
</asp:FormView>
<br />
<asp:Label ID="lbl" runat="server" />
Your data will not be preserved on PostBack. You'll need to rebind the FormView in the PageIndexChanging event using something like:
protected void FormView_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
FormView.PageIndex = e.NewPageIndex;
//rebind your data here
}
i have allowed paging and added the below codes but got the error. Does anyone know what could be the problem?
Code:
protected void SubmitAppraisalGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
SubmitAppraisalGrid.PageIndex = e.NewSelectedIndex;
SubmitAppraisalGrid.DataBind();
}
Design:
<asp:GridView ID="SubmitAppraisalGrid" runat="server"
AutoGenerateColumns="False" BorderWidth="0px"
onrowcreated="SubmitAppraisalGrid_RowCreated" ShowHeader="False"
style="margin-right: 0px" AllowPaging="True" PageSize="1"
onselectedindexchanging="SubmitAppraisalGrid_SelectedIndexChanging">
</asp:GridView>
If you have set a gridviews AllowPaging attribute to “true” and do not handle the PageIndexChanging event then this error raise.
To work with paging add the PageIndexChanging event handler to grid and change your markup and code as:
<asp:GridView ID="SubmitAppraisalGrid" runat="server"
AutoGenerateColumns="False" BorderWidth="0px"
onrowcreated="SubmitAppraisalGrid_RowCreated" ShowHeader="False"
style="margin-right: 0px" AllowPaging="True" PageSize="1"
onselectedindexchanging="SubmitAppraisalGrid_SelectedIndexChanging"
OnPageIndexChanging="SubmitAppraisalGrid_PageIndexChanging">
</asp:GridView>
///
protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
SubmitAppraisalGrid.PageIndex = e.NewPageIndex;
SubmitAppraisalGrid.DataBind();
//bindGrid();
//SubmitAppraisalGrid.PageIndex = e.NewPageIndex;
//SubmitAppraisalGrid.DataBind();
}
protected void SubmitAppraisalGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
/// you selected index related logic here.
}
This event is not raised when you programmatically set the PageIndex property.
Check MSDN documentation of GridView.PageIndexChanging Event
For reference:
The GridView fired event PageIndexChanging which wasn't handled
Your code should be inside On PageIndexChanging Event
protected void SubmitAppraisalGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
SubmitAppraisalGrid.PageIndex = e.NewPageIndex;
SubmitAppraisalGrid.DataBind();
}
Design:
<asp:GridView ID="SubmitAppraisalGrid" runat="server"
AutoGenerateColumns="False" BorderWidth="0px"
onrowcreated="SubmitAppraisalGrid_RowCreated" ShowHeader="False"
style="margin-right: 0px" AllowPaging="True" PageSize="1"
OnPageIndexChanging="SubmitAppraisalGrid_PageIndexChanging">
</asp:GridView>
try
OnPageIndexChanging="SubmitAppraisalGrid_PageIndexChanging"
instead of
onselectedindexchanging="SubmitAppraisalGrid_SelectedIndexChanging"
protected void SubmitAppraisalGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
SubmitAppraisalGrid.PageIndex = e.NewPageIndex;
BindGrid();
}
insted of using
SubmitAppraisalGrid.PageIndex = e.NewSelectedIndex;
you must use
SubmitAppraisalGrid.PageIndex = e.NewPageIndex;
and if you got error again plese post the error too..
Step by Step:
Select gridview from design and go to property and fire the event (PageIndexChanging)
Code : gridviewname.pageindex=e.NewPageIndex;
You need to call the Pageindex changing event from selected index changing event of dropdown.
protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
{
// Retrieve the pager row.
GridViewRow pagerRow = SubmitAppraisalGrid.BottomPagerRow;
// Retrieve the PageDropDownList DropDownList from the bottom pager row.
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
// Set the PageIndex property to display that page selected by the user.
GridViewPageEventArgs evt = new GridViewPageEventArgs(pageList.SelectedIndex);
SubmitAppraisalGrid_PageIndexChanging(sender, evt);
}
by right my label1.text will be display by each clicks, however my label was fire during page load, and this is not what i want, so any idea can perform fire event per clicks?
gridview property
asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnRowDataBound="abcde"
link button that inside gridview
asp:LinkButton ID="lnkname" runat="server" Text='<%#Eval("movieTitle") %>' Width=500 CommandName="cmdLink">
code behind for link button
protected void abcde(object sender, GridViewRowEventArgs e)
{
Label1.Text = ((LinkButton)e.Row.FindControl("lnkname")).Text;
}
Problem is that whenever any click event occur in Gridview that time GridviewRowCommand will be fire..you just check that when you want to fire this event.its means during paging event also fire this event..thats y just check it out using [CommandName] like this
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
string namec = e.CommandName.ToString();
if (namec == "cmdLink")
{
//put your code
}
It sounds like you bind the gridview on page load event. Every time you bind the gridview, the onbound event will be fired:
protected void abcde(object sender, GridViewRowEventArgs e)
You can avoid that by checking postback in pageload before gridview databind:
if (!IsPostBack)
{
//Gridview databind
}
Change the Command Name of LinkButton to
CommandName="Select"
and use GridView1_SelectIndexChanged event
First Get the Select Row
GridViewRow gvr = new GridView1.SelectedRow;
and FindControl of the row