1

我在 ListView 中有一个 DropDownList 。我想在单击命令时获取数据。这是我的代码..

protected void ListView2_ItemCommand(object sender, ListViewCommandEventArgs e){

string shipmethod = ((DropDownList)e.Item.FindControl("ShippingComapnyDDL")).SelectedValue;

}

但它总是返回空值..我用谷歌搜索了大约 3 个小时,并尝试了很多功能..但仍然无法解决这个错误..

请帮帮我,伙计们,

更新 这是我的 aspx 页面

 <asp:DropDownList ID="ShippingComapnyDDL"  runat="server" SelectedValue='<%# Eval("ShippingCompany") %>'>
<asp:ListItem Text="" Value=""></asp:ListItem>
<asp:ListItem Text="FedEx" Value="FedEx"></asp:ListItem>
<asp:ListItem Text="UPS" Value="UPS"></asp:ListItem>
<asp:ListItem Text="Other" Value="Other"></asp:ListItem>
</asp:DropDownList>
4

4 回答 4

0

尝试这个:

  DropDownList ddl = (DropDownList)e.Item.FindControl("ShippingComapnyDDL");
  if (ddl != null)
  {
       //code here
  }
于 2012-04-10T07:29:31.793 回答
0

请试试

<%#DataBinder.Eval(Container.DataItem,"ShippingCompany")%>

代替

<%#Eval("ShippingCompany")%>

更多细节:

http://aspadvice.com/blogs/joteke/archive/2008/08/24/Is-_2200_Databinding-methods-such-as-Eval_280029002C00_-XPath_280029002C00_-and-Bind_28002900_-can-only-be-used-in-the-数据绑定控制的上下文_2E002200_-causing-you-grief_3F00_.aspx

于 2012-04-10T08:11:23.863 回答
0

你有设置标题吗?试试

    if(e.Item.ItemIndex!=-1)
{
     string shipmethod = ((DropDownList)e.Item.FindControl("ShippingComapnyDDL")).SelectedValue; 
}

if not working then try     
string shipmethod = (e.Item.FindControl("ShippingComapnyDDL") as DropDownList).SelectedValue; 
于 2012-04-10T07:26:57.787 回答
0
<asp:ListView ID="lvwCoreConfigureData" runat="server" DataKeyNames="Course_Config_Id" OnItemCommand="lvwCore_Config_ItemCommand" OnItemDataBound="lvwCore_Config_ItemDataBound">
            <LayoutTemplate>
                <table class="table mobile-stacked mb-0">
                    <thead>
                        <tr role="row">
        
                            <th style="width: 240px">
                                <asp:Label runat="server" Text="<%$Lang:Resource.Core_Config_Course %>"></asp:Label>
                            </th>
        
                        </tr>
                    </thead>
                    <tbody>
                        <tr runat="server" id="itemPlaceholder" />
                    </tbody>
                </table>
            </LayoutTemplate>
        
            <ItemTemplate>
                <tr role="row">
        
                    <td data-th="Name">
                    <asp:DropDownList ID="ConfigCourseName" runat="server" AutoPostBack="true" CssClass="form-control" OnSelectedIndexChanged="ConfigCourseName_SelectedIndexChanged"></asp:DropDownList>
                    </td>
                    
                </tr>
            </ItemTemplate>
        
        </asp:ListView>
            
                                
                            

2. 此方法将在编辑模式下提供数据绑定到下拉列表

protected void lvwCore_Config_ItemDataBound(Object sender, ListViewItemEventArgs e)
                        {
                            if (e.Item.ItemType == ListViewItemType.DataItem)
                            {
                                ListViewDataItem dataItem = (ListViewDataItem)e.Item; // load one row item

                                var CourseId = ((Edulearn.Model.CoreProgramme.CoreProgrammeConfigrationDM)dataItem.DataItem).CourseId; //

                                DropDownList ConfigCourseName = (e.Item.FindControl("ConfigCourseName") as DropDownList);
                                ConfigCourseName.DataSource = CoreProgrammeRepository.GetCoreProgrammeCourseList(TimeZoneId, new { Name = "TEST" }); // Get the data to dropdown (all data)
                                ConfigCourseName.DataTextField = "Name"; // assign here which one need to display
                                ConfigCourseName.DataValueField = "CourseId"; // assign here which one is the value for database save 
                                ConfigCourseName.DataBind(); // bind the data
                                ConfigCourseName.SelectedValue = CourseId.ToString(); // Here selected values from database will be added 
                                ConfigCourseName.Items.Insert(0, new ListItem("Please select")); // Default value only for display

                            }
                        }
于 2022-02-22T09:29:13.620 回答