I have a linkbutton that somehow does not give a postback when clicked. Here is the code
<script type="text/javascript">
$(function () {
$('#divTabs').tabs({
show: function () {
var sel = $('#divTabs').tabs('option', 'selected');
$('#<%= hfLastTab.ClientID %>').val(sel);
},
selected: '<%= hfLastTab.Value %>'
});
});
</script>
<div id="divTabs">
<ul>
<li runat="server" id="TabBar1" ><asp:LinkButton ID="TabLink1" runat="server" OnClick="TabLink1_Click" > Link1 </asp:LinkButton></li>
<li runat="server" id="TabBar2" ><asp:LinkButton ID="TabLink2" runat="server" OnClick="TabLink2_Click"> Link2 </asp:LinkButton></li>
<li runat="server" id="TabBar3" ><asp:LinkButton ID="TabLink3" runat="server" OnClick="TabLink3_Click"> Link3 </asp:LinkButton></li>
</ul>
....
</div>
And the code behind:
protected void TabLink1_Click(object sender, System.EventArgs e)
{
SelectTopBar();
SetSelectTab("TabBar1");
}
protected void TabLink2_Click(object sender, System.EventArgs e)
{
SelectTopBar();
SetSelectTab("TabBar2");
}
protected void TabLink3_Click(object sender, System.EventArgs e)
{
SelectTopBar();
SetSelectTab("TabBar3");
}
I tried putting the breakpoint in the codebehind and it does not hit. The page is not refreshed as well. If I use <asp:Button>, then it will do the submit and everything seems to be fine.
I also tried to see whether there are more than one <form> tags, but there is not.
I also make sure that in the aspx page, those click event are tied.
[EDIT]: The link button is located inside a jquery tab. So instead of using the ordinary <a href... as in the jquery example which does not do any postback, I used asp linkbutton and want the postback.
Any idea?
In case anyone else is looking. I had the same problem with the LinkButton OnClick event not firing, and fixed it by simply using "OnCommand=" instead of "OnClick=".
Found some workaround. I defined another linkbutton outside the <div id="divTabs">, and the linkbutton inside <div id="divTabs"> will call the outer linkbutton. So the code becomes like
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="TabLink1_Click" ></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="TabLink2_Click" ></asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" OnClick="TabLink3_Click" ></asp:LinkButton>
<div id="divTabs">
<ul>
<li runat="server" id="TabBar1" ><asp:LinkButton ID="TabLink1" runat="server"> Link1 </asp:LinkButton></li>
<li runat="server" id="TabBar2" ><asp:LinkButton ID="TabLink2" runat="server"> Link2 </asp:LinkButton></li>
<li runat="server" id="TabBar3" ><asp:LinkButton ID="TabLink3" runat="server"> Link3 </asp:LinkButton></li>
</ul>
....
</div>
In the code behind, inside the Page_Load function
TabLink1.OnClientClick = "document.getElementById('" + LinkButton1.ClientID + "').click();";
TabLink2.OnClientClick = "document.getElementById('" + LinkButton2.ClientID + "').click();";
TabLink3.OnClientClick = "document.getElementById('" + LinkButton3.ClientID + "').click();";
I'm guessing the jQuery library might have some impact such that the link does not call a postback, so I'm using the workaround above.
Have you tried including a responseredirect as the last line of each event like in the below example?
protected void TabLink1_Click(object sender, System.EventArgs e)
{
SelectTopBar();
SetSelectTab("TabBar1");
Response.Redirect(Request.Url.AbsoluteUri);
}
<script type="text/javascript">
$(function () {
$('#divTabs').tabs({
show: function () {
var sel = $('#divTabs').tabs('option', 'selected');
$('#<%= hfLastTab.ClientID %>').val(sel);
},
selected: '<%= hfLastTab.Value %>'
});
});
</script>
<div id="divTabs">
<ul>
<li runat="server" id="TabBar1" ><asp:LinkButton ID="TabLink1" CauseValidation="false" runat="server" OnClick="TabLink1_Click" > Link1 </asp:LinkButton></li>
<li runat="server" id="TabBar2" ><asp:LinkButton ID="TabLink2" runat="server" CauseValidation="false" OnClick="TabLink2_Click"> Link2 </asp:LinkButton></li>
<li runat="server" id="TabBar3" ><asp:LinkButton ID="TabLink3" runat="server" CauseValidation="false" OnClick="TabLink3_Click"> Link3 </asp:LinkButton></li>
</ul>
....
</div>
Use CausesValidation="false"
In case anyone's looking for a solution that actually works, it's because you're fiddling with javascript somehow and that's overriding the .NET postback behaviour.
In my case it was simply using Bootstrap and adding the data-toggle="buttons" e.markup to the button group like:
<div class="btn-group btn-group-sm btn-group-justified" data-toggle="buttons">
That was recognized by Bootstrap JavaScript because it needs to watch these buttons in the group to be able to display their toggle status correctly (i.e. adding/removing the "active" class to/from children).
Solution: removed that data-toggle and implemented the toggle status highlighting with some C# inline code.
Maybe it is .NET framework's fault,
for me __doPostBack did not work in all pages because I compiled my project with a newer version of Visual studio. I changed "Target framework" from ".NET framework 4.0" to ".NET framework 3.0", and after that rebuild project and it has worked.
Related
I got a problem where when the button is clicked, the panel would not change. In the sense of error. I can change the panel using the following hyperlink.
Test
But I want to use an asp:LinkButton and it did not work like this.
<asp:LinkButton ID="btngantipassword" runat="server" CssClass="btn btn-lg btn-primary btn-block" href="#" OnClick="$('#panel1').hide(); $('#panel2').show()">Change Password</asp:LinkButton>
I am still a beginner in using asp.net. Help me to solve this problem.
Almost every Control has a Visibility property. This property can be set on the aspx page in the Control itself
<asp:Label ID="Label1" runat="server" Text="Label" Visible="false"></asp:Label>
The property can also be set in code behind
Label1.Visible = false;
The visibility property works different than with JavaScript and CSS. Normally if you define a CSS class with display:none for example, you won't see it in the browser but it DOES exist. If you look at the HTML you can find it.
But in asp.net the hidden control is not rendered to the browser and therefore does not exist in the HTML.
To expand this to your question. The Panel Control can be used like you ask in your question.
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
<br />
<asp:Panel ID="Panel1" runat="server">
<p>Normal Content here...</p>
</asp:Panel>
This will render in HTML as
<div id="Panel1">
<p>Normal Content here...</p>
</div>
With the OnClick event of LinkButton1, you can change the Visibility of Panel1
protected void LinkButton1_Click(object sender, EventArgs e)
{
if (Panel1.Visible == false)
{
Panel1.Visible = true;
}
else
{
Panel1.Visible = false;
}
}
I have a slideshow that images load from sql server.
here is my html code:
<!-- Elastislide Carousel -->
<ul id="Ul1" class="elastislide-list">
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<li>
<asp:Label ID="lbl" runat="server" Text='<%#Eval("IDMusic") %>' Visible="True"></asp:Label>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%#Eval("ImageUrl") %>' Width="250px" Height="250px" OnClick="ImageButton1_Click" />
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
<!-- End Elastislide Carousel -->
<br /><br />
</div>
</div>
Now when, I run Project and user click on one of the imageButtons, I want to understand what is he/she clicked? for example he/she click on second ImageButton, Then I want to save IDMusic into a session and use this session in another page but I don't know how can i get IDmusic user clicked on?
here is my code behind :
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
foreach (RepeaterItem item in Repeater2.Items)
{
Label lbl = (Label)item.FindControl("lbl");
if (lbl != null)
{
Session["selectedmusicID"] = lbl.Text;
}
}
Response.Redirect("music.aspx");
}
There is no easy way to do so while handling click on the image itself. However you can leverage ItemCommand event here, which is a repeater event. Basically when some control is clicked inside the repeater, it generates a command in repeater which you can handle, and you can have item-specific arguments for it.
To define a command and args:
<asp:ImageButton ID="ImageButton1" ... CommandName="Select" CommandArgument='<%#Eval("IDMusic") %>' />
Command name here is custom, choose whatever you like. Now subscribe to the event of the repeater:
<asp:Repeater ID="Repeater2" runat="server"
OnItemCommand="Repeater2_ItemCommand">
And to handle:
protected void Repeater2_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Select") //just in case you have more than one
{
e.CommandArgument //here is your ID
}
}
in my page I have two nested repeaters as follows:
<asp:Repeater runat="server" ID="rptStanze" OnItemDataBound="rptStanze_ItemDataBound">
<ItemTemplate>
<div>
<li class="datatable-item">
<asp:LinkButton runat="server" ID="hypGetDetails" Text="Get Details" OnCommand="GetObjectDetails" />
<div class="col2">DATE</div>
<div class="col3">VISITORS</div>
</li>
<li>
<asp:Repeater runat="server" ID="rptDetails" OnItemDataBound="rptDetails_ItemDataBound">
<ItemTemplate>
<div class="subitem">
<div class="col2"><asp:Literal runat="server" ID="ltlDetTitle" /></div>
<div class="col3"><asp:Literal runat="server" ID="ltlDetViews" /></div>
<div class="col4"><asp:Literal runat="server" ID="ltlDetComments" /></div>
</div>
</ItemTemplate>
</asp:Repeater>
</li>
</div>
</ItemTemplate>
</asp:Repeater>
In the first, I have a LinkButton with a method that fills the nested repeater. Concerning the bounding of data I've no problems, but I dunno how to get the correct nested repeater to fill. In the GetObjectDetails method how can I get it?
In your Command handler, one of the parameters is the button that sent the request, commonly referred to as the "sender". Using that, you just need to find the container of the button that was clicked and then find the repeater within that container.
Here is a C# example:
protected void GetObjectDetails(object sender, CommandEventArgs e)
{
LinkButton hypGetDetails = (LinkButton)sender;
RepeaterItem ri = (RepeaterItem)hypGetDetails.NamingContainer;
Repeater rptDetails = (Repeater)ri.FindControl("rptDetails");
}
I have two listboxes in my application on button click i am pushing the item from one list box to other , the code works fine but it causes postback , while i move the item from one list box to other whole page is being loaded again , how i can prevent this .
This will be the code on my aspx page
<div class="bx1">
<telerik:RadListBox ID="RadListBox1" runat="server" DataTextField="Name" DataValueField="Id"
Width="250px">
</telerik:RadListBox>
</div>
<div style="height:7px"></div>
<div class="bx5">
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="images/dwnArrow.png" OnClick="MoveDownClick" />
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="images/uparrow.png" OnClick="MoveUpClick" />
</div>
<div style="height:7px"></div>
<div class="bx1">
<telerik:RadListBox ID="RadListBox2" runat="server" DataTextField="Name"
DataValueField="Id" Width="250px" >
</telerik:RadListBox>
</div>
This is my code behind for listbox
protected void MoveDownClick(object sender, EventArgs e)
{
if (RadListBox1.SelectedIndex < 0)
{
}
else
{
RadListBox2.Items.Add(RadListBox1.SelectedItem);
RadListBox1.Items.Remove(RadListBox1.SelectedItem);
RadListBox2.SelectedItem.Selected = false;
}
}
protected void MoveUpClick(object sender, EventArgs e)
{
if (RadListBox2.SelectedIndex < 0)
{
}
else
{
RadListBox1.Items.Add(RadListBox2.SelectedItem);
RadListBox2.Items.Remove(RadListBox2.SelectedItem);
RadListBox1.SelectedItem.Selected = false;
}
}
If your version of Visual Studio is less than 2008, then first download the ajax from the following site and install it:
http://ajaxcontroltoolkit.codeplex.com/
Add a reference to the System.Web.Extensions dll and then add the following line right after your opening <form> tag:
<asp:ScriptManager runat="server" ID="Script1"></asp:ScriptManager>
Replace the Your Code in following piece of code with your entire code that you have written above:
<asp:UpdatePanel runat="Server" ID="u1">
<ContentTemplate>
Your Code
</ContentTemplate>
</asp:UpdatePanel>
That's it, this will stop posting back your page.
Take the time to look into using jQuery along with Microsoft's Ajax with Update Panels or moving into jQuery exclusively where possible. Here are two links that are excellent reads regarding the subject:
Microsoft Ajax? http://encosia.com/why-aspnet-ajax-updatepanels-are-dangerous/
jQuery Ajax: http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/
A strange problem.
On my page i have a placeholder where I create a repeater on run time.
In ItemTemplate i have a button that invokes repeater_ItemCommand event.
I put a breakpoint on first line of repeater_ItemCommand event, when the button is triggered the event is invoked as planned, but after RebindRepeaters() is finished, when i click on the button again repeater_ItemCommand event is not invoked!
Only if i click on the button one again it invokes, in other scenarios(this is simple version of my page, in original page i have updatepanel that complex everything) it never invokes!
My aspx code:
<form id="form1" runat="server">
<div id="talk">
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
My Code-behind:
protected void Page_Init(object sender, EventArgs e)
{
RebindRepeaters();
}
void RebindRepeaters()
{
PlaceHolder1.Controls.Clear();
CreateRepeater(PlaceHolder1);
}
void CreateRepeater(Control container)
{
Repeater repeater1 = new Repeater();
repeater1.ItemCommand += new RepeaterCommandEventHandler(repeater_ItemCommand);
repeater1.ItemTemplate = Page.LoadTemplate("CommentsTemplate.ascx");
container.Controls.Add(repeater1);
repeater1.DataSource = Comment.GetCommentsP(8, 0);
repeater1.DataBind();
}
void repeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "Edit":
RebindRepeaters();
break;
}
}
The CommentsTemplate.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="CommentsTemplate.ascx.cs" Inherits="CommentsTemplate" %>
<ul>
<li>
<div class="show">
<div class="c">
<asp:LinkButton ID="lbtnTitle" runat="server" Text='<%#Eval("EncodedTitle")%>'
CausesValidation="false" CommandName="VoteUp" OnClientClick="viewHide(this);return false;" CommandArgument='<%#Eval("ID")%>'></asp:LinkButton>
<p class="d"><%#Eval("AddedBy")%>,<%#Eval("AddedDate")%></p>
<div class="CommentBody"><%# Eval("EncodedBody") %>
</div>
</div>
<div class="userpanel">
<asp:Panel runat="server" ID="panAdmin" Visible='<%# UserCanEdit %>' style="float:left;">
<asp:ImageButton runat="server" ID="btnSelect" CommandName="Edit" CommandArgument='<%# Eval("ID") %>'
CausesValidation="false" AlternateText="Edit comment" ImageUrl="~/Images/Edit.gif" />
<asp:ImageButton runat="server" ID="btnDelete" CommandName="Delete" CommandArgument='<%# Eval("ID") %>'
CausesValidation="false" AlternateText="Delete comment" ImageUrl="~/Images/Delete.gif"
OnClientClick="if (confirm('Are you sure you want to delete this comment?') == false) return false;" />
</asp:Panel>
</div>
</div>
</li>
</ul>
If you need more info just ask.
Could you try overidding createChildcontrols and add the RebindRepeaters call there instead of in Page_Init? And make sure to give your repeater an explicit ID:
Repeater repeater1 = new Repeater();
repeater1.ID = "repeater1";
Without an id the asp.net event handling won't know where the event was called from.
Dynamic controls have to be re-added on every postback in order for their events to fire. A new control tree is being created on the postback and the dynamic control is no longer in it, therefore ASP.NET can't evaluate the change in order to fire the event.
EDIT: I was able to fire the event each click by removing RebindRepeaters(). You were binding it twice, once here and once in (Page_Init):
void repeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "Edit":
//RebindRepeaters();
break;
}
}