Count the no of hyeper link controls in asp.net page [Resolved]

Posted by Kondapallisrinivas under ASP.NET on 10/16/2012 | Points: 10 | Views : 1759 | Status : [Member] | Replies : 2
Dear all

My page contains different controls. But i want to count the hyperlinks among all the controls.

Please help for me

Thank you in advance

Regards
Kondapalli Srinivas




Responses

Posted by: Kondapallisrinivas on: 10/16/2012 [Member] Starter | Points: 25

Up
0
Down

Resolved
Dear All

At finally i find the solution for my above problem............

private void ListControlCollections() {
ArrayList controlList = new ArrayList();
AddControls(Page.Controls, controlList);
foreach (string str in controlList) { Response.Write(str + "<br/>");
}
Response.Write("Total Controls:" + controlList.Count);
}
private void AddControls(ControlCollection page, ArrayList controlList)
{
foreach (Control c in page) { if (c.ID != null)
{
controlList.Add(c.ID); } if (c.HasControls())
{
AddControls(c.Controls, controlList);
}
}
}

The above function returns the total no of controls in asp.net page......

Kondapallisrinivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vasanthmvp on: 10/17/2012 [Member] Starter | Points: 25

Up
0
Down
Hi KondapalliSrinivas,

I think your question was different, among all the controls in the aspx page.. you want to count the number of hyperlinks controls on it.
If you want to count the number of controls in your page, or form, this serves a more simpler way.
Suppose,
.aspx Page:

<form id="form1" runat="server">
<asp:HyperLink ID="L1" runat="server" />
<asp:HyperLink ID="L2" runat="server" />
<asp:HyperLink ID="L3" runat="server" />
<asp:HyperLink ID="L4" runat="server" />
</form>

.aspx.cs:

int i;
Protected void Page_Load(object sender, EventArgs e)
{
foreach(Control c in form1.Controls) // Here if i take this.Page.Controls instead of form1.Controls, then it considers the whole form as one tag . and results only 1 control.. not considering the child controls.
{
if(c.ID != null)
{
i++;
}
}

Response.Write("Total Number Of Controls : " + i);

}

If i am not wrong, the above code is not appropriate.
If you want to count the number of hyperlinks then from the code behind, we must find the control by its ID. If we can by any other way, please post here.

Regards,

Awesome Coding !! :)

Kondapallisrinivas, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response