1

在我的 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";             
}
4

1 回答 1

5

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

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

DropDownList ddlFixture = sender as DropDownList;

要在 edititemtemplate 中获取其他控件,请使用:

ddlFixture.NamingContainer.FindControl("control_id")

例如:

TextBox txtCampus = ddlFixture.NamingContainer.FindControl("txtCampus") as TextBox;
于 2013-11-11T16:18:43.090 回答