Receive ImageButton inside Repeater - c#

I have the following Repeater and ImageButton. The Repeater may have up to 750 of these ImageButtons (which works fine).
<asp:Repeater ID="PokeSearchControl" runat="server">
<ItemTemplate>
<asp:ImageButton OnClick="imgSearchResult_Click" BackColor="#333333" ID="imgSearchResult" height="32" width="32" runat="server" ImageUrl='<%# Eval("ImageUrl") %>'/>
</ItemTemplate>
</asp:Repeater>
My codebehind:
protected void imgSearchResult_Click(object sender, ImageClickEventArgs e)
{
ChangePokemon(selectedImage, imgPoke1, labPoke1, labPokeName1);
}
selectedImage would be the ImageButton which fired the OnClick event. How do I receive that Image?

Use sender:
protected void imgSearchResult_Click(object sender, ImageClickEventArgs e)
{
var selectedImage = sender as ImageButton;
//Get selectedImage's properties here for example
string imageUrl = selectedImage.ImageUrl;
//Or do your stuff
ChangePokemon(selectedImage, imgPoke1, labPoke1, labPokeName1);
}

Related

CheckedChanged Event Not Firing In Gridview

I have a gridview in which I have manually generated a column for checkboxes as a HeaderTemplate as below
<asp:GridView ID="gvDB" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" OnRowDataBound="gvDB_RowDataBound" <asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkSelectHeader" AutoPostBack="true" OnCheckedChanged="chkSelectHeader_CheckedChanged" runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" AutoPostBack="true" runat="server" OnCheckedChanged="chkSelect_CheckedChanged1" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And OnRowDataBound I'm dynamically generating controls and adding it to the each row
e.Row.Cells[rowIndex].Controls.Add(control);
And they are binding to the columns as expected.But my chkSelectHeader_CheckedChanged chkSelect_CheckedChanged1 events are not firing.
Page Load
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
AddTemplatesToGrid();
}
BindDataToGridView();
}
public void AddTemplatesToGrid()
{
DataTable dt = new DataTable();
foreach (Employees emp in EmployeesList)
{
TemplateField tfield = new TemplateField();
tfield.HeaderText = emp.Name;
gvDataEntry.Columns.Add(tfield);
}
}
You call BindDataToGridView on every postback which will discard events.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
AddTemplatesToGrid();
}
BindDataToGridView();
}
Include BindDataToGridView() in the !Page.IsPostBack-check.

DataList SelectedIndexChange event not firing

I have hyperlink and a hidden field inside a datalist as shown below
<asp:DataList ID="clientsList" runat="server" OnSelectedIndexChanged="clientsList_SelectedIndexChanged1" >
<ItemTemplate>
<asp:HyperLink ID="hlName" runat="server" Text='<%# Bind("Name") %>' NavigateUrl="#" ></asp:HyperLink>
<asp:HiddenField ID="HiddenFieldID" runat="server" Value='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:DataList>
When user clicks on any hyperlink, I need to store that value in an application variable. But the SelectedIndexChange event never fires.
This is my code:
protected void clientsList_SelectedIndexChanged1(object sender, EventArgs e)
{
int idx = clientsList.SelectedIndex;
HiddenField hiddenCID = clientsList.Items[idx].FindControl("HiddenFieldID") as HiddenField;
if (hiddenCID != null)
{
Logger.UpdateLog("Selected ID: " + hiddenCID.Value.ToString());
}
}
I am binding data from database to the datalist in the Page_load event as below and this is working fine.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataList();
}
}
Any idea why this event is never fired?
Also is my code is right as far as accessing the hiddenfield value is concerned?
Have you set commandname property of hyperlink field?
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select">Select</asp:LinkButton>
Now in selectedindexchanged method:
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
int idx = DataList1.SelectedIndex;
Label lbl = (Label)DataList1.Items[idx].FindControl("Label1");
int id =Convert.ToInt32(DataList1.SelectedValue);
}
Does it work?

ASP.NET Gridview ButtonField onclick fires containing row's onclick event

I have a gridview row that when clicked has to do postback A and a buttonfield in that row that when clicked has to do postback B. The problem is that when i click on the buttonfield, both event1 and event2 gets fired. Below is the code.
protected void gdv_RowCommand(object sender, GridViewCommandEventArgs e)
{
string arg = Convert.ToString(((System.Web.UI.WebControls.CommandEventArgs)(e)).CommandArgument);
if (e.CommandName == "Command1")
{
doEvent1(arg);
}
else if (e.CommandName == "Command2")
{
doEvent2(arg);
}
}
protected void gdv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton b1 = (LinkButton)e.Row.Cells[0].Controls[0];
matchesButton.CommandArgument = arg1;
LinkButton rowLink = (LinkButton)e.Row.Cells[1].Controls[1];
rowLink.CommandArgument = arg2;
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(rowLink, "");
}
}
And this is the asp code for the gridview
<Columns>
<asp:ButtonField DataTextField="f1" HeaderText="H1" CommandName="Command1" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btn1" runat="server" Text="" CommandName="Command2" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
try to use this
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Command2")
{
// Your Code here
}
}
to find control in grid view use this code
LinkButton lnkbtn= (LinkButton)e.Row.FindControl("btn1");
Try adding both the button with in same <asp:TemplateField> if you don't want separate headers
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="button" runat="server" CommandName="Command1" />
<asp:LinkButton ID="btn1" runat="server" CommandName="Command2" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
if you want separate headers make two separate <asp:TemplateField> and then add buttons in them.

how to get value of textbox in listview?

<asp:ListView runat="server" ID="lvAttachments" ItemPlaceholderID="ph" OnItemDataBound="lvAttachments_ItemDataBound">
<LayoutTemplate>
<asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<asp:LinkButton runat="server" ID="btnReject" OnClick="btnReject_Click"></asp:LinkButton>
<asp:TextBox runat="server" ID="tbReason" CssClass="textbox" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:ListView>
My question is: how to get text from textbox, while btnReject click action?
protected void btnReject_Click(object sender, EventArgs e)
{
LinkButton btnReject = (LinkButton)sender;
// how to get tbReason.Text from this item?
}
Regards
edit:
Problem resolved !
We just need to add in Page_Load code to prevent re-load listview and clear textboxes:)
http://forums.asp.net/t/1712482.aspx/1
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lvAttachments.DataSource = tAttachmentBO.getAttachmentsToAccept();
lvAttachments.DataBind();
}
}
Something like this should work
I'm on ipad so apologize for any mistakes
TextBox txt = (TextBox)btnReject.Parent.FindControl("tbReason")

casting LinkButton Telerik

I have a LinkButton inside of a telerik grid when it is clicked it updates the database. The trouble comes at the first line where I recieve an error that I am unable to cast the radgrid to linkButton. Can someone shed a little light. Here is the Error Msg.
Telerik.Web.UI.RadGrid' to type 'System.Web.UI.WebControls.LinkButton
Here is my method:
protected void rad_grdCompleteRequest(object sender, EventArgs e)
{
LinkButton btnCompleteRequest = (LinkButton)sender;
int requestID = Convert.ToInt32(btnCompleteRequest.Attributes["RequestID"]);
SqlManager.UpdateRequest(requestID, 3);
Response.Redirect(Request.RawUrl);
}
Please try with the below code snippet.
.aspx
// Normal Mode
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
</ItemTemplate>
</telerik:GridTemplateColumn>
// Edit Mode
<telerik:GridTemplateColumn>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
</EditItemTemplate>
</telerik:GridTemplateColumn>
.aspx.cs
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton LinkButton1 = sender as LinkButton;
// Do your logic here
}
Please let me know if you have any concerns.

Categories

Resources