Having this piece of code:
<asp:LinkButton runat="server" OnClick="ChangeImage_Click" >
<img src="imagethatshouldbechanged" />
</asp:LinkButton>
And this:
protected void ChangeImage_Click(object sender, EventArgs e)
{
//Code here
}
How do I change the image when I click the linkbutton.
You need to make img runat="server" and assign id to it to access in ChangeImage_Click or use asp:Image instead of img
HTML
<asp:LinkButton runat="server" OnClick="ChangeImage_Click" >
<img id="img1" runat="server" src="imagethatshouldbechanged" />
</asp:LinkButton>
Code bahind
protected void ChangeImage_Click(object sender, EventArgs e)
{
img1.Src = "url";
}
Try this way
<asp:LinkButton runat="server" OnClick="ChangeImage_Click" >
<img runat="server" id="myImage" src="imagethatshouldbechanged" />
</asp:LinkButton>
protected void ChangeImage_Click(object sender, EventArgs e)
{
myImage.Src="YourImagePath";//Like App/app1/image1.jpeg
}
Related
I am generating buttons inside foreach loop
<% foreach (var myObject in myObjectList)
{
%>
<b>Text field</b>: <%= myObject.Text%><br>
<asp:Button ID="" runat="server" OnClick="WaitQueueDeleteBtn_Click" CommandArgument="pass myObjectText" Text="Delete"/>
<% } %>
It seems that I cannot use myObject's field inside CommandArgument however myObjectList is accessible
i.e
<asp:Button ID="" runat="server" OnClick="WaitQueueDeleteBtn_Click" CommandArgument="<%#myObjectList.Count%>" Text="Delete"/>
The above statement would work but the one below would not
<asp:Button ID="" runat="server" OnClick="WaitQueueDeleteBtn_Click" CommandArgument="<%#myObject.Text %>" Text="Delete"/>
Any idea why? And how can I pass myObject's field values as CommandArgument?
Update:
Button method in class
protected void WaitQueueDeleteBtn_Click(object sender, EventArgs e)
{
}
It's so easy with Repeater
Markup
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<div><b>Text field</b>: <%# Eval("Text") %></div>
<asp:Button runat="server" CommandArgument='<%# Eval("ID") %>' Text="Delete" />
</ItemTemplate>
</asp:Repeater>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Repeater1.DataSource = YOUR_DATA_SOURCE; // myObjectList
Repeater1.DataBind();
// ...
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandArgument == null) return;
var id = int.Parse(e.CommandArgument.ToString());
// your logic here ...
}
You can add multiple buttons and using CommandName can figure out which one clicked.
Hope this helps.
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);
}
I am using a multiview control in asp.net, and on ActiveViewChanged event I want set default button programmatically as of the selected view so am using code:
if(myMultiview.GetActiveview() == myView)
this.Page.Form.DefaultButton = btnDefault.UniqueID;
here btnDefault is exist in myView, even though it throws exception 'System.NullReferenceException'
please suggest me the solutions
I had similar type of problem, And i try this,
<asp:Panel id="myPanel" runat="server" DefaultButton="ButtonID">
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server" >
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</asp:View>
<asp:View ID="View2" runat="server">
<asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" />
</asp:View>
</asp:MultiView>
</asp:Panel>
</div>
</form>
protected void Page_Load(object sender, EventArgs e)
{
myPanel.DefaultButton = "Button1";
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("View1 Button is clicked");
MultiView1.ActiveViewIndex = 1;
myPanel.DefaultButton = "Button2";
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write("View2 Button is clicked");
MultiView1.ActiveViewIndex = 0;
}
may be help you.
<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")
How do I access a Control in the LayoutTemplate of a ListView control?
I need to get to litControlTitle and set its Text attribute.
<asp:ListView ID="lv" runat="server">
<LayoutTemplate>
<asp:Literal ID="litControlTitle" runat="server" />
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
Any thoughts? Perhaps via the OnLayoutCreated event?
Try this:
((Literal)lv.FindControl("litControlTitle")).Text = "Your text";
The complete solution:
<asp:ListView ID="lv" OnLayoutCreated="OnLayoutCreated" runat="server">
<LayoutTemplate>
<asp:Literal ID="lt_Title" runat="server" />
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
In codebehind:
protected void OnLayoutCreated(object sender, EventArgs e)
{
(lv.FindControl("lt_Title") as Literal).Text = "Your text";
}
This technique works for template layout; use the init event of the control:
<asp:ListView ID="lv" runat="server" OnDataBound="lv_DataBound">
<LayoutTemplate>
<asp:Literal ID="litControlTitle" runat="server" OnInit="litControlTitle_Init" />
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
And capture a reference to the control for use in the code-behind (e.g) in the ListView's DataBound event:
private Literal litControlTitle;
protected void litControlTitle_Init(object sender, EventArgs e)
{
litControlTitle = (Literal) sender;
}
protected void lv_DataBound(object sender, EventArgs e)
{
litControlTitle.Text = "Title...";
}
For Nested LV Loop:
void lvSecondLevel_LayoutCreated(object sender, EventArgs e)
{
Literal litText = lvFirstLevel.FindControl("lvSecondLevel").FindControl("litText") as Literal;
litMainMenuText.Text = "This is test";
}
In case you need the VB version, here it is
Dim litControl = CType(lv.FindControl("litControlTitle"), Literal)
litControl.Text = "your text"