面对将标签值传递到listview的困难

时间:2015-06-07 19:32:24

标签: c# asp.net listview label formview

我正在寻找的是获得" Label10"这是进入viewmsgView然后根据标签值绑定FormView1,注意:viewmsgView& FormView1位于同一视图和同一页面中。但我收到如下错误消息,我已添加查找控件,因为它给出错误消息,控件不可用或不存在。

"对象引用未设置为对象的实例"在线旁边:

RedMsgDAADP.SelectCommand.Parameters.AddWithValue("@mailno", ((Label)myControl).Text);

viewmsgView包含Label10以及titlehyplink,当用户点击它时将显示并绑定FormView1

<asp:ListView ID="viewmsgView" runat="server">
                        <ItemTemplate>


                        <div class="col-lg-12">

                        <table class="table table-inbox table-hover" style="width:100%; margin-left:0px;">
                    <tbody>
                    <tr class="unread">

                        <td>

                            <asp:ImageButton ID="deltimgbtn" runat="server" 
                                ImageUrl="~/iconsimg/Delete2.png" onclick="deltimgbtn_Click" /> </td>  <td>
                            <asp:ImageButton ID="ReplyBtn" runat="server" ImageUrl="~/iconsimg/reply.png" 
                                onclick="ReplyBtn_Click" /></td>
                        <td class="view-message  dont-show"><asp:Label ID="msgfromlbl" runat="server" Text='<%# Bind ("sender") %>'></asp:Label></td>
                        <td class="view-message "><asp:LinkButton
                                ID="titlehyplink" runat="server" Text='<%# Bind ("Mestitle") %>' onclick="titlehyplink_Click"></asp:LinkButton></td>
                        <td class="view-message  inbox-small-cells"><asp:Label ID="Label10" runat="server" Style="color: #FFFFFF" Text='<%# Bind ("mailno") %>'></asp:Label></td>
                        <td class="view-message  text-right"><asp:Label ID="datelbl" runat="server" Text='<%# Bind ("Date") %>'></asp:Label></td>
                    </tr></tbody></table>
                    </div>
                        </ItemTemplate>
                        </asp:ListView>

FormView1将根据Label10显示消息详细信息,该信息位于viewmsgView

<asp:FormView ID="FormView1" runat="server"  Width="100%">

                        <ItemTemplate>


                     <br /><br />
                     <div style="margin-bottom:40px; background-color:#CCCCCC;">
                         <div class="col-md-4">

                                        <label>From:</label>
                                        &nbsp;
                                        <asp:Label ID="senderLabel" runat="server" Text='<%# Bind("sender") %>' />

                                    </div>
                                    <div class="col-md-4">
                                        <label>Date:</label>

                                        <asp:Label ID="DateLabel" runat="server" Text='<%# Bind("Date") %>' />
                                </div>
                                 <div class="col-md-4">
                                        <label>To:</label>

                                        &nbsp;<asp:Label ID="Label11" runat="server" Text='<%# Bind("Receiver") %>' />
                                    </div>

                                     <div class="col-md-4">
                                   <label>Mail No:</label>

                                    <asp:Label ID="Label15" runat="server" Text='<%# Eval("mailno") %>' />
                              </div>

                                 <div class="col-md-4">
                                        <label>Ads No:</label>

                                        &nbsp;&nbsp;<asp:Label ID="AdsIDLabel" runat="server" Text='<%# Bind("AdsID") %>' />
                                  </div>

                                   </div>
                            <br />
                            <br />
                            <asp:Label ID="MestitleLabel" runat="server" Text='<%# Bind("Mestitle") %>' Font-Bold="True"
                                Font-Size="Larger" /><br />
                            <br />

                            <asp:Label ID="MessageLabel" runat="server" Text='<%# Bind("Message") %>' Width="100%"
                                CssClass="labelmsg" />
                        </ItemTemplate>
                    </asp:FormView>

标题后缀代码

protected void titlehyplink_Click(object sender, EventArgs e)
    {
        if (Session["UsrNme"] != null)
        {
            using (SqlConnection RedMsgSQLCon = new SqlConnection(sc))
            {
                RedMsgSQLCon.Open();

                SqlDataAdapter RedMsgDAADP = new SqlDataAdapter(@"SELECT * From mails where Receiver=@UID And mailno=@mailno", sc);

                var use = Session["UsrNme"];

                Control myControl = FindControl("Label10");

                RedMsgDAADP.SelectCommand.Parameters.AddWithValue("@UID", use);
                RedMsgDAADP.SelectCommand.Parameters.AddWithValue("@mailno", ((Label)myControl).Text);

                DataSet RedMsgCVs = new DataSet();

                RedMsgDAADP.Fill(RedMsgCVs);

                FormView1.DataSource = RedMsgCVs.Tables[0];
                FormView1.DataBind();
                FormView1.Visible = true;
            }

        }
        else
        {

        }
    }

1 个答案:

答案 0 :(得分:0)

Label10控件是viewmsgView中的子控件,将为具有唯一ID的每一行创建,您将拥有与viewmsgView中的行一样多的Label10控件。您可以毫不费力地获取价值的一种方法是更改​​这些行:

<asp:LinkButton ID="titlehyplink" runat="server" Text='<%# Bind ("Mestitle") %>' onclick="titlehyplink_Click"></asp:LinkButton>

进入这个:

<asp:LinkButton ID="titlehyplink" runat="server" Text='<%# Bind "Mestitle") %>' CommandArgument='<%# Bind ("mailno") %>' OnCommand="titlehyplink_Command"></asp:LinkButton>

然后再进行代码更改

protected void titlehyplink_Click(object sender, EventArgs e)

protected void titlehyplink_Command(object sender, CommandEventArgs e)

在那种方法中你可以使用这样的东西:

RedMsgDAADP.SelectCommand.Parameters.AddWithValue("@mailno", e.CommandArgument);

几点提示:

的FindControl(&#34; Label10&#34); 只查看Page控件集合而不是子控件集合。

Label10的ID将自动生成,其中一个数字添加到它的末尾,如Label10_0,并且还包含父控件的id,如viewmsgView_Label10_0,因此您无法通过&#34; Label10&#34找到它;