面临将标签值传递到列表视图的困难

Facing difficulty to pass label value into listview

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

"Object reference not set to an instance of an object" 行旁边:

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

viewmsgView 其中包括 Label10 以及当用户单击它时将显示并绑定 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 将根据进入 viewmsgView 的 Label10 显示消息详细信息

<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>

titlehyplink 代码隐藏

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
        {

        }
    }

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);

小提示:

查找控件("Label10"); 只查看页面控件集合而不查看子控件集合。

Label10的id是自动生成的,后面加了一个数字,如Label10_0,还包含了父控件的id,如viewmsgView_Label10_0,所以找不到只有 "Label10"