2 Replies - 897 Views - Last Post: 27 January 2012 - 01:43 PM Rate Topic: -----

Topic Sponsor:

#1 jokertin92  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 26-March 09

Access Gridview element in Code Behind

Posted 27 January 2012 - 04:31 AM

Hi all,
I've created a gridview in which I have added a 'div' section and under that I've created a Listbox (lstEmailIds). Now I want to assign the datasource for the Listview in RowDataBound event. But I'm unable to access the Listbox. Suggest me how can I achieve this. Here is my code.

asp:GridView ID="gridview1" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None" Width="252px" onrowdatabound="gridview1_RowDataBound">
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <table><tr><td valign="middle" align="left">
                </td></tr></table>
                <asp:Button ID="btn1" Text="Show" runat="server" PostBackUrl="#" onmouseover="<%# GetPopupScript() %>" />
                <div id="<%#Eval("issueid")%>DivDetails3" style="Z-INDEX:260; POSITION: absolute;HEIGHT:38px; display:none">
                    <table bgcolor="#cccccc" border="1" bordercolor="#ff9900" cellpadding="1" cellspacing="0"
                        style="border-collapse: collapse; width: 201px; height: 1px;">
                        <tr>
                            <td colspan="1" style="height: 115px">
                                <table bordercolor="#ffff00" cellpadding="1" cellspacing="0" style="width: 229px">
                                    <tr>
                                        <td align="left" colspan="2" style="width: 77%; height: 14px;">
                                            SendMail
                                        </td>
                                        <td align="right" style="height: 14px; width: 23%;">
                                            <a href="#"  önclick="CloseDiv3('DivDetails3');" title="Click here to close.">
                                                X</a>
                                        </td>
                                    </tr>
                                    <tr colspan="2">
                                        <td align="left" valign="middle">
                                            <asp:ListBox ID="lstEmailIds" runat="server" SelectionMode="Multiple" Width="223px">
                                            </asp:ListBox>
                                        </td>
                                    </tr>
                                    <tr colspan="2">
                                        <td>
                                            <asp:TextBox runat="server" ID="txtNotifyComments" Height="47px" TextMode="MultiLine"
                                                Width="222px"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr colspan="2">
                                        <td>
                                            <asp:Button ID="btnSendMail" runat="server" Text="Send Mail" CssClass="standardText"
                                                AccessKey="M" />
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </div>
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
        </asp:GridView>



Is This A Good Question/Topic? 0
  • +

Replies To: Access Gridview element in Code Behind

#2 JackOfAllTrades  Icon User is offline

  • No Sugar Coding Here!
  • member icon

Reputation: 4683
  • View blog
  • Posts: 20,361
  • Joined: 23-August 08

Re: Access Gridview element in Code Behind

Posted 27 January 2012 - 07:06 AM

Moved to ASP.NET
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is online

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1449
  • View blog
  • Posts: 5,761
  • Joined: 21-March 08

Re: Access Gridview element in Code Behind

Posted 27 January 2012 - 01:43 PM

To access elements that are inside of collection controls (DataGrid, GridView, Repeater, etc.), you need to get the row, then search for the control.

In your case, in the RowDataBound event, you would get the row from the GridView...

GridViewRow row = e.Row;



Then you need to find the ListBox...

ListBox listBox = row.FindControl("") as ListBox;

// check to make sure it was found
if (listBox != null)
{
    // listbox was found.  assign datasource
    listBox.DataSource = someDataSource;
    listBox.DataBind();
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1