为什么我的第二个listview为空

时间:2014-04-01 18:36:08

标签: c# asp.net listview

我很难过。我有2个相对相同的嵌套列表视图,在按钮Click事件上,第一个listview很好,第二个是空的:

ASPX:

...
<asp:HiddenField ID="hfCourseId" ClientIDMode="Static" runat="server" Value="" />
...


<asp:listView ... ID="lvResponseNames">
...
   <InsertItemTemplate>
       <asp:ListView ... ID="lvQuestions" DataSourceId="sdsQuestions" onItemDataBound="lvQuestions_ItemDataBound">
           <ItemTemplate>
                 ...
           </ItemTemplate>
       </asp:ListView>
       <asp:SqlDataSource ID="sdsQuestions" ...
            SelectCommand="SELECT col1, col2, col3 from tbl where id=0">
       </asp:SqlDataSource>
       <asp:ListView ... ID="lvAdditionalQs" DataSourceId="sdsAdditionalQuestions" onItemDataBound="lvAdditionalQuestions_ItemDataBound">
          <ItemTemplate>
                 ...              
          </ItemTemplate>
       </asp:ListView>
       <asp:SqlDataSource ID="sdsAdditionalQuestions" ...
            SelectCommand="SELECT col1, col2, col3 from tbl where id=@CourseId">
            <SelectParameters>
               <asp:ControlParameter ControlID="hfCourseId" Name="CourseId" PropertyName="Value" />
            </SelectParameters>
       </asp:SqlDataSource>        
       <asp:Button ID="CustomInsertButton" runat="server" Text="Insert Attendee Responses >>" OnClick="CustomInsertButton_Click" />
   </InsertItemTemplate>     
<asp:listView ... ID="lvResponseNames">

aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
     ...
     if (int.TryParse(Request.QueryString["CourseId"], out iCourseId)
     hfCourseId.Value = iCourseId; 

}

...

protected void CustomInsertButton_Click(object sender, EventArgs e)
{

    //Default Questions
    ListView lvQs = (ListView)lvResponseNames.InsertItem.FindControl("lvQuestions") as ListView;
    Response.Write("Checking " + lvQs.Items.Count + " Defualt list view items. <br />");
    SqlDataSource dsDef = (SqlDataSource)lvResponseNames.InsertItem.FindControl("sdsQuestions") as SqlDataSource;
    DataView dvDef = (DataView)dsDef.Select(DataSourceSelectArguments.Empty);
    Response.Write("Checking " + dvDef.Count + " records found in datasource 'Default'. <br />");

    //Additional Quesetions
    ListView lvAQs = (ListView)lvResponseNames.InsertItem.FindControl("lvAdditionalQs") as ListView;
    Response.Write("Checking " + lvAQs.Items.Count + " Additional list view items. <br />");
    SqlDataSource dsAQs = (SqlDataSource)lvResponseNames.InsertItem.FindControl("sdsAdditionalQuestions") as SqlDataSource;
    DataView dvAQs = (DataView)dsAQs.Select(DataSourceSelectArguments.Empty);
    Response.Write("Checking " + dvAQs.Count + " records found in datasource 'Additional'. <br />");
    ...
}

哪个输出:

Checking 9 Defualt list view items. 
Checking 9 records found in datasource 'Default'. 
Checking 0 Additional list view items. 
Checking 6 records found in datasource 'Additional'. 

&#34; 0附加&#34;应该读6,就像默认的一样,但对于我的生活,我无法弄清楚为什么它不会看到&#39;所有ItemTemplates。

现在有任何想法或想法如何调试?

更新4/1/14 9:45 PM :添加了sqldatasource SelectCommands,因为问题似乎已连接到第二个SqlDataSource上的ControlParameter。

当select命令为:

 SelectCommand="SELECT col1, col2, col3 from tbl where id=0"

它工作正常。

但是当它出现时:

 SelectCommand="SELECT col1, col2, col3 from tbl where id=@CourseId"

找到的项目是0.不知何故,它找不到控件ID,即使DataSource找到了9个项目。

2 个答案:

答案 0 :(得分:0)

您是否看过数据库中的内容?也许你得到6条记录,其中没有任何东西或类似的东西。只是一个猜测。

答案 1 :(得分:0)

我最终将代码重新分解为使用

<asp:QueryStringParameter Name="CourseId" QueryStringField="CourseId" />

而不是:

<asp:ControlParameter ControlID="hfCourseId" Name="CourseId" PropertyName="Value" />

即使我在后面的代码中设置了hfCourseId,但由于某种原因它似乎没有按照我需要的方式设置。可能与asp.net页面生命周期有关。但是稍微更改代码,并使用CourseId作为QueryStringParameter似乎可以解决问题。