Click Event for ImageButton Inside RadGrid - asp.net

I have an asp.net ImageButton inside a RadGrid (in a column) that when clicked opens a popup window. I can also expand this same RadGrid to reveal a nested grid. I have a button inside here that I need to assign a click event to such that it opens the same popup. How do I fire off an ImageButton click that's housed inside a RadGrid?
Here is the aspx and the "imgEdit" is what I need to fire off:
<MasterTableView DataKeyNames="SuggestionID">
<EditFormSettings CaptionFormatString="Edit Suggestion: {0}" CaptionDataField="Title"
InsertCaption="Add Suggestion" EditFormType="WebUserControl" PopUpSettings-Width="655px"
UserControlName="~/CommonUserControls/SuggestionControl.ascx">
</EditFormSettings>
<Columns>
<telerik:GridTemplateColumn UniqueName="EditAction" HeaderStyle-Width="32px" ItemStyle-Wrap="false"
Resizable="false">
<ItemTemplate>
<asp:ImageButton ID="imgEdit" runat="server" CommandName="Edit" Resizable="false"
ImageUrl="/ESDNET/Images/Icons/pencil.png" ToolTip="Edit Suggestion" Visible='<%# Eval("CanEdit") %>' />

You can achieve the same in different ways:
1) Directly attach the button click event of imagebutton
protected void imgEdit_Click(object sender, ImageClickEventArgs e)
{
// your code
}
2) Using CommandName:
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
if(e.CommandName=="Edit")
{
}
}
3) If the commandname is Edit ,then it will autometically fire EditCommand
protected void RadGrid1_EditCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
}

Related

Dropdownlist SelectedIndexChanged not firing on aspx page

I want to auto click the search button whenever user select a value from drop down list.
code snippet:
Added the event handler in InitializeComponent() :
this.ddltrim.SelectedIndexChanged += new System.EventHandler(this.ddltrim_SelectedIndexChanged);
code:
private void ddltrim_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(ddlStores.Items.Count ==1)
btnSearch_Click("Search", null);
}
In designer:
<asp:dropdownlist id=ddltrim width="100%" Runat="server" AutoPostBack="True" EnableViewState="True">
<asp:ListItem Value="Select Submodel" Selected="True">
Select SubModel
</asp:ListItem></asp:dropdownlist>
But selectIndexChanged in not firing when i select any value from ddl. Have to manually click the button search.
Try
<asp:dropdownlist id=ddltrim width="100%" Runat="server" AutoPostBack="True" EnableViewState="True" onselectedindexchanged="ddltrim_SelectedIndexChanged">
<asp:ListItem Value="Select Submodel" Selected="True">Select SubModel</asp:ListItem>
Try to set your add event code in Page_PreInit method
protected void Page_PreInit(object sender, EventArgs e)
{
this.ddltrim.SelectedIndexChanged += new System.EventHandler(this.ddltrim_SelectedIndexChanged);
}

Button Control not working with RowCommand Event

<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="lbShowGroup" CommandName="View" CommandArgument='<%# Eval("Topic") %>'
runat="server" Text="View"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
Code behind:
protected void tblTopics_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "View")
{
Response.Redirect("Group.aspx?Topic=" + e.CommandArgument.ToString());
}
}
Debugging doesn't reach the RowCommand event, but when I change the button control to LinkButton, it works. What's wrong?
Do you databind your grid on postbacks?
You must not bind your grid on postbacks in Page_Load, only when something has changed that causes the GridView to reload data(f.e. Sorting,Paging) and only in the appropriate even-handlers.
So wrap the databinding in a PostBack-check:
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
DataBindGrid();
}
}
Another possible reason: Have you disabled ViewState somewhere?
I had the same issue and found out that my problem was in the Master Page where EnableViewState="false".
I changed the Master page to use EnableViewState="True".
And the rowcommand event fired as expected.

ASP.NET GridView Data which i set on RowDatabound event loses after post back

I have an asp.net page where i have a gridview control which bind data from a DataTable.
In my 5 th column of the grid i am showing a Radio button list which has 2 Radio button items (Yes or No). For Some rows i will not show the RadioButton control, if the 4 th column cell value is empty. It works fine.But in my button click event (postback), the Grid is showing the Radio button list for those cells which was not shown initially. I have enabled ViewState for page and Control
This is my code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns=false
DataKeyNames="RequestDetailId" ondatabound="GridView1_DataBound" EnableViewState ="true" AllowPaging=false
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="RequestDetailId" HeaderText="Request Detail Id" />
<asp:BoundField DataField="Item" HeaderText="Item" />
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:BoundField DataField="NewOffer" HeaderText="New Offer" />
<asp:TemplateField HeaderText="Your Response" ItemStyle-CssClass="radioTD">
<ItemTemplate>
<asp:RadioButtonList ID="radioList" runat="server">
<asp:ListItem Text="Yes" Value="Accept"></asp:ListItem>
<asp:ListItem Text="No" Value="Reject"></asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
in code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadItemDetails();
}
}
private void LoadItemDetails()
{
DataTable objDt= GetGridDataSource();
if (objDt.Rows.Count > 0)
{
GridView1.DataSource = objDt;
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(String.IsNullOrEmpty (e.Row.Cells[3].Text.Trim())||(e.Row.Cells [3].Text ==" "))
{
e.Row.Cells[4].Text = "";
}
}
My results are
Before Postback
After Postback
How do i maintain the content after postback ? Thanks
The problem is that you are setting the Text of the GridView table cell to an empty string rather than setting the visibility of the RadioButtonList control to false.
Clearing the Text property is removing the markup for the RadioButtonList on the first load, but on postback the RowDataBound event is not fired and the RadioButtonList control is recreated and displayed again.
To avoid this you could find the control and set its visibility to false, this will then be remembered across postbacks.
Try the following:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (String.IsNullOrEmpty(e.Row.Cells[3].Text.Trim()) || (e.Row.Cells[3].Text == " "))
{
RadioButtonList radioList = (RadioButtonList)e.Row.FindControl("radioList");
radioList.Visible = false;
}
}
Hope this helps.
you can do something like this ..
from the description of the problem. it sounds as if you are doing the databinding in the code behind. in such case asp.net does not preserve the datasource in the viewstate for you. try retrieving the data and storing it in the ViewState hashtable object with something like
ViewState["GridviewData"] = GridviewData
and retreiving it from there between postbacks

Bit of help with DataPager template & LinkButton please?

I have a datapager with a pagertemplate. In the template I have a "Show All" button, which sets the PageSize of the datapager to show all records. This works fine but I want to be able to hide the button when it's clicked. It's in an UpdatePanel so I don't know if that makes a difference?
<asp:DataPager ID="Pager" runat="server" PagedControlID="rangeList" PageSize="15" EnableViewState="false">
<Fields>
<asp:TemplatePagerField>
<PagerTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument="<%# Container.TotalRowCount.ToString() %>"
oncommand="LinkButton1_Command" >Show All Ranges</asp:LinkButton>
</PagerTemplate>
</asp:TemplatePagerField>
<asp:numericpagerfield ButtonCount="10" NextPageText="..." PreviousPageText="..." CurrentPageLabelCssClass="pageOn" />
</Fields>
</asp:DataPager>
And the codebehind:
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
this.Pager.PageSize = int.Parse(e.CommandArgument.ToString());
LinkButton lb = (LinkButton)sender;
if (lb != null)
{
lb.Visible = false;
}
rangeList.DataBind();
}
The first click works fine, and refreshes the ListView which in turn adjusts the pager to show one page with all the results on it, but the button doesn't disappear as I want it to.
Any ideas?
If there's nothing to display within the pager, why not hide the Pager control itself:
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
this.Pager.PageSize = int.Parse(e.CommandArgument.ToString());
this.Pager.Visible = false;
lnkShowPages.Visible = true; // EDIT only
rangeList.DataBind();
}
EDIT:
You could have a second "Show Pages" LinkButton that's initially not visible and becomes visible when the Show All LinkButton is clicked (above). When this new LinkButton is clicked, it could then enable paging by setting the Pager's PageSize and visibility and hiding itself:
protected void lnkShowPages_Command(object sender, CommandEventArgs e)
{
this.Pager.PageSize = int.Parse(e.CommandArgument.ToString());
this.Pager.Visible = true;
lnkShowPages.Visible = false;
rangeList.DataBind();
}

GridView Paging Issue

I have a very simple GridView on one of my pages with the following markup on my .aspx page:
<asp:GridView ID="gvNews" runat="server" AutoGenerateColumns="false" AllowPaging="true"
AllowSorting="true" DataKeyNames="NewsID,VersionStamp" OnPageIndexChanging="gvNews_PageIndexChanging"
OnRowCreated="gvNews_RowCreated">
<Columns>
<asp:BoundField HeaderText="News Title" DataField="NewsTitle"
SortExpression="NewsTitle" ReadOnly="true" />
<asp:BoundField HeaderText="News Content" DataField="NewsContent"
SortExpression="NewsContent" ReadOnly="true" />
<asp:BoundField HeaderText="Posted Date" DataField="InsertedDate"
SortExpression="InsertedDate" ReadOnly="True" />
<asp:BoundField HeaderText="InsertedBy" DataField="InsertedBy" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbEdit" runat="server" Text="Edit" CommandName="Select" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Below is the code on my .cs page:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadGrid();
}
}
private void LoadGrid()
{
gvNews.DataSource = GetNews();
gvNews.DataBind();
}
protected void gvNews_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
}
protected void gvNews_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[3].Visible = false;
}
On the RowCreated event I am trying to hide the InsertedBy column in the gridview. This code works fine when AllowPaging is set to flase. But when the AllowPaging is set to true I get the following error in the RowCreated event handler:
Specified argument was out of the range of valid values.
Parameter name: index
What could be the reasons for this behavior?
You need to write your code like this:
protected void gvNews_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[3].Visible = false;
}
}
With a GridView there are different types of rows that might get created and they will have different numbers of cells, but the RowCreated event will fire for all rows, so you need to limit your logic to only data rows in this case.
From what you have posted your hard coded value of 3 in the RowCreated event seems like the problem. Enable tracing on the page and see what you get. BTW the pager next->prev links also cause postback and in PageLoad u are only loading grid if its not a postback which it is when u try to go for next page and the row created is fired.

Resources