views:

41

answers:

2

is it possible to access the CouncilIdLabel via DropDownList_SelectedIndexChanged event?!

<tr id="Tr10" runat="server">
            <td width="110px">
                دسته :&nbsp;
            </td>
            <td>
                <asp:DropDownList ID="CategoryDropDownList" runat="server" Font-Names="tahoma" Font-Size="13px" onselectedindexchanged="CategoryDropDownList_SelectedIndexChanged" AutoPostBack="true" SelectedValue='<%# Eval("Category") %>'>
                    <asp:ListItem Text="عمومی" Value="عمومی"></asp:ListItem>
                    <asp:ListItem Text="پزشکی" Value="پزشکی"></asp:ListItem>
                    <asp:ListItem Text="مددکاری" Value="مددکاری"></asp:ListItem>
                    <asp:ListItem Text="روان شناسی" Value="روان شناسی"></asp:ListItem>
                </asp:DropDownList>
                <asp:Label ID="CouncilIdLabel" runat="server" Text='<%# Eval("CouncilId") %>' Visible="false" />
            </td>
        </tr>

it is located in a listview ItemTemplate

please help me, i need to solve this problem really soon

A: 

Yes, it is possible. You'll have to find the control in the listview. Use:

Label coucilIdLabel = (Label)MyListView.FindControl("CouncilIdLabel");
womp
then how to access the text property?!
Mahdi
Sorry, I updated the code a bit.
womp
i have tried this and get this error" Object reference not set to an instance of an object. "
Mahdi
+1  A: 

You need to use FindControl on the ListViewDataItem itself. i.e.

Label coucilIdLabel = (Label)SomeListView.Items[SomeItemIndex].FindControl("CouncilIdLabel");

Or - In your selectedIndex changed event, this should work:

var item = sender.Parent as ListViewDataItem;
Label coucilIdLabel = (Label)item.FindControl("CouncilIdLabel");
zincorp
the sender didnt have the "parent" memberso i used thisDropDownList CategoryDropDown = (sender as DropDownList); var item = CategoryDropDown.Parent as ListViewDataItem; Label coucilIdLabel = (Label)item.FindControl("CouncilIdLabel");and it gave me this : Object reference not set to an instance of an object.
Mahdi
DropDownList CategoryDropDown = (sender as DropDownList); Control item = CategoryDropDown.Parent.FindControl("CouncilIdLabel"); Label coucilIdLabel2 = (Label)item.FindControl("CouncilIdLabel");i think this has solved the problem :-)
Mahdi