user2721874
user2721874

Reputation:

How to access aspx control in code behind?

NET. i am trying to access the div tag in code behind which is inside the SeparatorTemplate Here is my aspx code

<div>
    <asp:DataList ID="DataList1" runat="server">
        <ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
        <HeaderTemplate>
            <table width="900px">
                <tr>
                    <td width="300px">
                        <b>Name</b>
                    </td>
                    <td width="300px">
                        <b>Account No</b>
                    </td>
                    <td width="300px">
                        <b>Company</b>
                    </td>
                </tr>
            </table>
        </HeaderTemplate>
        <ItemTemplate>
            <table width="900px">
                <tr>
                    <td align="left" width="300px">
                        <%# DataBinder.Eval(Container.DataItem, "Name")%>
                    </td>
                    <td align="left" width="300px">
                        <%# DataBinder.Eval(Container.DataItem, "AccountNo")%>
                    </td>
                    <td align="left" width="300px">
                        <%# DataBinder.Eval(Container.DataItem, "Company")%>
                    </td>
                </tr>
            </table>
        </ItemTemplate>
        <HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
        <SeparatorTemplate>
            <div id="divSeprator" runat="server">//This div tag i want to access in the code behind  
            <br />
            </div>

        </SeparatorTemplate>
    </asp:DataList>
</div>

I have tried accessing the this.Controls and DataList1.Controls but both of those doesn't contain this div i know it is in the SepratorTemplate but i don't know how to access control from that template because there is nothing to find the controls.

Upvotes: 2

Views: 3714

Answers (4)

Wyller Gomes
Wyller Gomes

Reputation: 53

HtmlGenericControl divSeprator = (HtmlGenericControl)DataList1.Items[0].FindControl("divSeprator");

Where 0 is your item index.

Or just bind a DataList1_ItemDataBoud event and use:

if(e.Item.ItemType == ListItemType.Separator)
   HtmlGenericControl divSeprator = (HtmlGenericControl)e.Item.FindControl("divSeprator");

Upvotes: 0

Devraj Gadhavi
Devraj Gadhavi

Reputation: 3611

You will need to find it from the datalist row as below.

HtmlGenericControl div = (HtmlGenericControl)yourDataList.Items[0].FindControl("dvSeparator");

You can pass the index of the data list item (row) in .Items[] for which you want to find the div for processing.

If you want to process div from all the datalist items then you can do it in the item data bound event of the datalist as @Upvote MarkAnswer has suggested in his answer.

Upvotes: 0

Sasidharan
Sasidharan

Reputation: 3740

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    // Find the div control as htmlgenericcontrol type, if found apply style
    System.Web.UI.HtmlControls.HtmlGenericControl div =  (System.Web.UI.HtmlControls.HtmlGenericControl)e.Item.FindControl("DivContent");

    if(div != null)
        div.Style.Add("border-color", "Red");

}

Upvotes: 3

Dhaval
Dhaval

Reputation: 2861

you need to make tag runat="sever" and give it id

<div id="div" runat="server">

Then you can access it by using

HtmlGenericControl div = HtmlGenericControl("div")

Upvotes: -1

Related Questions