带DropDownList的EditItemTemplate - onselectedindexchanged

时间:2013-11-11 16:05:57

标签: c# asp.net

在我的EditTemplate中,我有一个DropDownList:

<EditItemTemplate>                      
    <asp:DropDownList ID="ddlFixture" runat="server"   AutoPostBack="True"   onselectedindexchanged="fixtureSelected"
           DataSourceID="FixtureDataSource"  DataTextField="WTag" DataValueField="WTag" AppendDataBoundItems="true" SelectedValue='<%#Bind("Fixure") %>'>
    </asp:DropDownList>                                                         
</EditItemTemplate>

当用户从DropDownList中选择一个项目时,我也想填充EditMode中的其他字段。

你会注意到onselectedindexchanged =“fixtureSelected”我正在调用fixtureSelected。我已经注意到我面临的一些问题:

protected void fixtureSelected(object sender, EventArgs e)
{
     // Below I am trying to get value of ddlFixture but it cannot recognize ddlFixture. GettingThe name 'ddlFixture' does not exist in the current context

     string fixtureId = ddlFixture.SelectedItem.Value;

    // I also need to update the text in EditMode but this will not work either. Get similar message as for ddlFixture 

    txtCampus.Text = "Campus1";             
}

1 个答案:

答案 0 :(得分:3)

您的问题是由于您尝试访问的控件位于GridView的edititemtemplate内部。

要访问下拉列表实例,您可以使用:

DropDownList ddlFixture = sender as DropDownList;

要获得edititemtemplate中的其他控件,请使用:

ddlFixture.NamingContainer.FindControl("control_id")

例如:

TextBox txtCampus = ddlFixture.NamingContainer.FindControl("txtCampus") as TextBox;