Geeker Geeker - 5 days ago 5
ASP.NET (C#) Question

OnServerClick on anchor tag is not firing

I have 3 tabs, On which I want to show the respective data. I have given a attribute as

onserverclick
to the anchor tag inside
<li>
. but the problem is that it is not firing.

Also When I tried putting a breakpoint it was not working. Here is my piece of html code:-

<ul class='tabs1'>
<li><a href='#tab1' id="allNews" runat="server" onserverclick="allNews_ServerClick">All News</a></li>

<li><a href='#tab2' id="forNgo" runat="server" onserverclick="forNgo_ServerClick">For NGO</a></li>
<li><a href='#tab3' id="fromNgo" runat="server" onserverclick="fromNgo_ServerClick">From NGO</a></li>
</ul>


Please suggest what might be the issue which is preventing to work.

UPDATE

Server code:-

protected void fromNgo_ServerClick(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
{
string query = "SSELECT dbo.tbl_post.title, dbo.tbl_post.description, dbo.tbl_post.active, dbo.tbl_post.dateforPost, dbo.tbl_ngoname.ngo_name, dbo.tbl_User.usertype FROM dbo.tbl_post INNER JOIN dbo.tbl_User ON dbo.tbl_post.UserId = dbo.tbl_User.Id INNER JOIN dbo.tbl_ngoname ON dbo.tbl_post.NgoId = dbo.tbl_ngoname.Id AND dbo.tbl_User.NgoId = dbo.tbl_ngoname.Id WHERE (dbo.tbl_User.usertype=2)";
SqlDataAdapter sda = new SqlDataAdapter(query, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
lstNews.DataSource = dt;
lstNews.DataBind();
}
}

Answer

You need to create the Click event for Anchor tag in page load as below

private void Page_Load(object sender, System.EventArgs e)
{
HtmlAnchor HA = new HtmlAnchor();
HA.ServerClick += new EventHandler(linkclickeve1);
HtmlAnchor HA2 = new HtmlAnchor();
HA2.ServerClick += new EventHandler(linkclickeve2);
}

protected void linkclickeve1(object sender, System.EventArgs e)
{

}

protected void linkclickeve2(object sender, System.EventArgs e)
{

}

Example Here

Comments