If I have a LinkButton in an ItemTemplate of a GridView, and I want that LinkButton to fire it's events (having given up on the row event), when should I declare the button?
<ItemTemplate>
<asp:LinkButton runat="server" Text="Edit" OnInit="EditLinkButton_Init" CommandName="Edit" OnCommand="EditLinkButton_Command" ID="EditLinkButton"></asp:LinkButton>
The only event below that ever fires is EditLinkButton_Init. The text of the LinkButton is changed to ROCKON, so I think the events should fire as well? I'm still LifeCycle challenged, so if you could offer a brief explanation or link that would be great.
Here is the code-behind:
protected void EditLinkButton_Init(object sender, EventArgs e)
{
LinkButton myLinkButton = new LinkButton();
myLinkButton = (LinkButton)sender;
myLinkButton.Text = "ROCKON";
myLinkButton.Click += new EventHandler(EditLinkButton_Click);
myLinkButton.CommandName = "Edit";
myLinkButton.Command += myLinkButton_Command;
}
void myLinkButton_Command(object sender, CommandEventArgs e)
{
throw new NotImplementedException();
}
protected void EditLinkButton_Command(object sender, CommandEventArgs e)
{
}
protected void EditLinkButton_Click(object sender, EventArgs e)
{
}
I've desperately thrown everything I can think of at it....
Related
I'm trying to change ImageUrl onclick Imagebutton that is present on my Repeater. I have to change it from behind code(cs file) not using jquery or js.
<asp:ImageButton ID="imgLike" ImageUrl="Content/images/thumbsup.png" onclick="imgLike_Click" runat="server" style="width:25px;height:25px" />
and here is code
protected void imgLike_Click(object sender, ImageClickEventArgs e)
{
ImageButton imgLike = (ImageButton)rptinserting.FindControl("imgLike");
imgLike.ImageUrl = "Content/images/thumbsup1.png";
}
You cast the sender back to an ImageButton and change the ImageUrl.
protected void imgLike_Click(object sender, ImageClickEventArgs e)
{
ImageButton imgLike = sender as ImageButton;
imgLike.ImageUrl = "/Content/images/thumbsup1.png";
}
I have a dropdown list inside a asp:repeater item template.
how can I get its value on button click event.
<asp:Repeater runat="server" ID="WorkflowListAfter" onitemcreated="WorkflowListAfterItemCreated">
<ItemTemplate>
<asp:DropDownList ID="ddlWorkflowMembers" runat="server" DataTextField="MemberName" DataValueField="MemberID">
</ItemTemplate>
</asp:Repeater>
protected DropDownList ddlWorkflowMembers = new DropDownList();
protected void WorkflowListAfterItemCreated(object sender, RepeaterItemEventArgs e)
{
ddlWorkflowMembers = (DropDownList) e.Item.FindControl("ddlWorkflowMembers");
}
protected void BtnSaveClick(object sender, EventArgs e) {
if (ddlWorkflowMembers.SelectedItem == null) return;
}
the code above is working at first time but after postback ddlWorkflowMembers is always null expersion.
Assuming that BtnSave is also inside the repeater.
You get the RepeaterItem by casting the button's NamingContainer. Then you can use FindControl to get the reference to your DropDownList:
protected void BtnSaveClick(object sender, EventArgs e) {
var btn = (Button)sender;
var item = (RepeaterItem)btn.NamingContainer;
var ddl = (DropDownList) item.FindControl("ddlWorkflowMembers");
// ...
}
If the button is outside of the repeater and you want to save all items, you need to loop through all:
protected void BtnSaveClick(object sender, EventArgs e) {
foreach(RepeaterItem item in WorkflowListAfter.Items)
{
var ddl = (DropDownList) item.FindControl("ddlWorkflowMembers");
// ...
}
}
I have a TextBox control inside a panel and this panel is inside DataList ItemTemplate.
After firing the ItemCommand event, everything works fine except that the TextBox.Text property is always an empty string "" although there is some text in it.
I tried several ways but without success. I would really appreciate if someone can assist me with this. Simplified code is shown below.
Thank you!
ASPX page:
<asp:DataList ID="dlDataList" runat="server" onitemcommand="dlDataList_ItemCommand">
<ItemTemplate>
<asp:Panel ID="pnlReply" runat="server" Visible="False">
<asp:TextBox ID="txtTextBox" runat="server"></asp:TextBox><br />
<asp:LinkButton ID="lnkbtnSend" CommandName="Send" runat="server">Send</asp:LinkButton>
</asp:Panel><br />
<asp:LinkButton ID="OpenPanel" CommandName="OpenPanel" runat="server">Open panel</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</asp:Content>
ASPX.CS Page code behind
protected void Page_Load(object sender, EventArgs e)
{
FillDataList();
}
private void FillDataList()
{
List<string> list = new List<string>();
list.Add("First");
list.Add("Second");
list.Add("Third");
dlDataList.DataSource = list;
dlDataList.DataBind();
}
protected void dlDataList_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "OpenPanel")
{
Panel pnlReply = (Panel)e.Item.FindControl("pnlReply");
pnlReply.Visible = true;
}
if (e.CommandName == "Send")
{
TextBox txtTextBox = (TextBox)e.Item.FindControl("txtTextBox");
//I tried this way also..
//TextBox txtTextBox = (TextBox)e.item.FindControl("pnlReady").FindControl("txtTextBox");
Label1.Text = txtTextBox.Text;
}
}
Please use IsPostBack in page load event. Without it your FillDataList(); is executing on every postback and resetting your DataList.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDataList();
}
}
I have a grid in asp.net, inside the asp.net i am binding data as linkbutton when clicking on link button I need to call a method in code behind. the attached event is not woking in my code. how i can solve this?
my code is similar like this,
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton link = new LinkButton();
link.Text = e.Row.Cells[0].Text;
link.CommandArgument = "Hello";
link.Click += new EventHandler(this.onLinkClick);
e.Row.Cells[0].Controls.Add(link);
}
}
protected void onLinkClick(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
string value = btn.CommandArgument;
TextBox1.Text=value;
}
You have to call the function that binds the source to the GridView everytime in the Page Load
ex.
protected void Page_Load(object sender, EventArgs e)
{
PopulateGridView();
}
Because there is not logic for adding or not the link button(I guess you have to add it for each record) why don't you add it at design time?
<asp:GridView ID="GridView1" runat="server">
....
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>
</ItemTemplate>
......
</asp:GridView>
Make sure that AutoEventWireup="true" on the page
Handle RowCommand event of GridView to handle "events" of buttons and you are adding LinkButton dynamically then Data Binding must be performed either at Page_Init or Page_Load.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton link = new LinkButton();
link.Text = e.Row.Cells[0].Text;
link.CommandArgument = "Hello";
e.Row.Cells[0].Controls.Add(link);
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string value = e.CommandArgument.ToString();
TextBox1.Text=value;
}
You need to hook up the event handler for your dynamic button in the GridView's RowCreated event or it won't fire. Then use "FindControl" in the RowDataBound event handler. Personally, I don't like this model at all but sometimes it's unavoidable.
You should use the GridView's <asp:ButtonField> with the grid's RowCommand event. This way, you're not the one creating the dynamic control and wiring up the events.
Here's an article on how to use it.
I have a ListView with an IEnumerable<MyDocument> DataSource pulled from a method.
The code in myDocsList_ItemCommand() definitely runs, because the document is actually deleted. My problem is that the ListView still shows the (now deleted) document until the next page refresh, even though I have code to do myDocsList.Items.Remove(dataItem).
The simplified .ascx is basically:
<asp:ListView id="myDocsList" runat="server"
OnItemDataBound="myDocsList_ItemDataBound"
OnItemDeleting="myDocsList_ItemDeleting"
OnItemCommand="myDocsList_ItemCommand">
<LayoutTemplate>
<table>
<asp:Placeholder id="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr><td>
<asp:LinkButton
ID="delete" runat="server"
CommandName="Delete" CommandArgument="X"
OnClientClick="javascript:return confirm('...');">
Delete
</asp:LinkButton>
</td></tr>
</ItemTemplate>
</asp:ListView>
The simplified .ascx.cs is basically:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_PreRender(object sender, EventArgs e)
{
if (!IsPostBack)
{
IEnumerable<MyDocument> docs = getDocuments();
myDocsList.DataSource = docs;
myDocsList.DataBind();
}
}
/* so we have the ID of the document we're deleting later on */
protected void myDocsList_ItemDataBound(object sender,
ListViewItemEventArgs e)
{
var deleteButton =
(LinkButton) ((Control) e.Item).FindControl("delete");
deleteButton.CommandArgument =
((MyDocument) e.Item.DataItem).id.ToString();
}
/* or we get "raised event ItemDeleting which wasn't handled" */
protected void myDocsList_ItemDeleting(Object sender,
ListViewDeleteEventArgs e)
{
}
/* do something here? */
//protected void myDocsList_ItemDeleted(Object sender,
// ListViewDeletedEventArgs e)
//{
//}
protected void myDocsList_ItemCommand(object sender,
ListViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int docId = int.Parse(e.CommandArgument.ToString());
deleteDocument(docId);
ListViewDataItem dataItem = (ListViewDataItem) e.Item;
myDocsList.Items.Remove(dataItem);
}
}
I've been reading up on the ASP page lifecycle and a few related questions, but I'm reasonably inexperienced with ASP and a bit lost.
How do I get my ListView items to disappear on the PostBack instead of on the next page refresh?
You have to rebound data to your ListView in ItemCommand event, you can change your coding style as below:
//Create a new method for databind
void BindData()
{
IEnumerable<MyDocument> docs = getDocuments();
myDocsList.DataSource = docs;
myDocsList.DataBind();
}
//Call databind method in your prerender event
protected void Page_PreRender(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
//Again bind data after delete operation
protected void myDocsList_ItemCommand(object sender,
ListViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int docId = int.Parse(e.CommandArgument.ToString());
deleteDocument(docId);
BindData();
}
}